From 676eca28e802b602b399b2965ffb15cb8a7a42ae Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Fri, 26 Jan 2018 21:28:18 -0500 Subject: Implement SHA-1 --- src/Data/Digest/Sha1.hsc | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/Data/Digest/Sha1.hsc (limited to 'src') 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 + +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 -- cgit v1.2.3