Skip to content

Commit 50f1329

Browse files
committed
hash: add printHashAlgo
1 parent 2957ce1 commit 50f1329

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module System.Nix.Hash (
2222

2323
, HNix.printAsBase16
2424
, HNix.printAsBase32
25+
, HNix.printHashAlgo
2526
) where
2627

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

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class HasDigest (a :: HashAlgorithm) where
7676
initialize :: AlgoCtx a
7777
update :: AlgoCtx a -> BS.ByteString -> AlgoCtx a
7878
finalize :: AlgoCtx a -> Digest a
79+
hashName :: T.Text
7980

8081

8182
-- | The cryptographic hash of of a strict bytestring, where hash
@@ -111,30 +112,37 @@ printAsBase16 (Digest bs) = printHashBytes16 bs
111112
printAsBase32 :: Digest a -> T.Text
112113
printAsBase32 (Digest bs) = printHashBytes32 bs
113114

115+
-- | Print lowercased name of the hashing algorithm
116+
printHashAlgo :: forall a.HasDigest a => Digest a -> T.Text
117+
printHashAlgo _ = hashName @a
114118

115119
instance HasDigest MD5 where
116120
type AlgoCtx 'MD5 = MD5.Ctx
117121
initialize = MD5.init
118122
update = MD5.update
119123
finalize = Digest . MD5.finalize
124+
hashName = T.pack "md5"
120125

121126
instance HasDigest 'SHA1 where
122127
type AlgoCtx SHA1 = SHA1.Ctx
123128
initialize = SHA1.init
124129
update = SHA1.update
125130
finalize = Digest . SHA1.finalize
131+
hashName = T.pack "sha1"
126132

127133
instance HasDigest 'SHA256 where
128134
type AlgoCtx SHA256 = SHA256.Ctx
129135
initialize = SHA256.init
130136
update = SHA256.update
131137
finalize = Digest . SHA256.finalize
138+
hashName = T.pack "sha256"
132139

133140
instance (HasDigest a, KnownNat n) => HasDigest (Truncated n a) where
134141
type AlgoCtx (Truncated n a) = AlgoCtx a
135142
initialize = initialize @a
136143
update = update @a
137144
finalize = truncateDigest @n . finalize @a
145+
hashName = hashName @a
138146

139147
-- | A raw hash digest, with a type-level tag
140148
newtype Digest (a :: HashAlgorithm) = Digest

0 commit comments

Comments
 (0)