module Crypto.Hash.SHA512t
( Ctx(..)
, init
, update
, finalize
, hash
, hashlazy
) where
import Prelude hiding (init)
import Data.List (foldl')
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
import qualified Crypto.Hash.SHA512 as SHA512
data Ctx = Ctx !Int !SHA512.Ctx
init :: Int -> Ctx
init :: Int -> Ctx
init t :: Int
t = Int -> Ctx -> Ctx
Ctx Int
t (Int -> Ctx
SHA512.init_t Int
t)
update :: Ctx -> ByteString -> Ctx
update :: Ctx -> ByteString -> Ctx
update (Ctx t :: Int
t ctx :: Ctx
ctx) d :: ByteString
d = Int -> Ctx -> Ctx
Ctx Int
t (Ctx -> ByteString -> Ctx
SHA512.update Ctx
ctx ByteString
d)
finalize :: Ctx -> ByteString
finalize :: Ctx -> ByteString
finalize (Ctx sz :: Int
sz ctx :: Ctx
ctx) = Int -> ByteString -> ByteString
B.take (Int
sz Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 8) (Ctx -> ByteString
SHA512.finalize Ctx
ctx)
hash :: Int -> ByteString -> ByteString
hash :: Int -> ByteString -> ByteString
hash t :: Int
t = Ctx -> ByteString
finalize (Ctx -> ByteString)
-> (ByteString -> Ctx) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ctx -> ByteString -> Ctx
update (Int -> Ctx
init Int
t)
hashlazy :: Int -> L.ByteString -> ByteString
hashlazy :: Int -> ByteString -> ByteString
hashlazy t :: Int
t = Ctx -> ByteString
finalize (Ctx -> ByteString)
-> (ByteString -> Ctx) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ctx -> ByteString -> Ctx) -> Ctx -> [ByteString] -> Ctx
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Ctx -> ByteString -> Ctx
update (Int -> Ctx
init Int
t) ([ByteString] -> Ctx)
-> (ByteString -> [ByteString]) -> ByteString -> Ctx
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
L.toChunks