Skip to content

Commit 3ad41dc

Browse files
committed
Remove value-level hash algorithms.
We can use NamedHashAlgorithm to get a name where relevant, and soon the pair of (recursive, Proxy) :: (Bool, Proxy algo) will be replaced with a separate type which will let us drop the proxies. Also, we drop logic from addToStore in the remote impl that was copied from C++ compatibility code for very old daemon versions.
1 parent 20e7b03 commit 3ad41dc

File tree

5 files changed

+11
-40
lines changed

5 files changed

+11
-40
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ Description : Cryptographic hashes for hnix-store.
44
module System.Nix.Hash (
55
HNix.Digest
66

7-
, HNix.HashAlgorithm
8-
, HNix.HashAlgorithm'(..)
9-
, HNix.AlgoVal(..)
7+
, HNix.HashAlgorithm(..)
108
, HNix.HasDigest(..)
9+
, HNix.NamedAlgo(..)
1110
, HNix.hash
1211
, HNix.hashLazy
1312

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

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,11 @@ import GHC.TypeLits
3939
-- | A tag for different hashing algorithms
4040
-- Also used as a type-level tag for hash digests
4141
-- (e.g. @Digest SHA256@ is the type for a sha256 hash)
42-
--
43-
-- When used at the type level, `n` is `Nat`
44-
data HashAlgorithm' n
42+
data HashAlgorithm
4543
= MD5
4644
| SHA1
4745
| SHA256
48-
| Truncated n (HashAlgorithm' n)
49-
deriving (Eq, Show)
46+
| Truncated Nat HashAlgorithm
5047

5148
class NamedAlgo a where
5249
algoName :: Text
@@ -60,8 +57,6 @@ instance NamedAlgo 'SHA1 where
6057
instance NamedAlgo 'SHA256 where
6158
algoName = "sha256"
6259

63-
type HashAlgorithm = HashAlgorithm' Nat
64-
6560
-- | Types with kind @HashAlgorithm@ may be a @HasDigest@ instance
6661
-- if they are able to hash bytestrings via the init/update/finalize
6762
-- API of cryptonite
@@ -191,20 +186,3 @@ truncateDigest (Digest c) = Digest $ BS.pack $ map truncOutputByte [0.. n-1]
191186

192187
digits32 :: V.Vector Char
193188
digits32 = V.fromList "0123456789abcdfghijklmnpqrsvwxyz"
194-
195-
196-
-- | Convert type-level @HashAlgorithm@ into the value level
197-
class AlgoVal (a :: HashAlgorithm) where
198-
algoVal :: HashAlgorithm' Integer
199-
200-
instance AlgoVal MD5 where
201-
algoVal = MD5
202-
203-
instance AlgoVal SHA1 where
204-
algoVal = SHA1
205-
206-
instance AlgoVal SHA256 where
207-
algoVal = SHA256
208-
209-
instance forall a n.(AlgoVal a, KnownNat n) => AlgoVal (Truncated n a) where
210-
algoVal = Truncated (natVal (Proxy @n)) (algoVal @a)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module System.Nix.Path
2020
) where
2121

2222
import System.Nix.Hash (Digest(..),
23-
HashAlgorithm'(Truncated, SHA256))
23+
HashAlgorithm(Truncated, SHA256))
2424
import System.Nix.Internal.Hash
2525
import qualified Data.ByteString as BS
2626
import qualified Data.ByteString.Char8 as BSC

hnix-store-remote/app/Main.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE DataKinds #-}
23
import qualified Data.ByteString.Lazy as LBS
34
import qualified Data.HashSet as HS
45
import Data.Maybe
@@ -10,6 +11,7 @@ import qualified System.Nix.GC as GC
1011
import System.Nix.Path (PathHashAlgo)
1112
import System.Nix.Store.Remote
1213
import System.Nix.Store.Remote.Util
14+
import System.Nix.Hash
1315

1416
noSuchPath = fromJust $ mkPath "blah"
1517

@@ -24,7 +26,7 @@ main = do
2426
-- (Just path2) <- addTextToStore "hnix-store2" "test2" (HS.fromList []) False
2527
path2 <- addToStore "hi-test-file"
2628
"/home/greghale/code/hnix-store/hnix-store-remote/hi"
27-
False (Proxy :: Proxy PathHashAlgo) (const True) False
29+
False (Proxy :: Proxy 'SHA256) (const True) False
2830

2931
valid <- isValidPathUncached path
3032
valid2 <- isValidPathUncached path2

hnix-store-remote/src/System/Nix/Store/Remote.hs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,10 @@ type Source = () -- abstract binary source
175175
addToStoreNar :: ValidPathInfo -> Source -> RepairFlag -> CheckSigsFlag -> MonadStore ()
176176
addToStoreNar = undefined -- XXX
177177

178-
printHashType :: HashAlgorithm' Integer -> T.Text
179-
printHashType MD5 = "MD5"
180-
printHashType SHA1 = "SHA1"
181-
printHashType SHA256 = "SHA256"
182-
printHashType (Truncated _ a) = printHashType a
183-
184178
type PathFilter = Path -> Bool
185179

186180
addToStore
187-
:: forall a. (HasDigest a, AlgoVal a)
181+
:: forall a. (HasDigest a, NamedAlgo a)
188182
=> LBS.ByteString
189183
-> FilePath
190184
-> Bool
@@ -199,14 +193,12 @@ addToStore name pth recursive algoProxy pfilter repair = do
199193

200194
runOpArgs AddToStore $ do
201195
putByteStringLen name
202-
if algoVal @a `elem` [SHA256, Truncated 20 SHA256] && recursive
203-
then putInt 0
204-
else putInt 1
196+
putInt 1
205197
if recursive
206198
then putInt 1
207199
else putInt 0
208200

209-
putByteStringLen (T.encodeUtf8 . T.toLower . printHashType $ algoVal @a)
201+
putByteStringLen (T.encodeUtf8 . T.toLower . T.fromStrict $ algoName @a)
210202

211203
B.putLazyByteString bs
212204

0 commit comments

Comments
 (0)