{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Crypto.Hash (
Context,
Digest,
digestFromByteString,
hashInitWith,
hashWith,
hashPrefixWith,
hashInit,
hashUpdates,
hashUpdate,
hashFinalize,
hashFinalizePrefix,
hashBlockSize,
hashDigestSize,
hash,
hashPrefix,
hashlazy,
module Crypto.Hash.Algorithms,
) where
import Crypto.Hash.Algorithms
import Crypto.Hash.Types
import Crypto.Internal.ByteArray (ByteArrayAccess, allocAndFreezePrim)
import qualified Crypto.Internal.ByteArray as B
import Data.Bits (shiftR, unsafeShiftR, (.|.))
import qualified Data.ByteString.Lazy as L
import Data.Int (Int64)
import Data.Word (Word32, Word64)
import Foreign.Marshal.Alloc (allocaBytes)
import qualified Foreign.Marshal.Utils as FMU
import Foreign.Ptr (Ptr, castPtr)
hash :: forall ba a. (ByteArrayAccess ba, HashAlgorithm a) => ba -> Digest a
hash :: forall ba a.
(ByteArrayAccess ba, HashAlgorithm a) =>
ba -> Digest a
hash ba
bs = ByteArray -> Digest a
forall a. ByteArray -> Digest a
Digest (ByteArray -> Digest a) -> ByteArray -> Digest a
forall a b. (a -> b) -> a -> b
$
Int -> (Ptr (Digest a) -> IO ()) -> ByteArray
forall p. Int -> (Ptr p -> IO ()) -> ByteArray
allocAndFreezePrim (a -> Int
forall a. HashAlgorithm a => a -> Int
hashDigestSize (a
forall a. HasCallStack => a
undefined :: a)) ((Ptr (Digest a) -> IO ()) -> ByteArray)
-> (Ptr (Digest a) -> IO ()) -> ByteArray
forall a b. (a -> b) -> a -> b
$ \(Ptr (Digest a)
dig :: Ptr (Digest a)) ->
Int -> (Ptr (Context a) -> IO ()) -> IO ()
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes (a -> Int
forall a. HashAlgorithm a => a -> Int
hashInternalContextSize (a
forall a. HasCallStack => a
undefined :: a)) ((Ptr (Context a) -> IO ()) -> IO ())
-> (Ptr (Context a) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Ptr (Context a)
ctx :: Ptr (Context a)) -> do
Ptr (Context a) -> IO ()
forall a. HashAlgorithm a => Ptr (Context a) -> IO ()
hashInternalInit Ptr (Context a)
ctx
ba -> (Ptr Word8 -> IO ()) -> IO ()
forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
forall p a. ba -> (Ptr p -> IO a) -> IO a
B.withByteArray ba
bs (Ptr (Context a) -> Int -> Ptr Word8 -> IO ()
forall a.
HashAlgorithm a =>
Ptr (Context a) -> Int -> Ptr Word8 -> IO ()
updateBlocks Ptr (Context a)
ctx (ba -> Int
forall ba. ByteArrayAccess ba => ba -> Int
B.length ba
bs))
Ptr (Context a) -> Ptr (Digest a) -> IO ()
forall a.
HashAlgorithm a =>
Ptr (Context a) -> Ptr (Digest a) -> IO ()
hashInternalFinalize Ptr (Context a)
ctx Ptr (Digest a)
dig
hashPrefix
:: (ByteArrayAccess ba, HashAlgorithmPrefix a) => ba -> Int -> Digest a
hashPrefix :: forall ba a.
(ByteArrayAccess ba, HashAlgorithmPrefix a) =>
ba -> Int -> Digest a
hashPrefix = Context a -> ba -> Int -> Digest a
forall a ba.
(HashAlgorithmPrefix a, ByteArrayAccess ba) =>
Context a -> ba -> Int -> Digest a
hashFinalizePrefix Context a
forall a. HashAlgorithm a => Context a
hashInit
hashlazy :: HashAlgorithm a => L.ByteString -> Digest a
hashlazy :: forall a. HashAlgorithm a => ByteString -> Digest a
hashlazy ByteString
lbs = Context a -> Digest a
forall a. HashAlgorithm a => Context a -> Digest a
hashFinalize (Context a -> Digest a) -> Context a -> Digest a
forall a b. (a -> b) -> a -> b
$ Context a -> [StrictByteString] -> Context a
forall a ba.
(HashAlgorithm a, ByteArrayAccess ba) =>
Context a -> [ba] -> Context a
hashUpdates Context a
forall a. HashAlgorithm a => Context a
hashInit (ByteString -> [StrictByteString]
L.toChunks ByteString
lbs)
hashInit :: forall a. HashAlgorithm a => Context a
hashInit :: forall a. HashAlgorithm a => Context a
hashInit = Bytes -> Context a
forall a. Bytes -> Context a
Context (Bytes -> Context a) -> Bytes -> Context a
forall a b. (a -> b) -> a -> b
$ Int -> (Ptr (Context a) -> IO ()) -> Bytes
forall a p. ByteArray a => Int -> (Ptr p -> IO ()) -> a
B.allocAndFreeze (a -> Int
forall a. HashAlgorithm a => a -> Int
hashInternalContextSize (a
forall a. HasCallStack => a
undefined :: a)) ((Ptr (Context a) -> IO ()) -> Bytes)
-> (Ptr (Context a) -> IO ()) -> Bytes
forall a b. (a -> b) -> a -> b
$ \(Ptr (Context a)
ptr :: Ptr (Context a)) ->
Ptr (Context a) -> IO ()
forall a. HashAlgorithm a => Ptr (Context a) -> IO ()
hashInternalInit Ptr (Context a)
ptr
hashUpdate
:: (ByteArrayAccess ba, HashAlgorithm a) => Context a -> ba -> Context a
hashUpdate :: forall ba a.
(ByteArrayAccess ba, HashAlgorithm a) =>
Context a -> ba -> Context a
hashUpdate Context a
ctx ba
b
| ba -> Bool
forall a. ByteArrayAccess a => a -> Bool
B.null ba
b = Context a
ctx
| Bool
otherwise = Context a -> [ba] -> Context a
forall a ba.
(HashAlgorithm a, ByteArrayAccess ba) =>
Context a -> [ba] -> Context a
hashUpdates Context a
ctx [ba
b]
hashUpdates
:: forall a ba
. (HashAlgorithm a, ByteArrayAccess ba)
=> Context a
-> [ba]
-> Context a
hashUpdates :: forall a ba.
(HashAlgorithm a, ByteArrayAccess ba) =>
Context a -> [ba] -> Context a
hashUpdates Context a
c [ba]
l
| [ba] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ba]
ls = Context a
c
| Bool
otherwise = Bytes -> Context a
forall a. Bytes -> Context a
Context (Bytes -> Context a) -> Bytes -> Context a
forall a b. (a -> b) -> a -> b
$ Context a -> (Ptr (Context a) -> IO ()) -> Bytes
forall bs1 bs2 p.
(ByteArrayAccess bs1, ByteArray bs2) =>
bs1 -> (Ptr p -> IO ()) -> bs2
B.copyAndFreeze Context a
c ((Ptr (Context a) -> IO ()) -> Bytes)
-> (Ptr (Context a) -> IO ()) -> Bytes
forall a b. (a -> b) -> a -> b
$ \(Ptr (Context a)
ctx :: Ptr (Context a)) ->
(ba -> IO ()) -> [ba] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\ba
b -> ba -> (Ptr Word8 -> IO ()) -> IO ()
forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
forall p a. ba -> (Ptr p -> IO a) -> IO a
B.withByteArray ba
b (Ptr (Context a) -> Int -> Ptr Word8 -> IO ()
forall a.
HashAlgorithm a =>
Ptr (Context a) -> Int -> Ptr Word8 -> IO ()
updateBlocks Ptr (Context a)
ctx (ba -> Int
forall ba. ByteArrayAccess ba => ba -> Int
B.length ba
b))) [ba]
ls
where
ls :: [ba]
ls = (ba -> Bool) -> [ba] -> [ba]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (ba -> Bool) -> ba -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ba -> Bool
forall a. ByteArrayAccess a => a -> Bool
B.null) [ba]
l
hashFinalize
:: forall a
. HashAlgorithm a
=> Context a
-> Digest a
hashFinalize :: forall a. HashAlgorithm a => Context a -> Digest a
hashFinalize !Context a
c = ByteArray -> Digest a
forall a. ByteArray -> Digest a
Digest (ByteArray -> Digest a) -> ByteArray -> Digest a
forall a b. (a -> b) -> a -> b
$
Int -> (Ptr (Digest a) -> IO ()) -> ByteArray
forall p. Int -> (Ptr p -> IO ()) -> ByteArray
allocAndFreezePrim (a -> Int
forall a. HashAlgorithm a => a -> Int
hashDigestSize (a
forall a. HasCallStack => a
undefined :: a)) ((Ptr (Digest a) -> IO ()) -> ByteArray)
-> (Ptr (Digest a) -> IO ()) -> ByteArray
forall a b. (a -> b) -> a -> b
$
\(Ptr (Digest a)
dig :: Ptr (Digest a)) -> do
((!_) :: B.Bytes) <- Context a -> (Ptr (Context a) -> IO ()) -> IO Bytes
forall bs1 bs2 p.
(ByteArrayAccess bs1, ByteArray bs2) =>
bs1 -> (Ptr p -> IO ()) -> IO bs2
B.copy Context a
c ((Ptr (Context a) -> IO ()) -> IO Bytes)
-> (Ptr (Context a) -> IO ()) -> IO Bytes
forall a b. (a -> b) -> a -> b
$ \(Ptr (Context a)
ctx :: Ptr (Context a)) -> Ptr (Context a) -> Ptr (Digest a) -> IO ()
forall a.
HashAlgorithm a =>
Ptr (Context a) -> Ptr (Digest a) -> IO ()
hashInternalFinalize Ptr (Context a)
ctx Ptr (Digest a)
dig
return ()
hashFinalizePrefix
:: forall a ba
. (HashAlgorithmPrefix a, ByteArrayAccess ba)
=> Context a
-> ba
-> Int
-> Digest a
hashFinalizePrefix :: forall a ba.
(HashAlgorithmPrefix a, ByteArrayAccess ba) =>
Context a -> ba -> Int -> Digest a
hashFinalizePrefix !Context a
c ba
b Int
len
| (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ba -> Int
forall ba. ByteArrayAccess ba => ba -> Int
B.length ba
b) :: Word64) Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
0xfffffe00 =
[Char] -> Digest a
forall a. HasCallStack => [Char] -> a
error [Char]
"Crypto.Hash.hashFinalizePrefix: input of 4 GiB - 512 bytes or more"
| Bool
otherwise = ByteArray -> Digest a
forall a. ByteArray -> Digest a
Digest (ByteArray -> Digest a) -> ByteArray -> Digest a
forall a b. (a -> b) -> a -> b
$
Int -> (Ptr (Digest a) -> IO ()) -> ByteArray
forall p. Int -> (Ptr p -> IO ()) -> ByteArray
allocAndFreezePrim (a -> Int
forall a. HashAlgorithm a => a -> Int
hashDigestSize (a
forall a. HasCallStack => a
undefined :: a)) ((Ptr (Digest a) -> IO ()) -> ByteArray)
-> (Ptr (Digest a) -> IO ()) -> ByteArray
forall a b. (a -> b) -> a -> b
$
\(Ptr (Digest a)
dig :: Ptr (Digest a)) -> do
((!_) :: B.Bytes) <- Context a -> (Ptr (Context a) -> IO ()) -> IO Bytes
forall bs1 bs2 p.
(ByteArrayAccess bs1, ByteArray bs2) =>
bs1 -> (Ptr p -> IO ()) -> IO bs2
B.copy Context a
c ((Ptr (Context a) -> IO ()) -> IO Bytes)
-> (Ptr (Context a) -> IO ()) -> IO Bytes
forall a b. (a -> b) -> a -> b
$ \(Ptr (Context a)
ctx :: Ptr (Context a)) ->
ba -> (Ptr Word8 -> IO ()) -> IO ()
forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
forall p a. ba -> (Ptr p -> IO a) -> IO a
B.withByteArray ba
b ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
d ->
Ptr (Context a)
-> Ptr Word8 -> Word32 -> Word32 -> Ptr (Digest a) -> IO ()
forall a.
HashAlgorithmPrefix a =>
Ptr (Context a)
-> Ptr Word8 -> Word32 -> Word32 -> Ptr (Digest a) -> IO ()
hashInternalFinalizePrefix
Ptr (Context a)
ctx
Ptr Word8
d
(Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word32) -> Int -> Word32
forall a b. (a -> b) -> a -> b
$ ba -> Int
forall ba. ByteArrayAccess ba => ba -> Int
B.length ba
b)
(Int -> Word32
saturate32 Int
len)
Ptr (Digest a)
dig
return ()
saturate32 :: Int -> Word32
saturate32 :: Int -> Word32
saturate32 Int
n =
Int64 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
n64 Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|. Word32 -> Word32
forall a. Num a => a -> a
negate (Word64 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
outside Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
63))
where
n64 :: Int64
n64 = Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n :: Int64
hi :: Int64
hi = Int64
n64 Int64 -> Int -> Int64
forall a. Bits a => a -> Int -> a
`shiftR` Int
32
outside :: Word64
outside = Int64 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64
hi Int64 -> Int64 -> Int64
forall a. Bits a => a -> a -> a
.|. Int64 -> Int64
forall a. Num a => a -> a
negate Int64
hi) :: Word64
hashInitWith :: HashAlgorithm alg => alg -> Context alg
hashInitWith :: forall alg. HashAlgorithm alg => alg -> Context alg
hashInitWith alg
_ = Context alg
forall a. HashAlgorithm a => Context a
hashInit
hashWith :: (ByteArrayAccess ba, HashAlgorithm alg) => alg -> ba -> Digest alg
hashWith :: forall ba alg.
(ByteArrayAccess ba, HashAlgorithm alg) =>
alg -> ba -> Digest alg
hashWith alg
_ = ba -> Digest alg
forall ba a.
(ByteArrayAccess ba, HashAlgorithm a) =>
ba -> Digest a
hash
hashPrefixWith
:: (ByteArrayAccess ba, HashAlgorithmPrefix alg) => alg -> ba -> Int -> Digest alg
hashPrefixWith :: forall ba alg.
(ByteArrayAccess ba, HashAlgorithmPrefix alg) =>
alg -> ba -> Int -> Digest alg
hashPrefixWith alg
_ = ba -> Int -> Digest alg
forall ba a.
(ByteArrayAccess ba, HashAlgorithmPrefix a) =>
ba -> Int -> Digest a
hashPrefix
digestFromByteString
:: forall a ba. (HashAlgorithm a, ByteArrayAccess ba) => ba -> Maybe (Digest a)
digestFromByteString :: forall a ba.
(HashAlgorithm a, ByteArrayAccess ba) =>
ba -> Maybe (Digest a)
digestFromByteString = a -> ba -> Maybe (Digest a)
from a
forall a. HasCallStack => a
undefined
where
from :: a -> ba -> Maybe (Digest a)
from :: a -> ba -> Maybe (Digest a)
from a
alg ba
bs
| ba -> Int
forall ba. ByteArrayAccess ba => ba -> Int
B.length ba
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== (a -> Int
forall a. HashAlgorithm a => a -> Int
hashDigestSize a
alg) =
Digest a -> Maybe (Digest a)
forall a. a -> Maybe a
Just (Digest a -> Maybe (Digest a)) -> Digest a -> Maybe (Digest a)
forall a b. (a -> b) -> a -> b
$ ByteArray -> Digest a
forall a. ByteArray -> Digest a
Digest (ByteArray -> Digest a) -> ByteArray -> Digest a
forall a b. (a -> b) -> a -> b
$ ba -> ByteArray
forall {ba}. ByteArrayAccess ba => ba -> ByteArray
copyByteArray ba
bs
| Bool
otherwise = Maybe (Digest a)
forall a. Maybe a
Nothing
copyByteArray :: ba -> ByteArray
copyByteArray ba
ba = Int -> (Ptr (ZonkAny 0) -> IO ()) -> ByteArray
forall p. Int -> (Ptr p -> IO ()) -> ByteArray
allocAndFreezePrim (ba -> Int
forall ba. ByteArrayAccess ba => ba -> Int
B.length ba
ba) ((Ptr (ZonkAny 0) -> IO ()) -> ByteArray)
-> (Ptr (ZonkAny 0) -> IO ()) -> ByteArray
forall a b. (a -> b) -> a -> b
$ \Ptr (ZonkAny 0)
dst ->
ba -> (Ptr (ZonkAny 1) -> IO ()) -> IO ()
forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
forall p a. ba -> (Ptr p -> IO a) -> IO a
B.withByteArray ba
ba ((Ptr (ZonkAny 1) -> IO ()) -> IO ())
-> (Ptr (ZonkAny 1) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr (ZonkAny 1)
src ->
Ptr (ZonkAny 0) -> Ptr (ZonkAny 0) -> Int -> IO ()
forall a. Ptr a -> Ptr a -> Int -> IO ()
FMU.copyBytes Ptr (ZonkAny 0)
dst (Ptr (ZonkAny 1) -> Ptr (ZonkAny 0)
forall a b. Ptr a -> Ptr b
castPtr Ptr (ZonkAny 1)
src) (ba -> Int
forall ba. ByteArrayAccess ba => ba -> Int
B.length ba
ba)