about summary refs log tree commit diff
path: root/src/nix-hash
diff options
context:
space:
mode:
Diffstat (limited to 'src/nix-hash')
-rw-r--r--src/nix-hash/help.txt2
-rw-r--r--src/nix-hash/nix-hash.cc17
2 files changed, 12 insertions, 7 deletions
diff --git a/src/nix-hash/help.txt b/src/nix-hash/help.txt
index a38c2ab9ed..31fff1ffab 100644
--- a/src/nix-hash/help.txt
+++ b/src/nix-hash/help.txt
@@ -4,3 +4,5 @@ nix-hash [OPTIONS...] [FILES...]
 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")
diff --git a/src/nix-hash/nix-hash.cc b/src/nix-hash/nix-hash.cc
index 2cef7818e8..78c6f44012 100644
--- a/src/nix-hash/nix-hash.cc
+++ b/src/nix-hash/nix-hash.cc
@@ -15,22 +15,25 @@ void run(Strings args)
 {
     HashType ht = htMD5;
     bool flat = false;
+    bool base32 = 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 == "--type") {
             ++i;
             if (i == args.end()) throw UsageError("`--type' requires an argument");
-            if (*i == "md5") ht = htMD5;
-            else if (*i == "sha1") ht = htSHA1;
-            else if (*i == "sha256") ht = htSHA256;
-            else throw UsageError(format("unknown hash type `%1%'") % *i);
+            ht = parseHashType(*i);
+            if (ht == htUnknown)
+                throw UsageError(format("unknown hash type `%1%'") % *i);
+        }
+        else {
+            Hash h = flat ? hashFile(ht, *i) : hashPath(ht, *i);
+            cout << format("%1%\n") %
+                (base32 ? printHash32(h) : printHash(h));
         }
-        else
-            cout << format("%1%\n") % printHash(
-                (flat ? hashFile(*i, ht) : hashPath(*i, ht)));
     }
 }