Skip to content

Commit

Permalink
Hash commands can now take there input as a command argument
Browse files Browse the repository at this point in the history
  • Loading branch information
np committed Nov 20, 2014
1 parent b5b0abf commit a542a75
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 22 deletions.
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,19 @@ List of supported commands:
hx chksum32-decode <HEX>*

# HASHING
hx ripemd-hash
hx sha256
hx ripemd160 [0]
hx sha1 [0]
hx hash160 [0]
hx ripemd-hash [4]
hx sha256 [<HEX-INPUT>]
hx ripemd160 [<HEX-INPUT>] [0]
hx sha1 [<HEX-INPUT>] [0]
hx hash160 [<HEX-INPUT>] [0]
hx hash256 [0]

# HASH BASED MACs
hx hmac-sha224 <KEY> [<HEX-NPUT>] [0]
hx hmac-sha256 <KEY> [<HEX-NPUT>] [0]
hx hmac-sha384 <KEY> [<HEX-NPUT>] [0]
hx hmac-sha512 <KEY> [<HEX-NPUT>] [0]

[0]: Not available in sx
[1]: `hx showtx` is always using JSON output,
`-j` and `--json` are ignored.
Expand All @@ -120,6 +126,11 @@ List of supported commands:
[3]: Rounding is done upward in `hx` and downard in `sx`.
So they agree `btc 1.4` and `btc 1.9` but on `btc 1.5`,
`hx` returns `0.00000002` and `sx` returns `0.00000001`.
[4]: The `ripemd-hash` command is taking raw-bytes as input,
while the other hashing commands are taking hexadecimal encoded inputs.
This is for this reason that `hash160` has been added
(`hx ripemd-hash` is equivalent to `hx encode-hex | hx hash160`
and `hx hash160` is equivalent to `hx decode-hex | hx ripemd-hash`).

PATH ::= <PATH-HEAD> <PATH-CONT>
PATH-HEAD ::= 'A' [address (compressed)]
Expand Down
6 changes: 3 additions & 3 deletions Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ getPoint = pubKeyPoint . getHex "curve point"
putPoint :: Hex s => Point -> s
putPoint = putHex . PubKey

interactHex :: (BS -> BS) -> IO ()
interactHex f = BS.interact (withHex f :: BS -> BS)

interactArgs' :: (IsString s, Eq s) => (s -> IO ()) -> IO s -> ([s] -> s) -> [s] -> IO ()
interactArgs' puts gets f [] = puts . f . return =<< gets
interactArgs' puts gets f xs = case length (filter (=="-") xs) of
Expand All @@ -174,6 +171,9 @@ interactArg msg f = interactArgs f'
interactArgLn :: String -> (BS -> BS) -> [BS] -> IO ()
interactArgLn msg = interactArg msg . (putLn .)

interactHex :: String -> (BS -> BS) -> [BS] -> IO ()
interactHex msg = interactArg msg . withHex

writeArg :: BS -> BS -> IO ()
writeArg "-" = BS.putStr
writeArg fp = BS.writeFile (B8.unpack fp)
Expand Down
33 changes: 19 additions & 14 deletions hx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,11 @@ mainArgs ["encode-hex"]{-deprecated-}= interactLn encodeHex
mainArgs ["decode-hex"]{-deprecated-}= BS.interact $ decodeHex "input"

mainArgs ["ripemd-hash"] = interactLn $ encodeHex . hash160BS . hash256BS
mainArgs ["ripemd160"] = interactHex hash160BS
mainArgs ["sha256"] = interactHex hash256BS
mainArgs ["sha1"] = interactHex hashSha1BS
mainArgs ["hash256"] = interactHex $ hash256BS . hash256BS
mainArgs ["hash160"] = interactHex $ hash160BS . hash256BS
mainArgs ("ripemd160":args) = interactHex "hx ripemd160 [<HEX-INPUT>]" hash160BS args
mainArgs ("sha256":args) = interactHex "hx sha256 [<HEX-INPUT>]" hash256BS args
mainArgs ("sha1":args) = interactHex "hx sha1 [<HEX-INPUT>]" hashSha1BS args
mainArgs ("hash256":args) = interactHex "hx hash256 [<HEX-INPUT>]" doubleHash256BS args
mainArgs ("hash160":args) = interactHex "hx hash160 [<HEX-INPUT>]" (hash160BS . hash256BS) args

