Crypto.Nettle.Hash

nettle-0.2.0: safe nettle binding

Copyright | (c) 2013 Stefan Bühler
License | MIT-style (see the file COPYING)
Maintainer | stbuehler@web.de
Stability | experimental
Portability | portable
Safe Haskell | None
Language | Haskell98

Crypto.Nettle.Hash

Contents

- HashAlgorithm class
- hash algorithms
  - GOSTHASH94
  - MD family
  - RIPEMD160
  - SHA1
  - SHA2 family
  - SHA3 family

Description

This module exports hash algorithms supported by nettle: http://www.lysator.liu.se/~nisse/nettle/

Synopsis

- class HashAlgorithm a where
- hash :: HashAlgorithm a => ByteString -> Tagged a ByteString
- hash' :: HashAlgorithm a => a -> ByteString -> ByteString
- hashLazy :: HashAlgorithm a => ByteString -> Tagged a ByteString
- hashLazy' :: HashAlgorithm a => a -> ByteString -> ByteString
- data GOSTHASH94
- data MD2
- data MD4
- data MD5
- data RIPEMD160
- data SHA1
- data SHA224
- data SHA256
- data SHA384
- data SHA512
- data SHA3_224
- data SHA3_256
- data SHA3_384
- data SHA3_512

# HashAlgorithm class

class HashAlgorithm a where #

`HashAlgorithm` is a class that hash algorithms will implement. generating a digest is a 3 step procedure:

- `hashInit` to create a new context
- `hashUpdate` to hash data
- `hashFinalize` to extract the final digest

The final digest has `hashDigestSize` bytes, and the algorithm uses `hashBlockSize` as internal block size.

Minimal complete definition

hashBlockSize, hashDigestSize, hashName, hashInit, hashUpdate, hashFinalize

Methods

hashBlockSize :: Tagged a Int #

Block size in bytes the hash algorithm operates on

hashDigestSize :: Tagged a Int #

Digest size in bytes the hash algorithm returns

hashName :: Tagged a String #

Name of the hash algorithm

hashInit :: a #

Initialize a new context for this hash algorithm

hashUpdate :: a -> ByteString -> a #

Update the context with bytestring, and return a new context with the updates.

hashUpdateLazy :: a -> ByteString -> a #

Update the context with a lazy bytestring, and return a new context with the updates.

hashFinalize :: a -> ByteString #

Finalize a context and return a digest.

hashHMAC :: ByteString -> Tagged a KeyedHash #

Use `HashAlgorithm` for HMAC; can use a optimized variant or the default `hmacInit` one

Instances

