From 039936567539c1d6d7a3b824b94726b3fed3343b Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Sun, 14 Aug 2005 10:09:56 +0000 Subject: [PATCH] * nix-hash: option `--truncate' to truncate the hash to 160 bits. Hmm, kind of ad hoc ;-) --- src/nix-hash/help.txt | 1 + src/nix-hash/nix-hash.cc | 3 +++ 2 files changed, 4 insertions(+) diff --git a/src/nix-hash/help.txt b/src/nix-hash/help.txt index 31fff1ffab..082cc7571c 100644 --- a/src/nix-hash/help.txt +++ b/src/nix-hash/help.txt @@ -6,3 +6,4 @@ files. --flat: compute hash of regular file contents, not metadata --base32: print hash in base-32 instead of hexadecimal --type HASH: use hash algorithm HASH ("md5" (default), "sha1", "sha256") + --truncate: truncate the hash to 160 bits diff --git a/src/nix-hash/nix-hash.cc b/src/nix-hash/nix-hash.cc index 78c6f44012..4a7c943963 100644 --- a/src/nix-hash/nix-hash.cc +++ b/src/nix-hash/nix-hash.cc @@ -16,12 +16,14 @@ void run(Strings args) HashType ht = htMD5; bool flat = false; bool base32 = false; + bool truncate = false; for (Strings::iterator i = args.begin(); i != args.end(); i++) { if (*i == "--flat") flat = true; else if (*i == "--base32") base32 = true; + else if (*i == "--truncate") truncate = true; else if (*i == "--type") { ++i; if (i == args.end()) throw UsageError("`--type' requires an argument"); @@ -31,6 +33,7 @@ void run(Strings args) } else { Hash h = flat ? hashFile(ht, *i) : hashPath(ht, *i); + if (truncate && h.hashSize > 20) h = compressHash(h, 20); cout << format("%1%\n") % (base32 ? printHash32(h) : printHash(h)); }