about summary refs log tree commit diff
path: root/src/libutil/hash.cc
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2011-12-02T11·47+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2011-12-02T11·47+0000
commit49f59dceca37636353cf2f5f60135d7705ea154e (patch)
treeb6b26acd615b96ed0f53089941391d6a5cad864b /src/libutil/hash.cc
parentb12b21825c949ef9b9327c6a0c9e2d5601aaf0b2 (diff)
* Move parseHash16or32 into libutil, and use in nix-hash.
Diffstat (limited to 'src/libutil/hash.cc')
-rw-r--r--src/libutil/hash.cc16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc
index b9e7846992f6..53342344107d 100644
--- a/src/libutil/hash.cc
+++ b/src/libutil/hash.cc
@@ -204,6 +204,22 @@ Hash parseHash32(HashType ht, const string & s)
 }
 
 
+Hash parseHash16or32(HashType ht, const string & s)
+{
+    Hash hash(ht);
+    if (s.size() == hash.hashSize * 2)
+        /* hexadecimal representation */
+        hash = parseHash(ht, s);
+    else if (s.size() == hashLength32(hash))
+        /* base-32 representation */
+        hash = parseHash32(ht, s);
+    else
+        throw Error(format("hash `%1%' has wrong length for hash type `%2%'")
+            % s % printHashType(ht));
+    return hash;
+}
+
+
 bool isHash(const string & s)
 {
     if (s.length() != 32) return false;