Skip to content

Commit 8cc6595

Browse files
authored
Merge pull request #31 from haskell-nix/je-printHashBytes32-bit-fiddling
Weird bit fiddling to make `printHashBytes32` work
2 parents 3fde7c1 + ecf4d99 commit 8cc6595

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

hnix-store-core/src/System/Nix/Internal/Hash.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,11 @@ newtype Digest (a :: HashAlgorithm) = Digest
150150
printHashBytes32 :: BS.ByteString -> T.Text
151151
printHashBytes32 c = T.pack $ concatMap char32 [nChar - 1, nChar - 2 .. 0]
152152
where
153-
-- The base32 encoding is 8/5's as long as the base256 digest
154-
nChar = fromIntegral $ BS.length c * 8 `div` 5
153+
-- The base32 encoding is 8/5's as long as the base256 digest. This `+ 1`
154+
-- `- 1` business is a bit odd, but has always been used in C++ since the
155+
-- base32 truncation was added in was first added in
156+
-- d58a11e019813902b6c4547ca61a127938b2cc20.
157+
nChar = fromIntegral $ ((BS.length c * 8 - 1) `div` 5) + 1
155158

156159
char32 :: Integer -> [Char]
157160
char32 i = [digits32 V.! digitInd]

hnix-store-core/tests/Hash.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ spec_hash = do
3131

3232
describe "hashing parity with nix-store" $ do
3333

34+
it "produces (base32 . sha256) of \"nix-output:foo\" the same as Nix does at the moment for placeholder \"foo\"" $
35+
shouldBe (printAsBase32 (hash @SHA256 "nix-output:foo"))
36+
"1x0ymrsy7yr7i9wdsqy9khmzc1yy7nvxw6rdp72yzn50285s67j5"
37+
3438
it "produces (base32 . sha1) of \"Hello World\" the same as the thesis" $
3539
shouldBe (printAsBase32 (hash @SHA1 "Hello World"))
3640
"s23c9fs0v32pf6bhmcph5rbqsyl5ak8a"

0 commit comments

Comments
 (0)