Skip to content

Commit

Permalink
nix-hash: support base-64 and SRI format
Browse files Browse the repository at this point in the history
  • Loading branch information
ShamrockLee committed Mar 13, 2023
1 parent 9f7cab4 commit a34d173
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
32 changes: 28 additions & 4 deletions doc/manual/src/command-ref/nix-hash.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

`nix-hash` [`--flat`] [`--base32`] [`--truncate`] [`--type` *hashAlgo*] *path…*

`nix-hash` `--to-base16` *hash…*

`nix-hash` `--to-base32` *hash…*
`nix-hash` [`--to-base16`|`--to-base32`|`--to-base64`|`--to-sri`] [`--type` *hashAlgo*] *hash…*

# Description

Expand Down Expand Up @@ -40,6 +38,15 @@ md5sum`.
This base-32 representation is more compact and can be used in Nix
expressions (such as in calls to `fetchurl`).

- `--base64`\
Similar to --base32, but print the hash in a base-64 representation,
which is more compact than the base-32 one.

- `--sri`\
Print the hash in SRI format with base-64 encoding.
The type of hash algorithm will be prepended to the hash string,
followed by a hyphen (-) and the base-64 hash body.

- `--truncate`\
Truncate hashes longer than 160 bits (such as SHA-256) to 160 bits.

Expand All @@ -55,6 +62,14 @@ md5sum`.
Don’t hash anything, but convert the hexadecimal hash representation
*hash* to base-32.

- `--to-base64`\
Don’t hash anything, but convert the hexadecimal hash representation
*hash* to base-64.

- `--to-sri`\
Don’t hash anything, but convert the hexadecimal hash representation
*hash* to SRI.

# Examples

Computing the same hash as `nix-prefetch-url`:
Expand Down Expand Up @@ -91,12 +106,21 @@ $ nix-hash --type sha256 --flat test/world
5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03
```

Converting between hexadecimal and base-32:
Converting between hexadecimal, base-32, base-64, and SRI:

```console
$ nix-hash --type sha1 --to-base32 e4fd8ba5f7bbeaea5ace89fe10255536cd60dab6
nvd61k9nalji1zl9rrdfmsmvyyjqpzg4

$ nix-hash --type sha1 --to-base16 nvd61k9nalji1zl9rrdfmsmvyyjqpzg4
e4fd8ba5f7bbeaea5ace89fe10255536cd60dab6

$ nix-hash --type sha1 --to-base64 e4fd8ba5f7bbeaea5ace89fe10255536cd60dab6
5P2Lpfe76upazon+ECVVNs1g2rY=

$ nix-hash --type sha1 --to-sri nvd61k9nalji1zl9rrdfmsmvyyjqpzg4
sha1-5P2Lpfe76upazon+ECVVNs1g2rY=

$ nix-hash --to-base16 sha1-5P2Lpfe76upazon+ECVVNs1g2rY=
e4fd8ba5f7bbeaea5ace89fe10255536cd60dab6
```
4 changes: 4 additions & 0 deletions doc/manual/src/release-notes/rl-next.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@
discovered by making multiple syscalls. This change makes these operations
lazy such that these lookups will only be performed if the attribute is used.
This optimization affects a minority of filesystems and operating systems.

* The `nix-hash` command now supports Base64 and SRI. Use the flags `--base64`
or `--sri` to specify the format of output hash as Base64 or SRI, and `--to-base64`
or `--to-sri` to convert a hash to Base64 or SRI format, respectively.
37 changes: 27 additions & 10 deletions src/nix/hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ static auto rCmdHash = registerCommand<CmdHash>("hash");
/* Legacy nix-hash command. */
static int compatNixHash(int argc, char * * argv)
{
HashType ht = htMD5;
std::optional<HashType> ht;
bool flat = false;
bool base32 = false;
Base base = Base16;
bool truncate = false;
enum { opHash, opTo32, opTo16 } op = opHash;
enum { opHash, opTo } op = opHash;
std::vector<std::string> ss;

parseCmdLine(argc, argv, [&](Strings::iterator & arg, const Strings::iterator & end) {
Expand All @@ -174,14 +174,30 @@ static int compatNixHash(int argc, char * * argv)
else if (*arg == "--version")
printVersion("nix-hash");
else if (*arg == "--flat") flat = true;
else if (*arg == "--base32") base32 = true;
else if (*arg == "--base32") base = Base32;
else if (*arg == "--base64") base = Base64;
else if (*arg == "--sri") base = SRI;
else if (*arg == "--truncate") truncate = true;
else if (*arg == "--type") {
std::string s = getArg(*arg, arg, end);
ht = parseHashType(s);
}
else if (*arg == "--to-base16") op = opTo16;
else if (*arg == "--to-base32") op = opTo32;
else if (*arg == "--to-base16") {
op = opTo;
base = Base16;
}
else if (*arg == "--to-base32") {
op = opTo;
base = Base32;
}
else if (*arg == "--to-base64") {
op = opTo;
base = Base64;
}
else if (*arg == "--to-sri") {
op = opTo;
base = SRI;
}
else if (*arg != "" && arg->at(0) == '-')
return false;
else
Expand All @@ -191,17 +207,18 @@ static int compatNixHash(int argc, char * * argv)

if (op == opHash) {
CmdHashBase cmd(flat ? FileIngestionMethod::Flat : FileIngestionMethod::Recursive);
cmd.ht = ht;
cmd.base = base32 ? Base32 : Base16;
if (!ht.has_value()) ht = htMD5;
cmd.ht = ht.value();
cmd.base = base;
cmd.truncate = truncate;
cmd.paths = ss;
cmd.run();
}

else {
CmdToBase cmd(op == opTo32 ? Base32 : Base16);
CmdToBase cmd(base);
cmd.args = ss;
cmd.ht = ht;
if (ht.has_value()) cmd.ht = ht;
cmd.run();
}

Expand Down
11 changes: 10 additions & 1 deletion tests/hash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ source common.sh

try () {
printf "%s" "$2" > $TEST_ROOT/vector
hash=$(nix-hash file "--${FORMAT:-base16}" --type "$1" "$TEST_ROOT/vector")
if test "$hash" != "$3"; then
echo "try nix-hash: hash $1, expected $3, got $hash"
exit 1
fi
hash=$(nix hash file "${FORMAT:-base16}" --type "$1" "$TEST_ROOT/vector")
if test "$hash" != "$3"; then
echo "hash $1, expected $3, got $hash"
echo "try nix hash: hash $1, expected $3, got $hash"
exit 1
fi
}
Expand Down Expand Up @@ -70,8 +75,12 @@ try2 md5 "f78b733a68f5edbdf9413899339eaa4a"

# Conversion.
try3() {
h64=$(nix-hash --type "$1" --to-base64 "$2")
[ "$h64" = "$4" ]
h64=$(nix hash to-base64 --type "$1" "$2")
[ "$h64" = "$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 --type "$1" --to-base32 "$2")
Expand Down

0 comments on commit a34d173

Please sign in to comment.