From 4c34d384e68ce7e2c949a7588d80bbe7d5a96440 Mon Sep 17 00:00:00 2001
From: Eelco Dolstra <eelco.dolstra@logicblox.com>
Date: Tue, 23 Oct 2012 18:05:50 +0200
Subject: [PATCH] If hashes do not match, print them in base-32 for
 SHA-1/SHA-256

Fixes #57.
---
 src/libstore/build.cc | 2 +-
 src/libutil/hash.cc   | 6 ++++++
 src/libutil/hash.hh   | 3 +++
 3 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/libstore/build.cc b/src/libstore/build.cc
index 61abd2ed2..3a06aa1fc 100644
--- a/src/libstore/build.cc
+++ b/src/libstore/build.cc
@@ -2228,7 +2228,7 @@ void DerivationGoal::computeClosure()
             if (h != h2)
                 throw BuildError(
                     format("output path `%1%' should have %2% hash `%3%', instead has `%4%'")
-                    % path % i->second.hashAlgo % printHash(h) % printHash(h2));
+                    % path % i->second.hashAlgo % printHash16or32(h) % printHash16or32(h2));
         }
 
         /* Get rid of all weird permissions. */
diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc
index 697a6b475..de2c1ebd7 100644
--- a/src/libutil/hash.cc
+++ b/src/libutil/hash.cc
@@ -153,6 +153,12 @@ string printHash32(const Hash & hash)
 }
 
 
+string printHash16or32(const Hash & hash)
+{
+    return hash.type == htMD5 ? printHash(hash) : printHash32(hash);
+}
+
+
 static bool mul(unsigned char * bytes, unsigned char y, int maxSize)
 {
     unsigned char carry = 0;
diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh
index 781f51742..2c6f176ec 100644
--- a/src/libutil/hash.hh
+++ b/src/libutil/hash.hh
@@ -54,6 +54,9 @@ unsigned int hashLength32(const Hash & hash);
 /* Convert a hash to a base-32 representation. */
 string printHash32(const Hash & hash);
 
+/* Print a hash in base-16 if it's MD5, or base-32 otherwise. */
+string printHash16or32(const Hash & hash);
+
 /* Parse a base-32 representation of a hash code. */
 Hash parseHash32(HashType ht, const string & s);
 
-- 
GitLab