| License | BSD-style |
|---|---|
| Maintainer | Vincent Hanquez <vincent@snarc.org> |
| Stability | experimental |
| Portability | unknown |
| Safe Haskell | None |
| Language | Haskell2010 |
Crypto.Hash
Description
Generalized cryptographic hash interface, that you can use with cryptographic hash algorithm that belong to the HashAlgorithm type class.
import Crypto.Hash sha1 :: ByteString -> Digest SHA1 sha1 = hash hexSha3_512 :: ByteString -> String hexSha3_512 bs = show (hash bs :: Digest SHA3_512)
Synopsis
- data Context a
- data Digest a
- digestFromByteString :: (HashAlgorithm a, ByteArrayAccess ba) => ba -> Maybe (Digest a)
- hashInitWith :: HashAlgorithm alg => alg -> Context alg
- hashWith :: (ByteArrayAccess ba, HashAlgorithm alg) => alg -> ba -> Digest alg
- hashPrefixWith :: (ByteArrayAccess ba, HashAlgorithmPrefix alg) => alg -> ba -> Int -> Digest alg
- hashInit :: HashAlgorithm a => Context a
- hashUpdates :: (HashAlgorithm a, ByteArrayAccess ba) => Context a -> [ba] -> Context a
- hashUpdate :: (ByteArrayAccess ba, HashAlgorithm a) => Context a -> ba -> Context a
- hashFinalize :: HashAlgorithm a => Context a -> Digest a
- hashFinalizePrefix :: (HashAlgorithmPrefix a, ByteArrayAccess ba) => Context a -> ba -> Int -> Digest a
- hashBlockSize :: HashAlgorithm a => a -> Int
- hashDigestSize :: HashAlgorithm a => a -> Int
- hash :: (ByteArrayAccess ba, HashAlgorithm a) => ba -> Digest a
- hashPrefix :: (ByteArrayAccess ba, HashAlgorithmPrefix a) => ba -> Int -> Digest a
- hashlazy :: HashAlgorithm a => ByteString -> Digest a
- module Crypto.Hash.Algorithms
Types
Represent a context for a given hash algorithm.
This type is an instance of ByteArrayAccess for debugging purpose. Internal
layout is architecture dependent, may contain uninitialized data fragments,
and change in future versions. The bytearray should not be used as input to
cryptographic algorithms.
A context is not erased when it is finished with. A hash algorithm
buffers its input a block at a time, and finalizing does not clear what is
left there. How much survives depends on where the message ended relative
to the block: with SHA-256, a 32-byte message is still in the context in
full afterwards, and a 100-byte one leaves its last 36 bytes.
hashFinalize works on a copy, so the caller's own context keeps what it
had as well. Nothing clears either of them: this is Bytes rather than
ScrubbedBytes, and the C clears nothing. They go to the garbage
collector as they are, and a core file, a crash dump or a swapped page can
carry them away afterwards.
That is a deliberate trade rather than an oversight, and there is no way to ask for the other side of it: no operation here clears a context. Scrubbing them all was measured at about 70% of a 32-byte hash and a third of an incremental one, because the allocation is most of the work when the message is short -- and short hashes are the common case, in HMAC, in HKDF, and anywhere a key or an identifier is hashed. A 64 KB hash does not notice it. Anything that must not be left in memory this way is better not hashed through this interface at all.
Instances
Represent a digest for a given hash algorithm.
This type is an instance of ByteArrayAccess from package
ram.
Module Data.ByteArray provides many primitives to work with those values
including conversion to other types.
Creating a digest from a bytearray is also possible with function
digestFromByteString.
Instances
| Data a => Data (Digest a) Source # | |
Defined in Crypto.Hash.Types Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Digest a -> c (Digest a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Digest a) # toConstr :: Digest a -> Constr # dataTypeOf :: Digest a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Digest a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Digest a)) # gmapT :: (forall b. Data b => b -> b) -> Digest a -> Digest a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Digest a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Digest a -> r # gmapQ :: (forall d. Data d => d -> u) -> Digest a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Digest a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Digest a -> m (Digest a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Digest a -> m (Digest a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Digest a -> m (Digest a) # | |
| HashAlgorithm a => Read (Digest a) Source # | |
| Show (Digest a) Source # | |
| NFData (Digest a) Source # | |
Defined in Crypto.Hash.Types | |
| Eq (Digest a) Source # | |
| Ord (Digest a) Source # | |
Defined in Crypto.Hash.Types | |
| ByteArrayAccess (Digest a) Source # | |
Functions
digestFromByteString :: (HashAlgorithm a, ByteArrayAccess ba) => ba -> Maybe (Digest a) Source #
Try to transform a bytearray into a Digest of specific algorithm.
If the digest is not the right size for the algorithm specified, then Nothing is returned.
Hash methods parametrized by algorithm
hashInitWith :: HashAlgorithm alg => alg -> Context alg Source #
Initialize a new context for a specified hash algorithm
hashWith :: (ByteArrayAccess ba, HashAlgorithm alg) => alg -> ba -> Digest alg Source #
Run the hash function but takes an explicit hash algorithm parameter
hashPrefixWith :: (ByteArrayAccess ba, HashAlgorithmPrefix alg) => alg -> ba -> Int -> Digest alg Source #
Run the hashPrefix function but takes an explicit hash algorithm parameter
Hash methods
hashInit :: HashAlgorithm a => Context a Source #
Initialize a new context for this hash algorithm
hashUpdates :: (HashAlgorithm a, ByteArrayAccess ba) => Context a -> [ba] -> Context a Source #
Update the context with a list of strict bytestring, and return a new context with the updates.
hashUpdate :: (ByteArrayAccess ba, HashAlgorithm a) => Context a -> ba -> Context a Source #
run hashUpdates on one single bytestring and return the updated context.
hashFinalize :: HashAlgorithm a => Context a -> Digest a Source #
Finalize a context and return a digest.
hashFinalizePrefix :: (HashAlgorithmPrefix a, ByteArrayAccess ba) => Context a -> ba -> Int -> Digest a Source #
Update the context with the first N bytes of a bytestring and return the
digest. The code path is independent from N but much slower than a normal
hashUpdate. The function can be called for the last bytes of a message, in
order to exclude a variable padding, without leaking the padding length. The
begining of the message, never impacted by the padding, should preferably go
through hashUpdate for better performance.
hashBlockSize :: HashAlgorithm a => a -> Int Source #
Get the block size of a hash algorithm
hashDigestSize :: HashAlgorithm a => a -> Int Source #
Get the digest size of a hash algorithm
hash :: (ByteArrayAccess ba, HashAlgorithm a) => ba -> Digest a Source #
Hash a strict bytestring into a digest.
hashPrefix :: (ByteArrayAccess ba, HashAlgorithmPrefix a) => ba -> Int -> Digest a Source #
Hash the first N bytes of a bytestring, with code path independent from N.
hashlazy :: HashAlgorithm a => ByteString -> Digest a Source #
Hash a lazy bytestring into a digest.
Hash algorithms
module Crypto.Hash.Algorithms