HashAlgorithm SHA3_512 # |
Methods hashBlockSize :: Tagged * SHA3_512 Int # hashDigestSize :: Tagged * SHA3_512 Int # hashName :: Tagged * SHA3_512 String # hashInit :: SHA3_512 # hashUpdate :: SHA3_512 -> ByteString -> SHA3_512 # hashUpdateLazy :: SHA3_512 -> ByteString -> SHA3_512 # hashFinalize :: SHA3_512 -> ByteString # hashHMAC :: ByteString -> Tagged * SHA3_512 KeyedHash #
HashAlgorithm SHA3_384 # |
Methods hashBlockSize :: Tagged * SHA3_384 Int # hashDigestSize :: Tagged * SHA3_384 Int # hashName :: Tagged * SHA3_384 String # hashInit :: SHA3_384 # hashUpdate :: SHA3_384 -> ByteString -> SHA3_384 # hashUpdateLazy :: SHA3_384 -> ByteString -> SHA3_384 # hashFinalize :: SHA3_384 -> ByteString # hashHMAC :: ByteString -> Tagged * SHA3_384 KeyedHash #
HashAlgorithm SHA3_256 # |
Methods hashBlockSize :: Tagged * SHA3_256 Int # hashDigestSize :: Tagged * SHA3_256 Int # hashName :: Tagged * SHA3_256 String # hashInit :: SHA3_256 # hashUpdate :: SHA3_256 -> ByteString -> SHA3_256 # hashUpdateLazy :: SHA3_256 -> ByteString -> SHA3_256 # hashFinalize :: SHA3_256 -> ByteString # hashHMAC :: ByteString -> Tagged * SHA3_256 KeyedHash #
HashAlgorithm SHA3_224 # |
Methods hashBlockSize :: Tagged * SHA3_224 Int # hashDigestSize :: Tagged * SHA3_224 Int # hashName :: Tagged * SHA3_224 String # hashInit :: SHA3_224 # hashUpdate :: SHA3_224 -> ByteString -> SHA3_224 # hashUpdateLazy :: SHA3_224 -> ByteString -> SHA3_224 # hashFinalize :: SHA3_224 -> ByteString # hashHMAC :: ByteString -> Tagged * SHA3_224 KeyedHash #
HashAlgorithm SHA512 # |
Methods hashBlockSize :: Tagged * SHA512 Int # hashDigestSize :: Tagged * SHA512 Int # hashName :: Tagged * SHA512 String # hashInit :: SHA512 # hashUpdate :: SHA512 -> ByteString -> SHA512 # hashUpdateLazy :: SHA512 -> ByteString -> SHA512 # hashFinalize :: SHA512 -> ByteString # hashHMAC :: ByteString -> Tagged * SHA512 KeyedHash #
HashAlgorithm SHA384 # |
Methods hashBlockSize :: Tagged * SHA384 Int # hashDigestSize :: Tagged * SHA384 Int # hashName :: Tagged * SHA384 String # hashInit :: SHA384 # hashUpdate :: SHA384 -> ByteString -> SHA384 # hashUpdateLazy :: SHA384 -> ByteString -> SHA384 # hashFinalize :: SHA384 -> ByteString # hashHMAC :: ByteString -> Tagged * SHA384 KeyedHash #
HashAlgorithm SHA256 # |
Methods hashBlockSize :: Tagged * SHA256 Int # hashDigestSize :: Tagged * SHA256 Int # hashName :: Tagged * SHA256 String # hashInit :: SHA256 # hashUpdate :: SHA256 -> ByteString -> SHA256 # hashUpdateLazy :: SHA256 -> ByteString -> SHA256 # hashFinalize :: SHA256 -> ByteString # hashHMAC :: ByteString -> Tagged * SHA256 KeyedHash #
HashAlgorithm SHA224 # |
Methods hashBlockSize :: Tagged * SHA224 Int # hashDigestSize :: Tagged * SHA224 Int # hashName :: Tagged * SHA224 String # hashInit :: SHA224 # hashUpdate :: SHA224 -> ByteString -> SHA224 # hashUpdateLazy :: SHA224 -> ByteString -> SHA224 # hashFinalize :: SHA224 -> ByteString # hashHMAC :: ByteString -> Tagged * SHA224 KeyedHash #
HashAlgorithm SHA1 # |
Methods hashBlockSize :: Tagged * SHA1 Int # hashDigestSize :: Tagged * SHA1 Int # hashName :: Tagged * SHA1 String # hashInit :: SHA1 # hashUpdate :: SHA1 -> ByteString -> SHA1 # hashUpdateLazy :: SHA1 -> ByteString -> SHA1 # hashFinalize :: SHA1 -> ByteString # hashHMAC :: ByteString -> Tagged * SHA1 KeyedHash #
HashAlgorithm RIPEMD160 # |
Methods hashBlockSize :: Tagged * RIPEMD160 Int # hashDigestSize :: Tagged * RIPEMD160 Int # hashName :: Tagged * RIPEMD160 String # hashInit :: RIPEMD160 # hashUpdate :: RIPEMD160 -> ByteString -> RIPEMD160 # hashUpdateLazy :: RIPEMD160 -> ByteString -> RIPEMD160 # hashFinalize :: RIPEMD160 -> ByteString # hashHMAC :: ByteString -> Tagged * RIPEMD160 KeyedHash #
HashAlgorithm MD5 # |
Methods hashBlockSize :: Tagged * MD5 Int # hashDigestSize :: Tagged * MD5 Int # hashName :: Tagged * MD5 String # hashInit :: MD5 # hashUpdate :: MD5 -> ByteString -> MD5 # hashUpdateLazy :: MD5 -> ByteString -> MD5 # hashFinalize :: MD5 -> ByteString # hashHMAC :: ByteString -> Tagged * MD5 KeyedHash #
HashAlgorithm MD4 # |
Methods hashBlockSize :: Tagged * MD4 Int # hashDigestSize :: Tagged * MD4 Int # hashName :: Tagged * MD4 String # hashInit :: MD4 # hashUpdate :: MD4 -> ByteString -> MD4 # hashUpdateLazy :: MD4 -> ByteString -> MD4 # hashFinalize :: MD4 -> ByteString # hashHMAC :: ByteString -> Tagged * MD4 KeyedHash #
HashAlgorithm MD2 # |
Methods hashBlockSize :: Tagged * MD2 Int # hashDigestSize :: Tagged * MD2 Int # hashName :: Tagged * MD2 String # hashInit :: MD2 # hashUpdate :: MD2 -> ByteString -> MD2 # hashUpdateLazy :: MD2 -> ByteString -> MD2 # hashFinalize :: MD2 -> ByteString # hashHMAC :: ByteString -> Tagged * MD2 KeyedHash #
HashAlgorithm GOSTHASH94 # |
Methods hashBlockSize :: Tagged * GOSTHASH94 Int # hashDigestSize :: Tagged * GOSTHASH94 Int # hashName :: Tagged * GOSTHASH94 String # hashInit :: GOSTHASH94 # hashUpdate :: GOSTHASH94 -> ByteString -> GOSTHASH94 # hashUpdateLazy :: GOSTHASH94 -> ByteString -> GOSTHASH94 # hashFinalize :: GOSTHASH94 -> ByteString # hashHMAC :: ByteString -> Tagged * GOSTHASH94 KeyedHash #

