MyNixOS website logo
Description

collection of crypto hashes, fast, pure and practical.

DEPRECATED: this library is still fully functional, but please use cryptonite for new projects and convert old one to use cryptonite. This is where things are at nowadays.

A collection of crypto hashes, with a practical incremental and one-pass, pure APIs, with performance close to the fastest implementations available in other languages.

The implementations are made in C with a haskell FFI wrapper that hide the C implementation.

Simple examples using the unified API:

import Crypto.Hash

sha1 :: ByteString -> Digest SHA1
sha1 = hash

hexSha3_512 :: ByteString -> String
hexSha3_512 bs = show (hash bs :: Digest SHA3_512)

Simple examples using the module API:

import qualified Crypto.Hash.SHA1 as SHA1

main = putStrLn $ show $ SHA1.hash (Data.ByteString.pack [0..255])
import qualified Crypto.Hash.SHA3 as SHA3

main = putStrLn $ show $ digest
  where digest = SHA3.finalize ctx
        ctx    = foldl' SHA3.update iCtx (map Data.ByteString.pack [ [1,2,3], [4,5,6] ]
        iCtx   = SHA3.init 224

:warning: this library is now deprecated in favor of cryptonite, which is a superset of this, and also have more functionality. For new projects, you should not use this library anymore, and preferably you should convert old projects to cryptonite too.

:warning: the SHA3 implementation available in Crypto.Hash.SHA3 is not SHA3 as standardized by NIST, but Keccak as submitted for the SHA3 contest. A matching implementation is available as Keccak in cryptonite, although I would recommend not to use unless you happens to really really need your digest value to be compatible. On the other hand, the centralized Crypto.Hash export a proper SHA3 implementation (as standardized by NIST)

CryptoHash

hs-cryptohash provides many different secure digest algorithms, also called cryptographic hash functions or, simply, cryptohashes. The package exports common hash functions, as well as some more exotic ones, and provides a single API for them all.

The general performance is comparable to the most optimised implementations available.

Here is the complete list of supported algorithms:

  • 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>

We recommend using import qualified because the APIs are similar and many of the modules reuse the same names. However, if you are ony using one module, there is no need to qualify the names.

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

Performance

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

Metadata

Version

0.11.9

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows