Skip to content

Commit 7186850

Browse files
Ericson2314sorki
authored andcommitted
core: System.Nix.Hash add HashAlgo
prereq for replacing SomeNamedDigest with (DSum HashAlgo Digest)
1 parent fc0133f commit 7186850

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

cabal.project

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ packages:
44

55
package hnix-store-remote
66
flags: +build-readme +io-testsuite
7-

hnix-store-core/hnix-store-core.cabal

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ common commons
3737
, ExistentialQuantification
3838
, FlexibleContexts
3939
, FlexibleInstances
40+
, GADTs
4041
, StandaloneDeriving
4142
, ScopedTypeVariables
4243
, StandaloneDeriving
@@ -48,6 +49,7 @@ common commons
4849
, InstanceSigs
4950
, KindSignatures
5051
, MultiParamTypeClasses
52+
, MultiWayIf
5153
, TupleSections
5254
, LambdaCase
5355
, BangPatterns
@@ -83,6 +85,7 @@ library
8385
, case-insensitive
8486
, cereal
8587
, containers
88+
, constraints-extras
8689
, data-default-class
8790
, generic-arbitrary < 1.1
8891
-- Required for cryptonite low-level type convertion
@@ -97,7 +100,8 @@ library
97100
, nix-derivation >= 1.1.1 && <2
98101
, QuickCheck
99102
, quickcheck-instances
100-
, saltine
103+
, saltine >= 0.2 && < 0.3
104+
, some > 1.0.5 && < 2
101105
, time
102106
, text
103107
, unix

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
{-# LANGUAGE AllowAmbiguousTypes #-}
22
{-# LANGUAGE CPP #-}
33
{-# LANGUAGE OverloadedStrings #-}
4+
{-# LANGUAGE TemplateHaskell #-}
45
{-# OPTIONS_GHC -Wno-orphans #-}
56
{-|
67
Description : Cryptographic hashing interface for hnix-store, on top
78
of the cryptohash family of libraries.
89
-}
910

1011
module System.Nix.Hash
11-
( NamedAlgo(..)
12+
( HashAlgo(..)
13+
, NamedAlgo(..)
14+
, algoToText
15+
, textToAlgo
1216
, SomeNamedDigest(..)
1317
, mkNamedDigest
1418

@@ -23,6 +27,10 @@ module System.Nix.Hash
2327

2428
import Crypto.Hash (Digest, HashAlgorithm, MD5(..), SHA1(..), SHA256(..), SHA512(..))
2529
import Data.ByteString (ByteString)
30+
import Data.Constraint.Extras (Has(has))
31+
import Data.Constraint.Extras.TH (deriveArgDict)
32+
import Data.Kind (Type)
33+
import Data.Some (Some(Some))
2634
import Data.Text (Text)
2735
import Data.Text.Lazy.Builder (Builder)
2836
import System.Nix.Base (BaseEncoding(..))
@@ -67,6 +75,32 @@ instance Arbitrary (Digest SHA256) where
6775
instance Arbitrary (Digest SHA512) where
6876
arbitrary = Crypto.Hash.hash @ByteString <$> arbitrary
6977

78+
data HashAlgo :: Type -> Type where
79+
HashAlgo_MD5 :: HashAlgo MD5
80+
HashAlgo_SHA1 :: HashAlgo SHA1
81+
HashAlgo_SHA256 :: HashAlgo SHA256
82+
HashAlgo_SHA512 :: HashAlgo SHA512
83+
84+
deriveArgDict ''HashAlgo
85+
86+
algoToText :: forall t. HashAlgo t -> Text
87+
algoToText x = has @NamedAlgo x (algoName @t)
88+
89+
_hashAlgoValue :: HashAlgo a -> a
90+
_hashAlgoValue = \case
91+
HashAlgo_MD5 -> MD5
92+
HashAlgo_SHA1 -> SHA1
93+
HashAlgo_SHA256 -> SHA256
94+
HashAlgo_SHA512 -> SHA512
95+
96+
textToAlgo :: Text -> Either String (Some HashAlgo)
97+
textToAlgo = \case
98+
"md5" -> Right $ Some HashAlgo_MD5
99+
"sha1" -> Right $ Some HashAlgo_SHA1
100+
"sha256" -> Right $ Some HashAlgo_SHA256
101+
"sha512" -> Right $ Some HashAlgo_SHA512
102+
name -> Left $ "Unknown hash name: " <> Data.Text.unpack name
103+
70104
-- | A digest whose 'NamedAlgo' is not known at compile time.
71105
data SomeNamedDigest = forall a . NamedAlgo a => SomeDigest (Digest a)
72106

0 commit comments

Comments
 (0)