Skip to content

Commit 71025ed

Browse files
committed
Hash digests: Only export primitives for rendering as hex and base32
The old 'digestText16' was really a special case that doesn't belong in the hashing module proper.
1 parent 3ad41dc commit 71025ed

File tree

5 files changed

+16
-17
lines changed

5 files changed

+16
-17
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ module System.Nix.Hash (
1010
, HNix.hash
1111
, HNix.hashLazy
1212

13-
, HNix.printAsBase32
13+
, HNix.encodeBase32
14+
, HNix.encodeBase16
1415
) where
1516

1617
import qualified System.Nix.Internal.Hash as HNix

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,13 @@ hashLazy :: forall a.HasDigest a => BSL.ByteString -> Digest a
9090
hashLazy bsl =
9191
finalize $ foldl' (update @a) (initialize @a) (BSL.toChunks bsl)
9292

93-
digestText32 :: forall a. NamedAlgo a => Digest a -> T.Text
94-
digestText32 d = algoName @a <> ":" <> printAsBase32 d
93+
-- | Encode a Digest in the special Nix base-32 encoding.
94+
encodeBase32 :: Digest a -> T.Text
95+
encodeBase32 (Digest bs) = printHashBytes32 bs
9596

96-
digestText16 :: forall a. NamedAlgo a => Digest a -> T.Text
97-
digestText16 (Digest bs) = algoName @a <> ":" <> T.decodeUtf8 (Base16.encode bs)
98-
99-
-- | Convert any Digest to a base32-encoded string.
100-
-- This is not used in producing store path hashes
101-
printAsBase32 :: Digest a -> T.Text
102-
printAsBase32 (Digest bs) = printHashBytes32 bs
97+
-- | Encode a Digest in hex.
98+
encodeBase16 :: Digest a -> T.Text
99+
encodeBase16 (Digest bs) = T.decodeUtf8 (Base16.encode bs)
103100

104101

105102
instance HasDigest MD5 where

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ data Path = Path !(Digest PathHashAlgo) !PathName
6363
deriving (Eq, Ord, Show)
6464

6565
pathToText :: Text -> Path -> Text
66-
pathToText storeDir (Path h nm) = storeDir <> "/" <> printAsBase32 h <> "-" <> pathNameContents nm
66+
pathToText storeDir (Path h nm) = storeDir <> "/" <> encodeBase32 h <> "-" <> pathNameContents nm
6767

6868
type PathSet = HashSet Path
6969

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@ import qualified Data.HashSet as HS
1010
import Data.Text (Text)
1111
import qualified Data.Text as T
1212
import Data.Text.Encoding
13-
import System.Nix.Internal.Hash
13+
import System.Nix.Hash
1414
import System.Nix.Path
1515

1616
makeStorePath :: Text -> Text -> Digest 'SHA256 -> Text -> Path
1717
makeStorePath storeDir ty h nm = Path storeHash (PathName nm)
1818
where
1919
s = T.intercalate ":"
2020
[ ty
21-
, digestText16 h
21+
, algoName @'SHA256
22+
, encodeBase16 h
2223
, storeDir
2324
, nm
2425
]
25-
storeHash = truncateDigest $ hash $ encodeUtf8 s
26+
storeHash = hash $ encodeUtf8 s
2627

2728
makeTextPath :: Text -> Text -> Digest 'SHA256 -> PathSet -> Path
2829
makeTextPath storeDir nm h refs = makeStorePath storeDir ty h nm

hnix-store-core/tests/Hash.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ spec_hash = do
3232
describe "hashing parity with nix-store" $ do
3333

3434
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"))
35+
shouldBe (encodeBase32 (hash @SHA256 "nix-output:foo"))
3636
"1x0ymrsy7yr7i9wdsqy9khmzc1yy7nvxw6rdp72yzn50285s67j5"
3737

3838
it "produces (base32 . sha1) of \"Hello World\" the same as the thesis" $
39-
shouldBe (printAsBase32 (hash @SHA1 "Hello World"))
39+
shouldBe (encodeBase32 (hash @SHA1 "Hello World"))
4040
"s23c9fs0v32pf6bhmcph5rbqsyl5ak8a"
4141

4242
-- The example in question:
@@ -45,5 +45,5 @@ spec_hash = do
4545
let exampleStr =
4646
"source:sha256:2bfef67de873c54551d884fdab3055d84d573e654efa79db3"
4747
<> "c0d7b98883f9ee3:/nix/store:myfile"
48-
shouldBe (printAsBase32 @PathHashAlgo (hash exampleStr))
48+
shouldBe (encodeBase32 @PathHashAlgo (hash exampleStr))
4949
"xv2iccirbrvklck36f1g7vldn5v58vck"

0 commit comments

Comments
 (0)