aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Benjamin Barenblat <bbaren@google.com>2018-01-26 21:28:18 -0500
committerGravatar Benjamin Barenblat <bbaren@google.com>2018-01-26 21:28:18 -0500
commit676eca28e802b602b399b2965ffb15cb8a7a42ae (patch)
tree637affd93d4203646d7c42dfb301a0fda7ffac81 /src
parent4f5f81d8df452b83dbe0f9f31c4d657200c54dfd (diff)
Implement SHA-1
Diffstat (limited to 'src')
-rw-r--r--src/Data/Digest/Sha1.hsc38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/Data/Digest/Sha1.hsc b/src/Data/Digest/Sha1.hsc
new file mode 100644
index 0000000..6ac4c34
--- /dev/null
+++ b/src/Data/Digest/Sha1.hsc
@@ -0,0 +1,38 @@
+{-# LANGUAGE CApiFFI #-}
+{-# OPTIONS_GHC -Wno-missing-methods #-}
+
+module Data.Digest.Sha1
+ ( sha1
+ ) where
+
+import Data.ByteString.Lazy (ByteString)
+import Foreign (Ptr, Storable(alignment, sizeOf))
+import Foreign.C.Types
+
+import Data.Digest.Internal
+
+#include <openssl/sha.h>
+
+data ShaCtx
+
+instance Storable ShaCtx where
+ sizeOf _ = #size SHA_CTX
+ alignment _ = #alignment SHA_CTX
+
+foreign import capi "openssl/sha.h value SHA_DIGEST_LENGTH"
+ shaDigestLength :: CSize
+
+foreign import ccall "openssl/sha.h SHA1_Init"
+ sha1Init :: Ptr ShaCtx -> IO CInt
+
+foreign import ccall "openssl/sha.h SHA1_Update"
+ sha1Update :: Ptr ShaCtx -> Ptr a -> CSize -> IO CInt
+
+foreign import ccall "openssl/sha.h SHA1_Final"
+ sha1Final :: Ptr CUChar -> Ptr ShaCtx -> IO CInt
+
+sha1Algo :: Algo
+sha1Algo = Algo shaDigestLength sha1Init sha1Update sha1Final
+
+sha1 :: ByteString -> Digest
+sha1 = hash sha1Algo