-- |-- Module : Crypto.Hash.Types-- License : BSD-style-- Maintainer : Vincent Hanquez <vincent@snarc.org>-- Stability : experimental-- Portability : unknown---- Crypto hash types definitions--
module Crypto.Hash.Types
( HashAlgorithm(..)
, Context(..)
, Digest(..)
-- * deprecated
, contextToByteString
, digestToByteString
)
where
import Data.ByteString (ByteString)
import Data.Byteable
import qualified Data.ByteString.Char8 as BC
import Crypto.Hash.Utils (toHex)
-- | Class representing hashing algorithms.---- The hash algorithm is built over 3 primitives:---- * init : create a new hashing context---- * updates : update the hashing context with some strict bytestrings-- and return the new context---- * finalize : finalize the context into a digest--
class HashAlgorithma where
-- | Block size in bytes the hash algorithm operates onhashBlockSize :: Contexta -> Int-- | Initialize a new context for this hash algorithmhashInit :: Contexta-- | Update the context with a list of strict bytestring,-- and return a new context with the updates.hashUpdates :: Contexta -> [ByteString] -> Contexta-- | Finalize a context and return a digest.hashFinalize :: Contexta -> Digesta-- | Try to convert a binary digest bytestring to a digest.digestFromByteString :: ByteString -> Maybe (Digesta)
-- | Represent a context for a given hash algorithm.
newtype Contexta = ContextByteString
instance Byteable (Contexta) where
toBytes (Contextbs) = bs-- | return the binary bytestring. deprecated use toBytes.contextToByteString :: Contexta -> ByteStringcontextToByteString = toBytes-- | Represent a digest for a given hash algorithm.
newtype Digesta = DigestByteStringderiving (Eq,Ord)
instance Byteable (Digesta) where
toBytes (Digestbs) = bs-- | return the binary bytestring. deprecated use toBytes.{-# DEPRECATED digestToByteString "use toBytes from byteable:Data.Byteable" #-}digestToByteString :: Digesta -> ByteStringdigestToByteString = toBytes
instance Show (Digesta) where
show (Digestbs) = BC.unpack$toHexbs