From a542a7587314f3b22f3c69ae982540ceebb345fb Mon Sep 17 00:00:00 2001 From: Nicolas Pouillard Date: Thu, 20 Nov 2014 21:06:36 +0100 Subject: [PATCH] Hash commands can now take there input as a command argument --- README.md | 21 +++++++++++++---- Utils.hs | 6 ++--- hx.hs | 33 ++++++++++++++------------ tests/hash160-arg.t/TESTRECIPE | 42 ++++++++++++++++++++++++++++++++++ tests/hash160-arg.t/stdout | 1 + 5 files changed, 81 insertions(+), 22 deletions(-) create mode 100644 tests/hash160-arg.t/TESTRECIPE create mode 100644 tests/hash160-arg.t/stdout diff --git a/README.md b/README.md index 2b160ee..a74afab 100644 --- a/README.md +++ b/README.md @@ -95,13 +95,19 @@ List of supported commands: hx chksum32-decode * # HASHING - hx ripemd-hash - hx sha256 - hx ripemd160 [0] - hx sha1 [0] - hx hash160 [0] + hx ripemd-hash [4] + hx sha256 [] + hx ripemd160 [] [0] + hx sha1 [] [0] + hx hash160 [] [0] hx hash256 [0] + # HASH BASED MACs + hx hmac-sha224 [] [0] + hx hmac-sha256 [] [0] + hx hmac-sha384 [] [0] + hx hmac-sha512 [] [0] + [0]: Not available in sx [1]: `hx showtx` is always using JSON output, `-j` and `--json` are ignored. @@ -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 ::= 'A' [address (compressed)] diff --git a/Utils.hs b/Utils.hs index e1ff4cf..527f351 100644 --- a/Utils.hs +++ b/Utils.hs @@ -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 @@ -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) diff --git a/hx.hs b/hx.hs index 7bf81d7..ae4370f 100644 --- a/hx.hs +++ b/hx.hs @@ -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 []" hash160BS args +mainArgs ("sha256":args) = interactHex "hx sha256 []" hash256BS args +mainArgs ("sha1":args) = interactHex "hx sha1 []" hashSha1BS args +mainArgs ("hash256":args) = interactHex "hx hash256 []" doubleHash256BS args +mainArgs ("hash160":args) = interactHex "hx hash160 []" (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 @@ -683,18 +683,18 @@ mainArgs _ = error $ unlines ["Unexpected arguments." ,"hx chksum32-decode *" ,"" ,"# HASHING" - ,"hx ripemd-hash" - ,"hx sha256" - ,"hx ripemd160 [0]" - ,"hx sha1 [0]" - ,"hx hash160 [0]" + ,"hx ripemd-hash [4]" + ,"hx sha256 []" + ,"hx ripemd160 [] [0]" + ,"hx sha1 [] [0]" + ,"hx hash160 [] [0]" ,"hx hash256 [0]" ,"" ,"# HASH BASED MACs" - ,"hx hmac-sha224 [] [0]" - ,"hx hmac-sha256 [] [0]" - ,"hx hmac-sha384 [] [0]" - ,"hx hmac-sha512 [] [0]" + ,"hx hmac-sha224 [] [0]" + ,"hx hmac-sha256 [] [0]" + ,"hx hmac-sha384 [] [0]" + ,"hx hmac-sha512 [] [0]" ,"" ,"[0]: Not available in sx" ,"[1]: `hx showtx` is always using JSON output," @@ -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 ::= 'A' [address (compressed)]" diff --git a/tests/hash160-arg.t/TESTRECIPE b/tests/hash160-arg.t/TESTRECIPE new file mode 100644 index 0000000..9561237 --- /dev/null +++ b/tests/hash160-arg.t/TESTRECIPE @@ -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 +} diff --git a/tests/hash160-arg.t/stdout b/tests/hash160-arg.t/stdout new file mode 100644 index 0000000..fd0e473 --- /dev/null +++ b/tests/hash160-arg.t/stdout @@ -0,0 +1 @@ +79fbfc3f34e7745860d76137da68f362380c606c