Skip to content

Hash Functions

Cryptographic hash functions for data integrity, digital signatures, and content addressing.

Available Hash Functions

BLAKE3

Recommended - Its designers describe it as much faster than MD5, SHA-1, SHA-2, SHA-3 and BLAKE2

  • 128-bit security target
  • Parallel tree hashing
  • Extendable output, 1 to 1,024 bytes in this library
  • Keyed hashing (MAC mode)
  • Key derivation (KDF mode)

BLAKE2b / BLAKE2s

Specified in RFC 7693 (Informational)

  • BLAKE2b: optimised for 64-bit platforms (up to 512-bit output)
  • BLAKE2s: optimised for 8- to 32-bit platforms (up to 256-bit output)
  • Built-in keyed mode (MAC without HMAC)
  • Its designers describe it as faster than MD5, SHA-1, SHA-2 and SHA-3

SHA-256

Standard hash function

  • 128-bit collision and 256-bit preimage resistance
  • Specified in FIPS 180-4
  • Intel SHA extensions, ARMv8 and Power8 code paths in this library

SHA-512

Large output hash function

  • 512-bit output, 64-bit words
  • 256-bit collision and 512-bit preimage resistance

SHA-3

NIST standard based on KECCAK

  • Multiple output sizes (224, 256, 384, 512)
  • Sponge construction, different from SHA-2
  • Specified in FIPS 202

Legacy Hash Functions

These hash functions are broken for collision resistance. Only use them for legacy system compatibility or for checksums that do not need collision resistance. For new applications, use SHA-256, SHA-3, or BLAKE3.

SHA-1 (deprecated)

First collision computed in 2017

  • 160-bit output
  • Chosen-prefix collisions computed in 2020
  • NIST says it should be phased out by 31 December 2030
  • Only for legacy compatibility

MD5 (broken)

First collisions published in 2004

  • 128-bit output
  • Collisions in 10 seconds or less on a 2.6 GHz Pentium 4 (2007)
  • In Weak:: namespace
  • Never use for security

Common Interface

All hash functions derive from HashTransformation and share its interface:

class HashTransformation : public Algorithm {
public:
    // Get algorithm name (from Algorithm)
    virtual std::string AlgorithmName() const;

    // Get digest size
    virtual unsigned int DigestSize() const = 0;

    // Update hash with data
    virtual void Update(const byte* input, size_t length) = 0;

    // Finalize and get result; the object restarts afterwards
    virtual void TruncatedFinal(byte* digest, size_t digestSize) = 0;

    // Reset to initial state
    virtual void Restart();
};

Quick Reference

AlgorithmOutput SizeCollision ResistanceStatus
BLAKE332 bytes (1 to 1,024 configurable)128-bitRecommended
BLAKE2b64 bytes (1 to 64 configurable)256-bitRFC 7693 (Informational)
BLAKE2s32 bytes (1 to 32 configurable)128-bitRFC 7693 (Informational)
SHA-25632 bytes128-bitFIPS 180-4
SHA-51264 bytes256-bitFIPS 180-4
SHA3-25632 bytes128-bitFIPS 202
SHA-120 bytesBelow 80-bitLegacy only
MD516 bytesBrokenLegacy only

The BLAKE2 figures are RFC 7693’s collision security for the full-length digests. Collision resistance is about half the output length. Preimage resistance is about the full output length for SHA-2 and SHA-3. See Security Levels Explained for BLAKE3’s stated targets.

See Also

References

  1. BLAKE3 README
  2. BLAKE3 specification
  3. RFC 7693: The BLAKE2 Cryptographic Hash and Message Authentication Code (MAC)
  4. BLAKE2 home page
  5. NIST: Hash Functions
  6. FIPS 180-4: Secure Hash Standard (SHS)
  7. FIPS 202: SHA-3 Standard: Permutation-Based Hash and Extendable-Output Functions
  8. NIST SP 800-131A Revision 2
  9. RFC 6151: Updated Security Considerations for the MD5 Message-Digest and the HMAC-MD5 Algorithms
  10. Stevens, Bursztein, Karpman, Albertini and Markov: The first collision for full SHA-1 (IACR ePrint 2017/190)
  11. Leurent and Peyrin: SHA-1 is a Shambles (IACR ePrint 2020/014)
  12. NIST: NIST Retires SHA-1 Cryptographic Algorithm (15 December 2022)