File tree Expand file tree Collapse file tree 5 files changed +16
-17
lines changed Expand file tree Collapse file tree 5 files changed +16
-17
lines changed Original file line number Diff line number Diff 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
1617import qualified System.Nix.Internal.Hash as HNix
Original file line number Diff line number Diff line change @@ -90,16 +90,13 @@ hashLazy :: forall a.HasDigest a => BSL.ByteString -> Digest a
9090hashLazy 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
105102instance HasDigest MD5 where
Original file line number Diff line number Diff line change @@ -63,7 +63,7 @@ data Path = Path !(Digest PathHashAlgo) !PathName
6363 deriving (Eq , Ord , Show )
6464
6565pathToText :: 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
6868type PathSet = HashSet Path
6969
Original file line number Diff line number Diff line change @@ -10,19 +10,20 @@ import qualified Data.HashSet as HS
1010import Data.Text (Text )
1111import qualified Data.Text as T
1212import Data.Text.Encoding
13- import System.Nix.Internal. Hash
13+ import System.Nix.Hash
1414import System.Nix.Path
1515
1616makeStorePath :: Text -> Text -> Digest 'SHA256 -> Text -> Path
1717makeStorePath 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
2728makeTextPath :: Text -> Text -> Digest 'SHA256 -> PathSet -> Path
2829makeTextPath storeDir nm h refs = makeStorePath storeDir ty h nm
Original file line number Diff line number Diff 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"
You can’t perform that action at this time.
0 commit comments