mainArgs ("hmac-sha224":args) = interactArgsLn (hx_hmac "sha224" SHA224.hash 64) args
mainArgs ("hmac-sha256":args) = interactArgsLn (hx_hmac "sha256" hash256BS 64) args
Expand Down Expand Up @@ -683,18 +683,18 @@ mainArgs _ = error $ unlines ["Unexpected arguments."
,"hx chksum32-decode <HEX>*"
,""
,"# HASHING"
,"hx ripemd-hash"
,"hx sha256"
,"hx ripemd160 [0]"
,"hx sha1 [0]"
,"hx hash160 [0]"
,"hx ripemd-hash [4]"
,"hx sha256 [<HEX-INPUT>]"
,"hx ripemd160 [<HEX-INPUT>] [0]"
,"hx sha1 [<HEX-INPUT>] [0]"
,"hx hash160 [<HEX-INPUT>] [0]"
,"hx hash256 [0]"
,""
,"# HASH BASED MACs"
,"hx hmac-sha224 <KEY> [<INPUT>] [0]"
,"hx hmac-sha256 <KEY> [<INPUT>] [0]"
,"hx hmac-sha384 <KEY> [<INPUT>] [0]"
,"hx hmac-sha512 <KEY> [<INPUT>] [0]"
,"hx hmac-sha224 <KEY> [<HEX-NPUT>] [0]"
,"hx hmac-sha256 <KEY> [<HEX-NPUT>] [0]"
,"hx hmac-sha384 <KEY> [<HEX-NPUT>] [0]"
,"hx hmac-sha512 <KEY> [<HEX-NPUT>] [0]"
,""
,"[0]: Not available in sx"
,"[1]: `hx showtx` is always using JSON output,"
Expand All @@ -714,6 +714,11 @@ mainArgs _ = error $ unlines ["Unexpected arguments."
,"[3]: Rounding is done upward in `hx` and downard in `sx`."
," So they agree `btc 1.4` and `btc 1.9` but on `btc 1.5`,"
," `hx` returns `0.00000002` and `sx` returns `0.00000001`."
,"[4]: The `ripemd-hash` command is taking raw-bytes as input,"
," while the other hashing commands are taking hexadecimal encoded inputs."
," This is for this reason that `hash160` has been added"
," (`hx ripemd-hash` is equivalent to `hx encode-hex | hx hash160`"
," and `hx hash160` is equivalent to `hx decode-hex | hx ripemd-hash`)."
,""
,"PATH ::= <PATH-HEAD> <PATH-CONT>"
,"PATH-HEAD ::= 'A' [address (compressed)]"
Expand Down
42 changes: 42 additions & 0 deletions tests/hash160-arg.t/TESTRECIPE
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

testname=hash160-arg.t
command=hx
args=( hash160 0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71 )
exit_code=0
stdin_file=/dev/null
stdout_file=stdout
stderr_file=/dev/null
sources=( )
products=( )

# Environment variables:
env_vars=( )

setup(){
: Perform here actions to be run before the tested program
}

munge(){
: Munge here the results of the tested program to ease the check
}

check(){
check_exit_code &&
check_stderr &&
check_stdout &&
check_products &&
: Perform here extra checks on the tested program
}

explain(){
explain_exit_code
explain_stdout
explain_stderr
explain_products
: Explain here more potential differences
}

teardown(){
: Undo here the actions of setup
}
1 change: 1 addition & 0 deletions tests/hash160-arg.t/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
79fbfc3f34e7745860d76137da68f362380c606c

0 comments on commit a542a75

Please sign in to comment.