Skip to content

Commit

Permalink
nix hash convert: Don't fail on uppercase base-16 hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Dec 5, 2024
1 parent 52f1cd0 commit 33b645c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/nix/hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,18 @@ struct CmdHashConvert : Command
Category category() override { return catUtility; }

void run() override {
for (const auto & s: hashStrings) {
for (const auto & s : hashStrings) {
Hash h =
from == HashFormat::SRI
? Hash::parseSRI(s)
: Hash::parseAny(s, algo);
if (from && from != HashFormat::SRI && h.to_string(*from, false) != s) {
if (from
&& from != HashFormat::SRI
&& h.to_string(*from, false) !=
(from == HashFormat::Base16 ? toLower(s) : s))
{
auto from_as_string = printHashFormat(*from);
throw BadHash("input hash '%s' does not have the expected format '--from %s'", s, from_as_string);
throw BadHash("input hash '%s' does not have the expected format for '--from %s'", s, from_as_string);
}
logger->cout(h.to_string(to, to == HashFormat::SRI));
}
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/hash-convert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ try3() {
expectStderr 1 nix hash convert --hash-algo "$1" --from base16 "$3" | grepQuiet "input hash"
expectStderr 1 nix hash convert --hash-algo "$1" --from nix32 "$4" | grepQuiet "input hash"

# Base-16 hashes can be in uppercase.
nix hash convert --hash-algo "$1" --from base16 "$(echo $2 | tr [a-z] [A-Z])"
}

try3 sha1 "800d59cfcd3c05e900cb4e214be48f6b886a08df" "vw46m23bizj4n8afrc0fj19wrp7mj3c0" "gA1Zz808BekAy04hS+SPa4hqCN8="
Expand Down

0 comments on commit 33b645c

Please sign in to comment.