1 {-# LANGUAGE CPP, ForeignFunctionInterface #-}
    2 
    3 -- Private interface to a C implementation of SHA 256. Originally based on code
    4 -- by Zooko O'Whielacronx, but rewritten since. Therefore, BSD applies, as for
    5 -- the rest of hashed-storage.
    6 
    7 module Bundled.SHA256 ( sha256 ) where
    8 
    9 import Foreign
   10 import Foreign.C.Types
   11 import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
   12 import qualified Data.ByteString.Internal as BSI
   13 
   14 sha256 :: BSI.ByteString -> BSI.ByteString
   15 sha256 p = unsafePerformIO $ do
   16              digest <- BSI.create 32 $ \digest ->
   17                        unsafeUseAsCStringLen p $ \(ptr,n) ->
   18                            c_sha256 ptr (fromIntegral n) digest
   19              return $! digest
   20 
   21 -- void sha256sum(const unsigned char *d, size_t n, unsigned char *md);
   22 foreign import ccall unsafe "sha2.h hashed_storage_sha256" c_sha256
   23     :: Ptr CChar -> CSize -> Ptr Word8 -> IO ()