Skip to content

Commit bd0bd95

Browse files
committed
Base32: Document the confusing arithmetic.
1 parent 8d1e39a commit bd0bd95

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

hnix-store-core/src/System/Nix/Base32.hs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ encode :: BS.ByteString -> T.Text
1212
encode c = T.pack $ concatMap char32 [nChar - 1, nChar - 2 .. 0]
1313
where
1414
digits32 = V.fromList "0123456789abcdfghijklmnpqrsvwxyz"
15-
-- The base32 encoding is 8/5's as long as the base256 digest. This `+ 1`
16-
-- `- 1` business is a bit odd, but has always been used in C++ since the
17-
-- base32 truncation was added in was first added in
18-
-- d58a11e019813902b6c4547ca61a127938b2cc20.
15+
-- Each base32 character gives us 5 bits of information, while
16+
-- each byte gives is 8. Because 'div' rounds down, we need to add
17+
-- one extra character to the result, and because of that extra 1
18+
-- we need to subtract one from the number of bits in the
19+
-- bytestring to cover for the case where the number of bits is
20+
-- already a factor of 5. Thus, the + 1 outside of the 'div' and
21+
-- the - 1 inside of it.
1922
nChar = fromIntegral $ ((BS.length c * 8 - 1) `div` 5) + 1
2023

2124
byte = BS.index c . fromIntegral

0 commit comments

Comments
 (0)