{-# LANGUAGE RankNTypes, BangPatterns #-}
-- |
-- Module      : Crypto.MAC.HMAC.Conduit
-- License     : BSD-style
-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
-- Stability   : experimental
-- Portability : unknown
--
-- A module containing Conduit facilities for hmac based functions.
--
module Crypto.MAC.HMAC.Conduit
    ( -- * Cryptographic hash functions
      sinkHMAC
    ) where

import Crypto.Hash
import Crypto.MAC.HMAC
import Data.ByteArray
import Data.Conduit
import qualified Data.ByteString as BS

-- | A 'Sink' that calculates HMAC of a stream of 'B.ByteString'@s@ and
-- returns digest @d@.
sinkHMAC :: (Monad m, ByteArrayAccess key, HashAlgorithm hash) => key -> ConduitM BS.ByteString o m (HMAC hash)
sinkHMAC :: forall (m :: * -> *) key hash o.
(Monad m, ByteArrayAccess key, HashAlgorithm hash) =>
key -> ConduitM ByteString o m (HMAC hash)
sinkHMAC key
key = forall {m :: * -> *} {a} {message} {o}.
(Monad m, HashAlgorithm a, ByteArrayAccess message) =>
Context a -> ConduitT message o m (HMAC a)
sink (forall key a.
(ByteArrayAccess key, HashAlgorithm a) =>
key -> Context a
initialize key
key)
  where sink :: Context a -> ConduitT message o m (HMAC a)
sink Context a
ctx = do
            Maybe message
b <- forall (m :: * -> *) i o. Monad m => ConduitT i o m (Maybe i)
await
            case Maybe message
b of
                Maybe message
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall a. HashAlgorithm a => Context a -> HMAC a
finalize Context a
ctx
                Just message
bs -> Context a -> ConduitT message o m (HMAC a)
sink forall a b. (a -> b) -> a -> b
$! forall message a.
(ByteArrayAccess message, HashAlgorithm a) =>
Context a -> message -> Context a
update Context a
ctx message
bs