Skip to content

Commit 58d7630

Browse files
committed
Core: Internal: mv Base enc -> Base, Hash.encode(InBase->DigestWith)
1 parent 79a042d commit 58d7630

File tree

6 files changed

+38
-25
lines changed

6 files changed

+38
-25
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module System.Nix.Hash
1414
, Hash.mkNamedDigest
1515

1616
, Base.BaseEncoding(..)
17-
, Hash.encodeInBase
17+
, Hash.encodeDigestWith
1818
, Hash.decodeBase
1919
)
2020
where
Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
module System.Nix.Internal.Base
22
( module System.Nix.Internal.Base
3-
, encode
4-
, decode
3+
, Base32.encode
4+
, Base32.decode
55
)
66
where
77

8-
import System.Nix.Internal.Base32
8+
import qualified Data.Text as T
9+
import qualified Data.Text.Encoding as T
10+
import qualified Data.ByteString as Bytes
11+
import qualified Data.ByteString.Base16 as Base16
12+
import qualified System.Nix.Base32 as Base32 -- Nix has own Base32 encoding
13+
import qualified Data.ByteString.Base64 as Base64
914

1015
-- | Constructors to indicate the base encodings
1116
data BaseEncoding
12-
= Base16
13-
| NixBase32
17+
= NixBase32
1418
-- | ^ Nix has a special map of Base32 encoding
19+
-- Placed first, since it determines Haskell optimizations of pattern matches, & NixBase seems be the most widely used in Nix.
20+
| Base16
1521
| Base64
22+
23+
24+
-- | Encode @ByteString@ with @Base@ encoding, produce @Text@.
25+
encodeWith :: BaseEncoding -> Bytes.ByteString -> T.Text
26+
encodeWith Base16 = T.decodeUtf8 . Base16.encode
27+
encodeWith NixBase32 = Base32.encode
28+
encodeWith Base64 = T.decodeUtf8 . Base64.encode

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ import qualified GHC.TypeLits as Kind
3535
(Nat, KnownNat, natVal)
3636
import Data.Coerce (coerce)
3737
import System.Nix.Internal.Base
38-
(BaseEncoding(Base16,NixBase32,Base64))
38+
( BaseEncoding(Base16,NixBase32,Base64)
39+
, encodeWith
40+
)
3941

4042
-- | The universe of supported hash algorithms.
4143
--
@@ -55,7 +57,7 @@ newtype Digest (a :: HashAlgorithm) =
5557
Digest BS.ByteString deriving (Eq, Ord, DataHashable.Hashable)
5658

5759
instance Show (Digest a) where
58-
show = ("Digest " <>) . show . encodeInBase NixBase32
60+
show = ("Digest " <>) . show . encodeDigestWith NixBase32
5961

6062
-- | The primitive interface for incremental hashing for a given
6163
-- 'HashAlgorithm'. Every 'HashAlgorithm' should have an instance.
@@ -97,7 +99,7 @@ data SomeNamedDigest = forall a . NamedAlgo a => SomeDigest (Digest a)
9799

98100
instance Show SomeNamedDigest where
99101
show sd = case sd of
100-
SomeDigest (digest :: Digest hashType) -> T.unpack $ "SomeDigest " <> algoName @hashType <> ":" <> encodeInBase NixBase32 digest
102+
SomeDigest (digest :: Digest hashType) -> T.unpack $ "SomeDigest " <> algoName @hashType <> ":" <> encodeDigestWith NixBase32 digest
101103

102104
mkNamedDigest :: Text -> Text -> Either String SomeNamedDigest
103105
mkNamedDigest name sriHash =
@@ -147,10 +149,8 @@ hashLazy bsl =
147149

148150

149151
-- | Take BaseEncoding type of the output -> take the Digeest as input -> encode Digest
150-
encodeInBase :: BaseEncoding -> Digest a -> T.Text
151-
encodeInBase Base16 = T.decodeUtf8 . Base16.encode . coerce
152-
encodeInBase NixBase32 = Base32.encode . coerce
153-
encodeInBase Base64 = T.decodeUtf8 . Base64.encode . coerce
152+
encodeDigestWith :: BaseEncoding -> Digest a -> T.Text
153+
encodeDigestWith b = encodeWith b . coerce
154154

155155

156156
-- | Take BaseEncoding type of the input -> take the input itself -> decodeBase into Digest

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import System.Nix.Hash ( HashAlgorithm
1616
)
1717
, Digest
1818
, BaseEncoding(..)
19-
, encodeInBase
19+
, encodeDigestWith
2020
, decodeBase
2121
, SomeNamedDigest
2222
)
@@ -154,7 +154,7 @@ storePathToRawFilePath StorePath{..} =
154154
root <> "/" <> hashPart <> "-" <> name
155155
where
156156
root = Bytes.Char8.pack storePathRoot
157-
hashPart = Text.encodeUtf8 $ encodeInBase NixBase32 storePathHash
157+
hashPart = Text.encodeUtf8 $ encodeDigestWith NixBase32 storePathHash
158158
name = Text.encodeUtf8 $ unStorePathName storePathName
159159

