about summary refs log tree commit diff
path: root/src/nix-hash/nix-hash.cc
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2006-03-01T18·05+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2006-03-01T18·05+0000
commit70dee0f8ca9d7236008e161d1d1210b945ec832e (patch)
tree7fd5dd1a6bca6b91fdcac9e9db9940dbd6048aeb /src/nix-hash/nix-hash.cc
parente136532800113902432c451f0a38bf6d77e5801a (diff)
* Flags `--to-base32' and `--to-base16' to convert between hex and
  base-32 hashes.

Diffstat (limited to 'src/nix-hash/nix-hash.cc')
-rw-r--r--src/nix-hash/nix-hash.cc21
1 files changed, 19 insertions, 2 deletions
diff --git a/src/nix-hash/nix-hash.cc b/src/nix-hash/nix-hash.cc
index 4a7c943963d9..dd2ed8d4f4fa 100644
--- a/src/nix-hash/nix-hash.cc
+++ b/src/nix-hash/nix-hash.cc
@@ -17,7 +17,10 @@ void run(Strings args)
     bool flat = false;
     bool base32 = false;
     bool truncate = false;
-    
+    enum { opHash, opTo32, opTo16 } op = opHash;
+
+    Strings ss;
+
     for (Strings::iterator i = args.begin();
          i != args.end(); i++)
     {
@@ -31,13 +34,27 @@ void run(Strings args)
             if (ht == htUnknown)
                 throw UsageError(format("unknown hash type `%1%'") % *i);
         }
-        else {
+        else if (*i == "--to-base16") op = opTo16;
+        else if (*i == "--to-base32") op = opTo32;
+        else ss.push_back(*i);
+    }
+
+    if (op == opHash) {
+        for (Strings::iterator i = ss.begin(); i != ss.end(); ++i) {
             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));
         }
     }
+
+    else {
+        for (Strings::iterator i = ss.begin(); i != ss.end(); ++i) {
+            Hash h = op == opTo16 ? parseHash32(ht, *i) : parseHash(ht, *i);
+            cout << format("%1%\n") %
+                (op == opTo16 ? printHash(h) : printHash32(h));
+        }
+    }
 }