module Network.BackblazeB2.Crypto where

import qualified Crypto.Hash  as Hash
import qualified Data.Conduit as C

import           LocalPrelude

sha1Hasher :: Monad m => C.ConduitT ByteString ByteString m ()
sha1Hasher = loop Hash.hashInit
  where
    loop :: Monad m => Hash.Context Hash.SHA1
         -> C.ConduitT ByteString ByteString m ()
    loop !context = do
      bytesM <- C.await
      case bytesM of
        Just bytes -> do C.yield bytes
                         loop $ Hash.hashUpdate context bytes
        _ -> do let digest = Hash.hashFinalize context
                C.yield $ show digest