blob: f5878635210fe9874d6013b5c97b86f6b4af0681 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
{-# LANGUAGE CApiFFI #-}
{-# OPTIONS_GHC -Wno-missing-methods #-}
module Data.Digest.Sha2
( sha224
, sha256
, sha384
, sha512
) where
import Data.ByteString (ByteString)
import Foreign (Ptr, Storable(alignment, sizeOf))
import Foreign.C.Types
import Data.Digest.Internal
#include <openssl/sha.h>
-- SHA-224
foreign import capi "openssl/sha.h value SHA224_DIGEST_LENGTH"
sha224DigestLength :: CSize
foreign import ccall "openssl/sha.h SHA224_Init"
sha224Init :: Ptr Sha256Ctx -> IO CInt
foreign import ccall "openssl/sha.h SHA224_Update"
sha224Update :: Ptr Sha256Ctx -> Ptr a -> CSize -> IO CInt
foreign import ccall "openssl/sha.h SHA224_Final"
sha224Final :: Ptr CUChar -> Ptr Sha256Ctx -> IO CInt
sha224Algo :: Algo
sha224Algo = Algo sha224DigestLength sha224Init sha224Update sha224Final
sha224 :: ByteString -> Digest
sha224 = hash sha224Algo
-- SHA-256
data Sha256Ctx
instance Storable Sha256Ctx where
sizeOf _ = #size SHA256_CTX
alignment _ = #alignment SHA256_CTX
foreign import capi "openssl/sha.h value SHA256_DIGEST_LENGTH"
sha256DigestLength :: CSize
foreign import ccall "openssl/sha.h SHA256_Init"
sha256Init :: Ptr Sha256Ctx -> IO CInt
foreign import ccall "openssl/sha.h SHA256_Update"
sha256Update :: Ptr Sha256Ctx -> Ptr a -> CSize -> IO CInt
foreign import ccall "openssl/sha.h SHA256_Final"
sha256Final :: Ptr CUChar -> Ptr Sha256Ctx -> IO CInt
sha256Algo :: Algo
sha256Algo = Algo sha256DigestLength sha256Init sha256Update sha256Final
sha256 :: ByteString -> Digest
sha256 = hash sha256Algo
-- SHA-384
foreign import capi "openssl/sha.h value SHA384_DIGEST_LENGTH"
sha384DigestLength :: CSize
foreign import ccall "openssl/sha.h SHA384_Init"
sha384Init :: Ptr Sha512Ctx -> IO CInt
foreign import ccall "openssl/sha.h SHA384_Update"
sha384Update :: Ptr Sha512Ctx -> Ptr a -> CSize -> IO CInt
foreign import ccall "openssl/sha.h SHA384_Final"
sha384Final :: Ptr CUChar -> Ptr Sha512Ctx -> IO CInt
sha384Algo :: Algo
sha384Algo = Algo sha384DigestLength sha384Init sha384Update sha384Final
sha384 :: ByteString -> Digest
sha384 = hash sha384Algo
-- SHA-512
data Sha512Ctx
instance Storable Sha512Ctx where
sizeOf _ = #size SHA512_CTX
alignment _ = #alignment SHA512_CTX
foreign import capi "openssl/sha.h value SHA512_DIGEST_LENGTH"
sha512DigestLength :: CSize
foreign import ccall "openssl/sha.h SHA512_Init"
sha512Init :: Ptr Sha512Ctx -> IO CInt
foreign import ccall "openssl/sha.h SHA512_Update"
sha512Update :: Ptr Sha512Ctx -> Ptr a -> CSize -> IO CInt
foreign import ccall "openssl/sha.h SHA512_Final"
sha512Final :: Ptr CUChar -> Ptr Sha512Ctx -> IO CInt
sha512Algo :: Algo
sha512Algo = Algo sha512DigestLength sha512Init sha512Update sha512Final
sha512 :: ByteString -> Digest
sha512 = hash sha512Algo
|