160160
-- | Render a 'StorePath' as a 'FilePath'.
@@ -169,7 +169,7 @@ storePathToText = Text.pack . Bytes.Char8.unpack . storePathToRawFilePath
169169
-- can be used to query binary caches.
170170
storePathToNarInfo :: StorePath -> Bytes.Char8.ByteString
171171
storePathToNarInfo StorePath{..} =
172-
Text.encodeUtf8 $ encodeInBase NixBase32 storePathHash <> ".narinfo"
172+
Text.encodeUtf8 $ encodeDigestWith NixBase32 storePathHash <> ".narinfo"
173173

174174
-- | Parse `StorePath` from `Bytes.Char8.ByteString`, checking
175175
-- that store directory matches `expectedRoot`.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ makeStorePath fp ty h nm = StorePath storeHash nm fp
3434
BS.intercalate ":" $
3535
ty:fmap encodeUtf8
3636
[ algoName @hashAlgo
37-
, encodeInBase Base16 h
37+
, encodeDigestWith Base16 h
3838
, T.pack fp
3939
, unStorePathName nm
4040
]
@@ -64,7 +64,7 @@ makeFixedOutputPath fp recursive h =
6464
$ "fixed:out:"
6565
<> encodeUtf8 (algoName @hashAlgo)
6666
<> (if recursive then ":r:" else ":")
67-
<> encodeUtf8 (encodeInBase Base16 h)
67+
<> encodeUtf8 (encodeDigestWith Base16 h)
6868
<> ":"
6969

7070
computeStorePathForText

hnix-store-core/tests/Hash.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ spec_hash = do
2727
describe "hashing parity with nix-store" $ do
2828

2929
it "produces (base32 . sha256) of \"nix-output:foo\" the same as Nix does at the moment for placeholder \"foo\"" $
30-
shouldBe (encodeInBase NixBase32 (hash @'SHA256 "nix-output:foo"))
30+
shouldBe (encodeDigestWith NixBase32 (hash @'SHA256 "nix-output:foo"))
3131
"1x0ymrsy7yr7i9wdsqy9khmzc1yy7nvxw6rdp72yzn50285s67j5"
3232
it "produces (base16 . md5) of \"Hello World\" the same as the thesis" $
33-
shouldBe (encodeInBase Base16 (hash @'MD5 "Hello World"))
33+
shouldBe (encodeDigestWith Base16 (hash @'MD5 "Hello World"))
3434
"b10a8db164e0754105b7a99be72e3fe5"
3535
it "produces (base32 . sha1) of \"Hello World\" the same as the thesis" $
36-
shouldBe (encodeInBase NixBase32 (hash @'SHA1 "Hello World"))
36+
shouldBe (encodeDigestWith NixBase32 (hash @'SHA1 "Hello World"))
3737
"s23c9fs0v32pf6bhmcph5rbqsyl5ak8a"
3838

3939
-- The example in question:
@@ -42,11 +42,11 @@ spec_hash = do
4242
let exampleStr =
4343
"source:sha256:2bfef67de873c54551d884fdab3055d84d573e654efa79db3"
4444
<> "c0d7b98883f9ee3:/nix/store:myfile"
45-
shouldBe (encodeInBase32 @StorePathHashAlgo (hash exampleStr))
45+
shouldBe (encodeDigestWith32 @StorePathHashAlgo (hash exampleStr))
4646
"xv2iccirbrvklck36f1g7vldn5v58vck"
4747
where
48-
encodeInBase32 :: Digest a -> Text
49-
encodeInBase32 = encodeInBase NixBase32
48+
encodeDigestWith32 :: Digest a -> Text
49+
encodeDigestWith32 = encodeDigestWith NixBase32
5050

5151
-- | Test that Nix-like base32 encoding roundtrips
5252
prop_nixBase32Roundtrip :: Property
@@ -55,7 +55,7 @@ prop_nixBase32Roundtrip = forAllShrink nonEmptyString genericShrink $
5555

5656
-- | API variants
5757
prop_nixBase16Roundtrip :: Digest StorePathHashAlgo -> Property
58-
prop_nixBase16Roundtrip x = pure x === (decodeBase Base16 . encodeInBase Base16 $ x)
58+
prop_nixBase16Roundtrip x = pure x === (decodeBase Base16 . encodeDigestWith Base16 $ x)
5959

6060
-- | Hash encoding conversion ground-truth.
6161
-- Similiar to nix/tests/hash.sh

0 commit comments

Comments
 (0)