collection of crypto hashes, fast, pure and practical

Version on this page:0.11.6
LTS Haskell 22.22:0.11.9
Stackage Nightly 2024-05-19:0.11.9
Latest on Hackage:0.11.9

See all snapshots cryptohash appears in

BSD-3-Clause licensed and maintained by Vincent Hanquez
This version can be pinned in stack with:cryptohash-0.11.6@sha256:48e2a1fa9f757055d6f303e275fb8d3aa8a1c5684ae829794dce2fbd3db5cfac,4496

Module documentation for 0.11.6

  • Crypto
    • Crypto.Hash
      • Crypto.Hash.MD2
      • Crypto.Hash.MD4
      • Crypto.Hash.MD5
      • Crypto.Hash.RIPEMD160
      • Crypto.Hash.SHA1
      • Crypto.Hash.SHA224
      • Crypto.Hash.SHA256
      • Crypto.Hash.SHA3
      • Crypto.Hash.SHA384
      • Crypto.Hash.SHA512
      • Crypto.Hash.SHA512t
      • Crypto.Hash.Skein256
      • Crypto.Hash.Skein512
      • Crypto.Hash.Tiger
      • Crypto.Hash.Types
      • Crypto.Hash.Whirlpool
    • Crypto.MAC
      • Crypto.MAC.HMAC
      • Crypto.MAC.SHA3


hs-cryptohash provides lots of different secure digest algorithms, also called cryptohashes. It exposes every common hashes, but also some more exotic hashes, provides an extensive list of hashes available with a consistant API.

The general performance are comparable to the most optimised hashes available.

The complete list of supported hashes:

  • MD2, MD4, MD5
  • RIPEMD160
  • SHA1
  • SHA-2 family: 224, 256, 384, 512 and the newer 512t
  • SHA-3 (aka Keccak)
  • Skein: 256, 512
  • Tiger
  • Whirlpool

You can easily import any hash with the following:

import qualified Crypto.Hash.<HASH> as <Hash>

suggestion: it’s easier to import qualified since there’s a collision with the init symbol, but for only importing the hash or hashlazy function there’s no such need.

Every hashes, exposes a very similar API.

Incremental API

it’s based on 4 different functions, similar to the lowlevel operations of a typical hash:

  • init: create a new hash context
  • update: update non-destructively a new hash context with a strict bytestring
  • updates: same as update, except that it takes a list of strict bytestring
  • finalize: finalize the context and returns a digest bytestring.

all those operations are completely pure, and instead of changing the context as usual in others language, it re-allocates a new context each time.

One Pass API

The one pass API use the incremental API under the hood, but expose common operations to create digests out of a bytestring and lazy bytestring.

  • hash: create a digest (init+update+finalize) from a strict bytestring
  • hashlazy: create a digest (init+update+finalize) from a lazy bytestring

More Type safety

A more type safe API is also available from Crypto.Hash. The API provides all the supported hashes in the same namespace, through unified functions.

It introduces 2 new types, the Context type and the Digest type. Both those types are parametrized with the HashAlgorithm used.

The API is very similar to each single hash module, except the types are slightly different.

import Crypto.Hash

-- use the incremental API to hash the byte [1,2,3] with SHA1
-- and print the hexadecimal digest.
example1 = do
    let ctx = hashInit
        ctx' = hashUpdates ctx [ Data.ByteString.pack [1,2,3] ]
        dgt  = hashFinalize ctx' :: Digest SHA1
    putStrLn $ show dgt

-- use the one-pass API to hash the byte 1,2,3 with SHA3_512
-- and print the hexadecimal digest.
example2 = do
    let dgt  = hash (Data.ByteString.pack [1,2,3]) :: Digest SHA3_512
    putStrLn $ show dgt


Cryptohash uses C implementations to provide maximum performance. see the cbits directory for more information