0 | module Data.Cryptography.Hash.SHA
6 | import Data.Cryptography.Hash
8 | %foreign "C:hs_cryptohash_sha1_init,libsha-idris"
9 | prim__sha1_init: Buffer -> PrimIO Unit
11 | %foreign "C:hs_cryptohash_sha1_update,libsha-idris"
12 | prim__sha1_update: Buffer -> Buffer -> Int -> PrimIO Unit
14 | %foreign "C:hs_cryptohash_sha1_finalize,libsha-idris"
15 | prim__sha1_finalize: Buffer -> Buffer -> PrimIO Bits64
17 | %foreign "C:hs_cryptohash_sha256_init,libsha-idris"
18 | prim__sha256_init: Buffer -> PrimIO Unit
20 | %foreign "C:hs_cryptohash_sha256_update,libsha-idris"
21 | prim__sha256_update: Buffer -> Buffer -> Int -> PrimIO Unit
23 | %foreign "C:hs_cryptohash_sha256_finalize,libsha-idris"
24 | prim__sha256_finalize: Buffer -> Buffer -> PrimIO Bits64
26 | setBytes : Buffer -> Int -> List Bits8 -> IO Unit
27 | setBytes buf offset [] = pure ()
28 | setBytes buf offset (x :: xs) = do
29 | Buffer.setBits8 buf offset x
30 | setBytes buf (offset + 1) xs
32 | mkMaker: {sz: Nat} -> (Buffer -> PrimIO Unit) -> Vect sz Bits8
33 | mkMaker initter = assert_total $
unsafePerformIO $
do
34 | Just bufCtx <- newBuffer . the (Nat -> Int) cast $
sz
35 | | Nothing => idris_crash "Buffer alloc failed"
36 | primIO $
initter bufCtx
37 | bits8 <- bufferData' bufCtx
38 | Just vec <- pure $
toVect sz bits8
39 | | Nothing => idris_crash "Unexpected buf length"
42 | mkAppender : {sz: Nat}
43 | -> (Buffer -> Buffer -> Int -> PrimIO Unit)
47 | mkAppender updater listBits8 ctx = assert_total $
unsafePerformIO $
do
48 | Just toHash <- newBuffer (the (Nat -> Int) cast $
List.length listBits8)
49 | | Nothing => idris_crash "Buffer alloc failed"
50 | setBytes toHash 0 listBits8
51 | Just bufCtx <- newBuffer . the (Nat -> Int) cast $
sz
52 | | Nothing => idris_crash "Buffer alloc failed"
53 | setBytes bufCtx 0 (toList ctx)
54 | primIO $
updater bufCtx toHash (the (Nat -> Int) cast $
List.length listBits8)
55 | bits8 <- bufferData' bufCtx
56 | Just vec <- pure $
toVect sz bits8
57 | | Nothing => idris_crash "Unexpected buf length"
58 | the (IO (Vect sz Bits8)) (pure vec)
60 | mkFinalizer: {sz, outputSize: Nat}
61 | -> (Buffer -> Buffer -> PrimIO Bits64)
63 | -> Vect outputSize Bits8
64 | mkFinalizer finalizer ctx = assert_total $
unsafePerformIO $
do
65 | Just bufCtx <- newBuffer 104
66 | | Nothing => idris_crash "Buffer alloc failed"
67 | setBytes bufCtx 0 (toList ctx)
68 | Just out <- newBuffer . the (Nat -> Int) cast $
outputSize
69 | | Nothing => idris_crash "Buffer alloc failed"
70 | totalBytes <- primIO $
finalizer bufCtx out
71 | bits8List <- bufferData' out
72 | Just bits8Vec <- pure $
toVect outputSize bits8List
73 | | Nothing => idris_crash "Unexpected buf length"
79 | sha1 = MkHashAlgorithm
81 | , ctxType = Vect 92 Bits8
82 | , mkHashCtx = mkMaker {sz=92} prim__sha1_init
83 | , appendHash = mkAppender {sz=92} prim__sha1_update
84 | , finalizeHash = mkFinalizer {sz=92, outputSize=20} prim__sha1_finalize
89 | sha256: HashAlgorithm
90 | sha256 = MkHashAlgorithm
92 | , ctxType = Vect 104 Bits8
93 | , mkHashCtx = mkMaker {sz=104} prim__sha256_init
94 | , appendHash = mkAppender {sz=104} prim__sha256_update
95 | , finalizeHash = mkFinalizer {sz=104, outputSize=32} prim__sha256_finalize