hash :: HashAlgorithm a => ByteString -> Tagged a ByteString #

Helper to hash a single (strict) `ByteString` in one step.

Example:

    untag (hash (fromString "abc") :: Tagged SHA256 B.ByteString)

hash' :: HashAlgorithm a => a -> ByteString -> ByteString #

Untagged variant of `hash`; takes a (possible `undefined`) typed `HashAlgorithm` context as parameter.

Example:

    hash' (undefined :: SHA256) $ fromString "abc"

hashLazy :: HashAlgorithm a => ByteString -> Tagged a ByteString #

Helper to hash a single (lazy) `ByteString` in one step.

Example:

    untag (hashLazy (fromString "abc") :: Tagged SHA256 L.ByteString)

hashLazy' :: HashAlgorithm a => a -> ByteString -> ByteString #

Untagged variant of `hashLazy`; takes a (possible `undefined`) typed `HashAlgorithm` context as parameter.

Example:

    hashLazy' (undefined :: SHA256) $ fromString "abc"

# hash algorithms

Only members of the SHA2 and SHA3 family have no known weaknesses (according to http://www.lysator.liu.se/~nisse/nettle/nettle.html#Hash-functions)

## GOSTHASH94

data GOSTHASH94 #

The GOST94 or GOST R 34.11-94 hash algorithm is a Soviet-era algorithm used in Russian government standards (see RFC 4357). It outputs message digests of 32 bytes (256 bits).

Instances

HashAlgorithm GOSTHASH94 # |
Methods hashBlockSize :: Tagged * GOSTHASH94 Int # hashDigestSize :: Tagged * GOSTHASH94 Int # hashName :: Tagged * GOSTHASH94 String # hashInit :: GOSTHASH94 # hashUpdate :: GOSTHASH94 -> ByteString -> GOSTHASH94 # hashUpdateLazy :: GOSTHASH94 -> ByteString -> GOSTHASH94 # hashFinalize :: GOSTHASH94 -> ByteString # hashHMAC :: ByteString -> Tagged * GOSTHASH94 KeyedHash #

## MD family

data MD2 #

`MD2` is a hash function of Ronald Rivest's, described in RFC 1319. It outputs message digests of 16 bytes (128 bits).

Instances

HashAlgorithm MD2 # |
Methods hashBlockSize :: Tagged * MD2 Int # hashDigestSize :: Tagged * MD2 Int # hashName :: Tagged * MD2 String # hashInit :: MD2 # hashUpdate :: MD2 -> ByteString -> MD2 # hashUpdateLazy :: MD2 -> ByteString -> MD2 # hashFinalize :: MD2 -> ByteString # hashHMAC :: ByteString -> Tagged * MD2 KeyedHash #

data MD4 #

`MD4` is a hash function of Ronald Rivest's, described in RFC 1320. It outputs message digests of 16 bytes (128 bits).

Instances

HashAlgorithm MD4 # |
Methods hashBlockSize :: Tagged * MD4 Int # hashDigestSize :: Tagged * MD4 Int # hashName :: Tagged * MD4 String # hashInit :: MD4 # hashUpdate :: MD4 -> ByteString -> MD4 # hashUpdateLazy :: MD4 -> ByteString -> MD4 # hashFinalize :: MD4 -> ByteString # hashHMAC :: ByteString -> Tagged * MD4 KeyedHash #

data MD5 #

`MD5` is a hash function of Ronald Rivest's, described in RFC 1321. It outputs message digests of 16 bytes (128 bits).

Instances

HashAlgorithm MD5 # |
Methods hashBlockSize :: Tagged * MD5 Int # hashDigestSize :: Tagged * MD5 Int # hashName :: Tagged * MD5 String # hashInit :: MD5 # hashUpdate :: MD5 -> ByteString -> MD5 # hashUpdateLazy :: MD5 -> ByteString -> MD5 # hashFinalize :: MD5 -> ByteString # hashHMAC :: ByteString -> Tagged * MD5 KeyedHash #

## RIPEMD160

data RIPEMD160 #

`RIPEMD160` is a hash function designed by Hans Dobbertin, Antoon Bosselaers, and Bart Preneel, as a strengthened version of RIPEMD. It produces message digests of 20 bytes (160 bits).

Instances

HashAlgorithm RIPEMD160 # |
Methods hashBlockSize :: Tagged * RIPEMD160 Int # hashDigestSize :: Tagged * RIPEMD160 Int # hashName :: Tagged * RIPEMD160 String # hashInit :: RIPEMD160 # hashUpdate :: RIPEMD160 -> ByteString -> RIPEMD160 # hashUpdateLazy :: RIPEMD160 -> ByteString -> RIPEMD160 # hashFinalize :: RIPEMD160 -> ByteString # hashHMAC :: ByteString -> Tagged * RIPEMD160 KeyedHash #

## SHA1

data SHA1 #

`SHA1` is a hash function specified by NIST (The U.S. National Institute for Standards and Technology). It produces message digests of 20 bytes (160 bits).

Instances

HashAlgorithm SHA1 # |
Methods hashBlockSize :: Tagged * SHA1 Int # hashDigestSize :: Tagged * SHA1 Int # hashName :: Tagged * SHA1 String # hashInit :: SHA1 # hashUpdate :: SHA1 -> ByteString -> SHA1 # hashUpdateLazy :: SHA1 -> ByteString -> SHA1 # hashFinalize :: SHA1 -> ByteString # hashHMAC :: ByteString -> Tagged * SHA1 KeyedHash #

## SHA2 family

The SHA2 family supports digests lengths of 28, 32, 48 or 64 bytes (224, 256, 384, 512 bits), and the variants are named after the bit length.

The SHA2 family of hash functions were specified by NIST, intended as a replacement for `SHA1`.

data SHA224 #

`SHA224` is a member of the SHA2 family which outputs messages digests of 28 bytes (224 bits).

Instances

HashAlgorithm SHA224 # |
Methods hashBlockSize :: Tagged * SHA224 Int # hashDigestSize :: Tagged * SHA224 Int # hashName :: Tagged * SHA224 String # hashInit :: SHA224 # hashUpdate :: SHA224 -> ByteString -> SHA224 # hashUpdateLazy :: SHA224 -> ByteString -> SHA224 # hashFinalize :: SHA224 -> ByteString # hashHMAC :: ByteString -> Tagged * SHA224 KeyedHash #

data SHA256 #

`SHA256` is a member of the SHA2 family which outputs messages digests of 32 bytes (256 bits).

Instances

HashAlgorithm SHA256 # |
Methods hashBlockSize :: Tagged * SHA256 Int # hashDigestSize :: Tagged * SHA256 Int # hashName :: Tagged * SHA256 String # hashInit :: SHA256 # hashUpdate :: SHA256 -> ByteString -> SHA256 # hashUpdateLazy :: SHA256 -> ByteString -> SHA256 # hashFinalize :: SHA256 -> ByteString # hashHMAC :: ByteString -> Tagged * SHA256 KeyedHash #

data SHA384 #

`SHA384` is a member of the SHA2 family which outputs messages digests of 48 bytes (384 bits).

Instances

HashAlgorithm SHA384 # |
Methods hashBlockSize :: Tagged * SHA384 Int # hashDigestSize :: Tagged * SHA384 Int # hashName :: Tagged * SHA384 String # hashInit :: SHA384 # hashUpdate :: SHA384 -> ByteString -> SHA384 # hashUpdateLazy :: SHA384 -> ByteString -> SHA384 # hashFinalize :: SHA384 -> ByteString # hashHMAC :: ByteString -> Tagged * SHA384 KeyedHash #

data SHA512 #

`SHA512` is a member of the SHA2 family which outputs messages digests of 64 bytes (512 bits).

Instances

HashAlgorithm SHA512 # |
Methods hashBlockSize :: Tagged * SHA512 Int # hashDigestSize :: Tagged * SHA512 Int # hashName :: Tagged * SHA512 String # hashInit :: SHA512 # hashUpdate :: SHA512 -> ByteString -> SHA512 # hashUpdateLazy :: SHA512 -> ByteString -> SHA512 # hashFinalize :: SHA512 -> ByteString # hashHMAC :: ByteString -> Tagged * SHA512 KeyedHash #

## SHA3 family

The SHA3 family supports (like SHA2) digests lengths of 28, 32, 48 or 64 bytes (224, 256, 384, 512 bits), and the variants are named after the bit length.

The SHA3 hash functions were specified by NIST in response to weaknesses in SHA1, and doubts about SHA2 hash functions which structurally are very similar to SHA1. The standard is a result of a competition, where the winner, also known as Keccak, was designed by Guido Bertoni, Joan Daemen, Michaël Peeters and Gilles Van Assche. It is structurally very different from all widely used earlier hash functions.

data SHA3_224 #

`SHA3_224` is a member of the SHA3 family which outputs messages digests of 28 bytes (224 bits).

Instances

HashAlgorithm SHA3_224 # |
Methods hashBlockSize :: Tagged * SHA3_224 Int # hashDigestSize :: Tagged * SHA3_224 Int # hashName :: Tagged * SHA3_224 String # hashInit :: SHA3_224 # hashUpdate :: SHA3_224 -> ByteString -> SHA3_224 # hashUpdateLazy :: SHA3_224 -> ByteString -> SHA3_224 # hashFinalize :: SHA3_224 -> ByteString # hashHMAC :: ByteString -> Tagged * SHA3_224 KeyedHash #

data SHA3_256 #

`SHA3_256` is a member of the SHA3 family which outputs messages digests of 32 bytes (256 bits).

Instances

HashAlgorithm SHA3_256 # |
Methods hashBlockSize :: Tagged * SHA3_256 Int # hashDigestSize :: Tagged * SHA3_256 Int # hashName :: Tagged * SHA3_256 String # hashInit :: SHA3_256 # hashUpdate :: SHA3_256 -> ByteString -> SHA3_256 # hashUpdateLazy :: SHA3_256 -> ByteString -> SHA3_256 # hashFinalize :: SHA3_256 -> ByteString # hashHMAC :: ByteString -> Tagged * SHA3_256 KeyedHash #

data SHA3_384 #

`SHA3_384` is a member of the SHA3 family which outputs messages digests of 48 bytes (384 bits).

Instances

HashAlgorithm SHA3_384 # |
Methods hashBlockSize :: Tagged * SHA3_384 Int # hashDigestSize :: Tagged * SHA3_384 Int # hashName :: Tagged * SHA3_384 String # hashInit :: SHA3_384 # hashUpdate :: SHA3_384 -> ByteString -> SHA3_384 # hashUpdateLazy :: SHA3_384 -> ByteString -> SHA3_384 # hashFinalize :: SHA3_384 -> ByteString # hashHMAC :: ByteString -> Tagged * SHA3_384 KeyedHash #

data SHA3_512 #

`SHA3_512` is a member of the SHA3 family which outputs messages digests of 64 bytes (512 bits).

Instances

HashAlgorithm SHA3_512 # |
Methods hashBlockSize :: Tagged * SHA3_512 Int # hashDigestSize :: Tagged * SHA3_512 Int # hashName :: Tagged * SHA3_512 String # hashInit :: SHA3_512 # hashUpdate :: SHA3_512 -> ByteString -> SHA3_512 # hashUpdateLazy :: SHA3_512 -> ByteString -> SHA3_512 # hashFinalize :: SHA3_512 -> ByteString # hashHMAC :: ByteString -> Tagged * SHA3_512 KeyedHash #