Skip to content

Commit

Permalink
nix hash convert: s/--type/--algo/ + more functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kolloch committed Dec 6, 2023
1 parent 6bbd900 commit 0c2d5f7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/libutil/hash.hh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ enum struct HashFormat : int {
SRI
};


struct Hash
{
constexpr static size_t maxHashSize = 64;
Expand Down
8 changes: 4 additions & 4 deletions src/nix/hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ struct CmdHashConvert : Command
}},
});
addFlag({
.longName = "type",
.description = "Specify the type if it can't be auto-detected.",
.labels = {"hash type"},
.longName = "algo",
.description = "Specify the algorithm if it can't be auto-detected.",
.labels = {"hash algorithm"},
.handler = {[this](std::string str) {
type = parseHashType(str);
}},
Expand All @@ -177,7 +177,7 @@ struct CmdHashConvert : Command

std::string description() override
{
return "convert between different hash formats, e.g. base16 and sri.";
return "convert between different hash formats, e.g. base16, nix32, base64 and sri.";
}

Category category() override { return catUtility; }
Expand Down
72 changes: 67 additions & 5 deletions tests/functional/hash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,25 @@ try2 md5 "f78b733a68f5edbdf9413899339eaa4a"

# Conversion with `nix hash` `nix-hash` and `nix hash convert`
try3() {
# $1 = hash type
# $1 = hash algo
# $2 = expected hash in base16
# $3 = expected hash in base32
# $4 = expected hash in base64
h64=$(nix hash convert --type "$1" --to base64 "$2")
h64=$(nix hash convert --algo "$1" --to base64 "$2")
[ "$h64" = "$4" ]
h64=$(nix-hash --type "$1" --to-base64 "$2")
[ "$h64" = "$4" ]
# Deprecated experiment
h64=$(nix hash to-base64 --type "$1" "$2")
[ "$h64" = "$4" ]

sri=$(nix hash convert --type "$1" --to sri "$2")
sri=$(nix hash convert --algo "$1" --to sri "$2")
[ "$sri" = "$1-$4" ]
sri=$(nix-hash --type "$1" --to-sri "$2")
[ "$sri" = "$1-$4" ]
sri=$(nix hash to-sri --type "$1" "$2")
[ "$sri" = "$1-$4" ]
h32=$(nix hash convert --type "$1" --to base32 "$2")
h32=$(nix hash convert --algo "$1" --to base32 "$2")
[ "$h32" = "$3" ]
h32=$(nix-hash --type "$1" --to-base32 "$2")
[ "$h32" = "$3" ]
Expand All @@ -110,15 +110,77 @@ try3() {
h16=$(nix-hash --type "$1" --to-base16 "$h32")
[ "$h16" = "$2" ]

h16=$(nix hash convert --type "$1" --to base16 "$h64")
h16=$(nix hash convert --algo "$1" --to base16 "$h64")
[ "$h16" = "$2" ]
h16=$(nix hash to-base16 --type "$1" "$h64")
[ "$h16" = "$2" ]
h16=$(nix hash convert --to base16 "$sri")
[ "$h16" = "$2" ]
h16=$(nix hash to-base16 "$sri")
[ "$h16" = "$2" ]

#
# Converting from SRI
#

# Input hash algo auto-detected from SRI and output defaults to SRI as well.
sri=$(nix hash convert "$1-$4")
[ "$sri" = "$1-$4" ]

sri=$(nix hash convert --from sri "$1-$4")
[ "$sri" = "$1-$4" ]

sri=$(nix hash convert --to sri "$1-$4")
[ "$sri" = "$1-$4" ]

sri=$(nix hash convert --from sri --to sri "$1-$4")
[ "$sri" = "$1-$4" ]

sri=$(nix hash convert --to base64 "$1-$4")
[ "$sri" = "$4" ]

#
# Auto-detecting the input from algo and length.
#

sri=$(nix hash convert --algo "$1" "$2")
[ "$sri" = "$1-$4" ]
sri=$(nix hash convert --algo "$1" "$3")
[ "$sri" = "$1-$4" ]
sri=$(nix hash convert --algo "$1" "$4")
[ "$sri" = "$1-$4" ]

sri=$(nix hash convert --algo "$1" "$2")
[ "$sri" = "$1-$4" ]
sri=$(nix hash convert --algo "$1" "$3")
[ "$sri" = "$1-$4" ]
sri=$(nix hash convert --algo "$1" "$4")
[ "$sri" = "$1-$4" ]

#
# Asserting input format succeeds.
#

sri=$(nix hash convert --algo "$1" --from base16 "$2")
[ "$sri" = "$1-$4" ]
sri=$(nix hash convert --algo "$1" --from base32 "$3")
[ "$sri" = "$1-$4" ]
sri=$(nix hash convert --algo "$1" --from base64 "$4")
[ "$sri" = "$1-$4" ]

#
# Asserting input format fails.
#

fail=$(nix hash convert --algo "$1" --from base32 "$2" 2>&1 || echo "exit: $?")
[[ "$fail" == "error: input hash"*"exit: 1" ]]
fail=$(nix hash convert --algo "$1" --from base16 "$3" 2>&1 || echo "exit: $?")
[[ "$fail" == "error: input hash"*"exit: 1" ]]
fail=$(nix hash convert --algo "$1" --from base32 "$4" 2>&1 || echo "exit: $?")
[[ "$fail" == "error: input hash"*"exit: 1" ]]

}

try3 sha1 "800d59cfcd3c05e900cb4e214be48f6b886a08df" "vw46m23bizj4n8afrc0fj19wrp7mj3c0" "gA1Zz808BekAy04hS+SPa4hqCN8="
try3 sha256 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" "1b8m03r63zqhnjf7l5wnldhh7c134ap5vpj0850ymkq1iyzicy5s" "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0="
try3 sha512 "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445" "12k9jiq29iyqm03swfsgiw5mlqs173qazm3n7daz43infy12pyrcdf30fkk3qwv4yl2ick8yipc2mqnlh48xsvvxl60lbx8vp38yji0" "IEqPxt2oLwoM7XvrjgikFlfBbvRosiioJ5vjMacDwzWW/RXBOxsH+aodO+pXeJygMa2Fx6cd1wNU7GMSOMo0RQ=="

0 comments on commit 0c2d5f7

Please sign in to comment.