Geralt
  • Introduction
  • Random data
  • Constant time
  • Secure memory
  • Encoding
  • Padding
  • Hashing
  • Message authentication
  • Password hashing
  • Key derivation
  • Authenticated encryption
    • Stream and file encryption
    • AEGIS-128L
    • AEGIS-256
    • ChaCha20-Poly1305
    • XChaCha20-Poly1305
  • Key exchange
  • Digital signatures
  • Advanced
    • Validation
    • Concat
    • ChaCha20
    • XChaCha20
    • HChaCha20
    • Poly1305
    • Ed25519 to X25519
Powered by GitBook
On this page
  • Purpose
  • Usage
  • ComputeHash
  • ComputeHash
  • IncrementalBLAKE2b
  • Constants
  • Notes

Hashing

Last updated 5 months ago

Purpose

is a cryptographic hash function. It takes a message of any size and produces a 128-bit to 512-bit hash.

This hash acts as a fingerprint for the data. Hashes can be used to uniquely identify messages, detect corruption, detect duplicate data, and index data in a hash table.

However, unkeyed hashes do not provide (e.g. for ). Furthermore, they should be avoided for . Use the linked APIs instead.

BLAKE2b is NOT suitable for hashing passwords. Use instead.

A hash size of at least 256 bits is strongly recommended to obtain collision resistance.

Usage

ComputeHash

Fills a span with a hash computed from a message.

BLAKE2b.ComputeHash(Span<byte> hash, ReadOnlySpan<byte> message)

Exceptions

hash has a length less than MinHashSize or greater than MaxHashSize.

The hash could not be computed.

ComputeHash

Fills a span with a hash computed from a Stream message. This is useful for hashing files.

BLAKE2b.ComputeHash(Span<byte> hash, Stream message)

Exceptions

hash has a length less than MinHashSize or greater than MaxHashSize.

message is null.

message cannot be read.

The hash could not be computed.

IncrementalBLAKE2b

Provides support for computing a hash from several messages.

using var blake2b = new IncrementalBLAKE2b(int hashSize);
blake2b.Update(ReadOnlySpan<byte> message1);
blake2b.Update(ReadOnlySpan<byte> message2);
blake2b.Finalize(Span<byte> hash1);

// Avoid another using statement
blake2b.Reinitialize(int hashSize);
blake2b.Update(ReadOnlySpan<byte> message3);
// Cache the state
blake2b.CacheState();
blake2b.Finalize(Span<byte> hash2);

// Restore the cached state
blake2b.RestoreCachedState();
// hash3 == hash2
blake2b.Finalize(Span<byte> hash3);

Exceptions

hashSize is less than MinHashSize or greater than MaxHashSize.

hash has a length not equal to hashSize.

The hash could not be computed.

Cannot update after finalizing or finalize twice (without reinitializing or restoring a cached state).

Cannot cache the state after finalizing (without reinitializing).

Cannot restore the state when it has not been cached.

The object has been disposed.

Constants

These are used for validation and/or save you defining your own constants.

public const int HashSize = 32;
public const int MinHashSize = 16;
public const int MaxHashSize = 64;

Notes

Do NOT manually truncate a hash. Instead, specify the hash size you want directly. The hash size affects the output, which provides domain separation.

The security level of BLAKE2b is 1/2 the output length (e.g. 128-bit security for a 256-bit hash).​

CacheState() can only cache the state once. Each subsequent call will overwrite the previously cached state. See the for when this method should be used.

Do NOT use ComputeHash() for key derivation. Read the page instead.

Unlike older hash functions (e.g. MD5, SHA-1, SHA-256, and SHA-512), BLAKE2b is immune to .

If you are making multiple calls to IncrementalBLAKE2b with unchanging/static data at the beginning, you can cache the state to improve performance. This allows you to only process this data once. It is more relevant in scenarios, as explained on that page.

BLAKE2b
authentication
Encrypt-then-MAC
key derivation
Argon2id
ArgumentOutOfRangeException
CryptographicException
ArgumentOutOfRangeException
ArgumentNullException
InvalidOperationException
CryptographicException
ArgumentOutOfRangeException
ArgumentOutOfRangeException
CryptographicException
InvalidOperationException
InvalidOperationException
InvalidOperationException
ObjectDisposedException
Key derivation
length extension attacks
message authentication
Notes