HORIZON HASKELLDocslts/ghc-9.14.x45dd5372026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · 45dd537 · 2026-10-11

Modulecrypton-2.1.7Haskell2010

Crypto.Hash

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)
  • 2 types
  • 12 values
  • Packagecrypton-2.1.7
  • Exports16
  • LanguageHaskell2010
  • LicenceBSD-3-Clause AND MIT AND ISC
  • SourceHash.hs

Types

2 declarations
newtypenewtype Context a
#

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.

Instances2NFData, ByteArrayAccess
newtypenewtype Digest a
#

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.

Instances7Eq, Data, Ord, Read, Show, NFData, …

Functions

1 declaration

Hash methods parametrized by algorithm

3 declarations

Hash methods

10 declarations

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.

Hash algorithms

0 declarations