1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 10:41:16 +02:00

nix hash convert: s/--type/--algo/ + more functional tests

https://github.com/NixOS/nix/issues/8876
This commit is contained in:
Peter Kolloch 2023-11-28 11:42:52 +01:00
parent 6bbd900d4f
commit 0c2d5f7673
3 changed files with 71 additions and 10 deletions

View file

@ -40,7 +40,6 @@ enum struct HashFormat : int {
SRI SRI
}; };
struct Hash struct Hash
{ {
constexpr static size_t maxHashSize = 64; constexpr static size_t maxHashSize = 64;

View file

@ -162,9 +162,9 @@ struct CmdHashConvert : Command
}}, }},
}); });
addFlag({ addFlag({
.longName = "type", .longName = "algo",
.description = "Specify the type if it can't be auto-detected.", .description = "Specify the algorithm if it can't be auto-detected.",
.labels = {"hash type"}, .labels = {"hash algorithm"},
.handler = {[this](std::string str) { .handler = {[this](std::string str) {
type = parseHashType(str); type = parseHashType(str);
}}, }},
@ -177,7 +177,7 @@ struct CmdHashConvert : Command
std::string description() override 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; } Category category() override { return catUtility; }

View file

@ -83,11 +83,11 @@ try2 md5 "f78b733a68f5edbdf9413899339eaa4a"
# Conversion with `nix hash` `nix-hash` and `nix hash convert` # Conversion with `nix hash` `nix-hash` and `nix hash convert`
try3() { try3() {
# $1 = hash type # $1 = hash algo
# $2 = expected hash in base16 # $2 = expected hash in base16
# $3 = expected hash in base32 # $3 = expected hash in base32
# $4 = expected hash in base64 # $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" = "$4" ]
h64=$(nix-hash --type "$1" --to-base64 "$2") h64=$(nix-hash --type "$1" --to-base64 "$2")
[ "$h64" = "$4" ] [ "$h64" = "$4" ]
@ -95,13 +95,13 @@ try3() {
h64=$(nix hash to-base64 --type "$1" "$2") h64=$(nix hash to-base64 --type "$1" "$2")
[ "$h64" = "$4" ] [ "$h64" = "$4" ]
sri=$(nix hash convert --type "$1" --to sri "$2") sri=$(nix hash convert --algo "$1" --to sri "$2")
[ "$sri" = "$1-$4" ] [ "$sri" = "$1-$4" ]
sri=$(nix-hash --type "$1" --to-sri "$2") sri=$(nix-hash --type "$1" --to-sri "$2")
[ "$sri" = "$1-$4" ] [ "$sri" = "$1-$4" ]
sri=$(nix hash to-sri --type "$1" "$2") sri=$(nix hash to-sri --type "$1" "$2")
[ "$sri" = "$1-$4" ] [ "$sri" = "$1-$4" ]
h32=$(nix hash convert --type "$1" --to base32 "$2") h32=$(nix hash convert --algo "$1" --to base32 "$2")
[ "$h32" = "$3" ] [ "$h32" = "$3" ]
h32=$(nix-hash --type "$1" --to-base32 "$2") h32=$(nix-hash --type "$1" --to-base32 "$2")
[ "$h32" = "$3" ] [ "$h32" = "$3" ]
@ -110,7 +110,7 @@ try3() {
h16=$(nix-hash --type "$1" --to-base16 "$h32") h16=$(nix-hash --type "$1" --to-base16 "$h32")
[ "$h16" = "$2" ] [ "$h16" = "$2" ]
h16=$(nix hash convert --type "$1" --to base16 "$h64") h16=$(nix hash convert --algo "$1" --to base16 "$h64")
[ "$h16" = "$2" ] [ "$h16" = "$2" ]
h16=$(nix hash to-base16 --type "$1" "$h64") h16=$(nix hash to-base16 --type "$1" "$h64")
[ "$h16" = "$2" ] [ "$h16" = "$2" ]
@ -118,7 +118,69 @@ try3() {
[ "$h16" = "$2" ] [ "$h16" = "$2" ]
h16=$(nix hash to-base16 "$sri") h16=$(nix hash to-base16 "$sri")
[ "$h16" = "$2" ] [ "$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 sha1 "800d59cfcd3c05e900cb4e214be48f6b886a08df" "vw46m23bizj4n8afrc0fj19wrp7mj3c0" "gA1Zz808BekAy04hS+SPa4hqCN8="
try3 sha256 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" "1b8m03r63zqhnjf7l5wnldhh7c134ap5vpj0850ymkq1iyzicy5s" "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=" try3 sha256 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" "1b8m03r63zqhnjf7l5wnldhh7c134ap5vpj0850ymkq1iyzicy5s" "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0="
try3 sha512 "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445" "12k9jiq29iyqm03swfsgiw5mlqs173qazm3n7daz43infy12pyrcdf30fkk3qwv4yl2ick8yipc2mqnlh48xsvvxl60lbx8vp38yji0" "IEqPxt2oLwoM7XvrjgikFlfBbvRosiioJ5vjMacDwzWW/RXBOxsH+aodO+pXeJygMa2Fx6cd1wNU7GMSOMo0RQ==" try3 sha512 "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445" "12k9jiq29iyqm03swfsgiw5mlqs173qazm3n7daz43infy12pyrcdf30fkk3qwv4yl2ick8yipc2mqnlh48xsvvxl60lbx8vp38yji0" "IEqPxt2oLwoM7XvrjgikFlfBbvRosiioJ5vjMacDwzWW/RXBOxsH+aodO+pXeJygMa2Fx6cd1wNU7GMSOMo0RQ=="