{-# LANGUAGE PackageImports #-}

-- |
-- Module      : Crypto.Hash.MD2
-- License     : BSD-style
-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
-- Stability   : experimental
-- Portability : unknown
--
-- A module containing MD2 bindings
--
module Crypto.Hash.MD2
    ( Ctx(..)

    -- * Incremental hashing Functions
    , init     -- :: Ctx
    , update   -- :: Ctx -> ByteString -> Ctx
    , updates  -- :: Ctx -> [ByteString] -> Ctx
    , finalize -- :: Ctx -> ByteString

    -- * Single Pass hashing
    , hash     -- :: ByteString -> ByteString
    , hashlazy -- :: ByteString -> ByteString
    ) where

import Prelude hiding (init)
import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString)
import Crypto.Hash.Internal (digestToByteString, digestToByteStringWitness)

import qualified "cryptonite" Crypto.Hash as H

-- | MD2 Context
newtype Ctx = Ctx (H.Context H.MD2)

-- | init a context
init :: Ctx
init :: Ctx
init = Context MD2 -> Ctx
Ctx Context MD2
forall a. HashAlgorithm a => Context a
H.hashInit

-- | update a context with a bytestring
update :: Ctx -> ByteString -> Ctx
update :: Ctx -> ByteString -> Ctx
update (Ctx ctx :: Context MD2
ctx) d :: ByteString
d = Context MD2 -> Ctx
Ctx (Context MD2 -> Ctx) -> Context MD2 -> Ctx
forall a b. (a -> b) -> a -> b
$ Context MD2 -> ByteString -> Context MD2
forall ba a.
(ByteArrayAccess ba, HashAlgorithm a) =>
Context a -> ba -> Context a
H.hashUpdate Context MD2
ctx ByteString
d

-- | updates a context with multiples bytestring
updates :: Ctx -> [ByteString] -> Ctx
updates :: Ctx -> [ByteString] -> Ctx
updates (Ctx ctx :: Context MD2
ctx) d :: [ByteString]
d =
    Context MD2 -> Ctx
Ctx (Context MD2 -> Ctx) -> Context MD2 -> Ctx
forall a b. (a -> b) -> a -> b
$ Context MD2 -> [ByteString] -> Context MD2
forall a ba.
(HashAlgorithm a, ByteArrayAccess ba) =>
Context a -> [ba] -> Context a
H.hashUpdates Context MD2
ctx [ByteString]
d

-- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString
finalize :: Ctx -> ByteString
finalize (Ctx ctx :: Context MD2
ctx) = Digest MD2 -> ByteString
forall h. HashAlgorithm h => Digest h -> ByteString
digestToByteString (Digest MD2 -> ByteString) -> Digest MD2 -> ByteString
forall a b. (a -> b) -> a -> b
$ Context MD2 -> Digest MD2
forall a. HashAlgorithm a => Context a -> Digest a
H.hashFinalize Context MD2
ctx

-- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString
hash :: ByteString -> ByteString
hash d :: ByteString
d = MD2 -> Digest MD2 -> ByteString
forall h. HashAlgorithm h => h -> Digest h -> ByteString
digestToByteStringWitness MD2
H.MD2 (Digest MD2 -> ByteString) -> Digest MD2 -> ByteString
forall a b. (a -> b) -> a -> b
$ ByteString -> Digest MD2
forall ba a.
(ByteArrayAccess ba, HashAlgorithm a) =>
ba -> Digest a
H.hash ByteString
d

-- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString
hashlazy :: ByteString -> ByteString
hashlazy l :: ByteString
l = MD2 -> Digest MD2 -> ByteString
forall h. HashAlgorithm h => h -> Digest h -> ByteString
digestToByteStringWitness MD2
H.MD2 (Digest MD2 -> ByteString) -> Digest MD2 -> ByteString
forall a b. (a -> b) -> a -> b
$ ByteString -> Digest MD2
forall a. HashAlgorithm a => ByteString -> Digest a
H.hashlazy ByteString
l