Table of Contents

Class Blake2b

Namespace
CryptoHives.Foundation.Security.Cryptography.Hash
Assembly
CryptoHives.Foundation.Security.Cryptography.dll

Computes the BLAKE2b hash for the input data.

public sealed class Blake2b : HashAlgorithm, ICryptoTransform, IDisposable, IResettable
Inheritance
Blake2b
Implements
Inherited Members

Remarks

This is a fully managed implementation of BLAKE2b that does not rely on OS or hardware cryptographic APIs, ensuring deterministic behavior across all platforms and runtimes.

BLAKE2b is optimized for 64-bit platforms and produces digests from 1 to 64 bytes. The default output size is 64 bytes (512 bits).

BLAKE2b supports an optional key for keyed hashing (MAC mode) with keys up to 64 bytes.

Constructors

Blake2b()

Initializes a new instance of the Blake2b class with default output size (64 bytes).

public Blake2b()

Blake2b(int)

Initializes a new instance of the Blake2b class with specified output size.

public Blake2b(int outputBytes)

Parameters

outputBytes int

The desired output size in bytes (1-64).

Blake2b(int, ReadOnlySpan<byte>)

Initializes a new instance of the Blake2b class with specified output size and key.

public Blake2b(int outputBytes, ReadOnlySpan<byte> key)

Parameters

outputBytes int

The desired output size in bytes (1-64).

key ReadOnlySpan<byte>

The optional key for keyed hashing (MAC mode). Must be 0-MaxKeySizeBytes bytes.

Fields

BlockSizeBytes

The block size in bytes.

public const int BlockSizeBytes = 128

Field Value

int

MaxHashSizeBits

The maximum hash size in bits.

public const int MaxHashSizeBits = 512

Field Value

int

MaxHashSizeBytes

The maximum hash size in bytes.

public const int MaxHashSizeBytes = 64

Field Value

int

MaxKeySizeBytes

The maximum key size in bytes.

public const int MaxKeySizeBytes = 64

Field Value

int

Properties

AlgorithmName

Gets the name of the hash algorithm.

public override string AlgorithmName { get; }

Property Value

string

BlockSize

Gets the block size in bytes used by the hash algorithm.

public override int BlockSize { get; }

Property Value

int

IsKeyed

Gets a value indicating whether this instance is configured for keyed hashing (MAC mode).

public bool IsKeyed { get; }

Property Value

bool

Methods

Create()

Creates a new instance of the Blake2b class with default output size.

public static Blake2b Create()

Returns

Blake2b

A new BLAKE2b instance.

Create(int)

Creates a new instance of the Blake2b class with specified output size.

public static Blake2b Create(int outputBytes)

Parameters

outputBytes int

The desired output size in bytes (1-64).

Returns

Blake2b

A new BLAKE2b instance.

CreateKeyed(int, ReadOnlySpan<byte>)

Creates a new keyed instance of the Blake2b class.

public static Blake2b CreateKeyed(int outputBytes, ReadOnlySpan<byte> key)

Parameters

outputBytes int

The desired output size in bytes (1-64).

key ReadOnlySpan<byte>

The key for keyed hashing (MAC mode). Must be 0-MaxKeySizeBytes bytes.

Returns

Blake2b

A new BLAKE2b instance configured for keyed hashing.

CreateKeyed(ReadOnlySpan<byte>)

Creates a new keyed instance of the Blake2b class with default output size.

public static Blake2b CreateKeyed(ReadOnlySpan<byte> key)

Parameters

key ReadOnlySpan<byte>

The key for keyed hashing (MAC mode). Must be 0-MaxKeySizeBytes bytes.

Returns

Blake2b

A new BLAKE2b instance configured for keyed hashing.

Dispose(bool)

Releases the unmanaged resources used by the HashAlgorithm and optionally releases the managed resources.

protected override void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

HashCore(ReadOnlySpan<byte>)

When overridden in a derived class, routes data written to the object into the hash algorithm for computing the hash.

protected override void HashCore(ReadOnlySpan<byte> source)

Parameters

source ReadOnlySpan<byte>

The input to compute the hash code for.

Exceptions

ObjectDisposedException

Thrown when the instance has been disposed.

HashData(in ReadOnlySequence<byte>)

Computes the BLAKE2b hash of source using the default output size (64 bytes) and returns it as a new byte array.

public static byte[] HashData(in ReadOnlySequence<byte> source)

Parameters

source ReadOnlySequence<byte>

The (possibly multi-segment) input sequence to hash.

Returns

byte[]

A new byte array containing the BLAKE2b hash.

HashData(ReadOnlySpan<byte>)

Computes the BLAKE2b hash of source using the default output size (64 bytes) and returns it as a new byte array.

public static byte[] HashData(ReadOnlySpan<byte> source)

Parameters

source ReadOnlySpan<byte>

The input data to hash.

Returns

byte[]

A new byte array containing the BLAKE2b hash.

Initialize()

Initializes an implementation of the HashAlgorithm class.

public override void Initialize()

Exceptions

ObjectDisposedException

Thrown when the instance has been disposed.

TryHashData(in ReadOnlySequence<byte>, Span<byte>, out int)

Computes the BLAKE2b hash of source using the default output size (64 bytes) and writes it into destination.

public static bool TryHashData(in ReadOnlySequence<byte> source, Span<byte> destination, out int bytesWritten)

Parameters

source ReadOnlySequence<byte>

The (possibly multi-segment) input sequence to hash.

destination Span<byte>

The buffer to receive the hash value. Must be at least MaxHashSizeBytes bytes.

bytesWritten int

When this method returns, the number of bytes written into destination.

Returns

bool

true if destination was large enough; otherwise, false.

TryHashData(ReadOnlySpan<byte>, Span<byte>, out int)

Computes the BLAKE2b hash of source using the default output size (64 bytes) and writes it into destination.

public static bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)

Parameters

source ReadOnlySpan<byte>

The input data to hash.

destination Span<byte>

The buffer to receive the hash value. Must be at least MaxHashSizeBytes bytes.

bytesWritten int

When this method returns, the number of bytes written into destination.

Returns

bool

true if destination was large enough; otherwise, false.

TryHashFinal(Span<byte>, out int)

When overridden in a derived class, finalizes the hash computation and writes the result into the provided buffer.

protected override bool TryHashFinal(Span<byte> destination, out int bytesWritten)

Parameters

destination Span<byte>

The target buffer to write the hash to.

bytesWritten int

The number of bytes written into the buffer.

Returns

bool

true if the destination buffer was large enough; otherwise false.

Exceptions

ObjectDisposedException

Thrown when the instance has been disposed.