Class Blake3
- Namespace
- CryptoHives.Foundation.Security.Cryptography.Hash
- Assembly
- CryptoHives.Foundation.Security.Cryptography.dll
Computes the BLAKE3 hash for the input data.
public sealed class Blake3 : HashAlgorithm, ICryptoTransform, IDisposable, IResettable, IExtendableOutput
- Inheritance
-
Blake3
- Implements
- Inherited Members
Remarks
This is a fully managed implementation of BLAKE3 that does not rely on OS or hardware cryptographic APIs, ensuring deterministic behavior across all platforms and runtimes.
BLAKE3 is a cryptographic hash function that is much faster than SHA-256 while maintaining high security. It supports variable output length (XOF mode).
BLAKE3 supports three modes: standard hashing, keyed hashing (MAC), and key derivation.
Constructors
Blake3()
Initializes a new instance of the Blake3 class with default output size (32 bytes).
public Blake3()
Blake3(int)
Initializes a new instance of the Blake3 class with specified output size.
public Blake3(int outputBytes)
Parameters
outputBytesintThe desired output size in bytes.
Fields
BlockSizeBytes
The block size in bytes.
public const int BlockSizeBytes = 64
Field Value
ChunkSizeBytes
The chunk size in bytes (1024 bytes).
public const int ChunkSizeBytes = 1024
Field Value
DefaultHashSizeBits
The default hash size in bits.
public const int DefaultHashSizeBits = 256
Field Value
DefaultHashSizeBytes
The default hash size in bytes.
public const int DefaultHashSizeBytes = 32
Field Value
KeySizeBytes
The required key size in bytes for keyed hash mode.
public const int KeySizeBytes = 32
Field Value
Properties
AlgorithmName
Gets the name of the hash algorithm.
public override string AlgorithmName { get; }
Property Value
BlockSize
Gets the block size in bytes used by the hash algorithm.
public override int BlockSize { get; }
Property Value
Mode
Gets the mode of operation for this instance.
public Blake3Mode Mode { get; }
Property Value
Methods
Absorb(ReadOnlySpan<byte>)
Absorbs input data into the XOF state.
public void Absorb(ReadOnlySpan<byte> input)
Parameters
inputReadOnlySpan<byte>The input data to absorb.
Exceptions
- InvalidOperationException
Thrown when data is added after output has been squeezed.
Create()
Creates a new instance of the Blake3 class with default output size.
public static Blake3 Create()
Returns
- Blake3
A new BLAKE3 instance.
Create(int)
Creates a new instance of the Blake3 class with specified output size.
public static Blake3 Create(int outputBytes)
Parameters
outputBytesintThe desired output size in bytes.
Returns
- Blake3
A new BLAKE3 instance.
CreateKeyed(ReadOnlySpan<byte>)
Creates a new keyed instance of the Blake3 class.
public static Blake3 CreateKeyed(ReadOnlySpan<byte> key)
Parameters
keyReadOnlySpan<byte>The 32-byte key for keyed hashing.
Returns
- Blake3
A new BLAKE3 instance configured for keyed hashing.
CreateKeyed(ReadOnlySpan<byte>, int)
Creates a new keyed instance of the Blake3 class with specified output size.
public static Blake3 CreateKeyed(ReadOnlySpan<byte> key, int outputBytes)
Parameters
keyReadOnlySpan<byte>The 32-byte key for keyed hashing.
outputBytesintThe desired output size in bytes.
Returns
- Blake3
A new BLAKE3 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
disposingbooltrue 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
sourceReadOnlySpan<byte>The input to compute the hash code for.
Exceptions
- ObjectDisposedException
Thrown when the instance has been disposed.
HashData(in ReadOnlySequence<byte>)
Computes the BLAKE3 hash of source using the default output size (32 bytes)
and returns it as a new byte array.
public static byte[] HashData(in ReadOnlySequence<byte> source)
Parameters
sourceReadOnlySequence<byte>The (possibly multi-segment) input sequence to hash.
Returns
- byte[]
A new byte array containing the BLAKE3 hash.
HashData(ReadOnlySpan<byte>)
Computes the BLAKE3 hash of source using the default output size (32 bytes)
and returns it as a new byte array.
public static byte[] HashData(ReadOnlySpan<byte> source)
Parameters
sourceReadOnlySpan<byte>The input data to hash.
Returns
- byte[]
A new byte array containing the BLAKE3 hash.
Remarks
Uses the dedicated one-shot path (see TryHashOneShot(ReadOnlySpan<byte>, Span<byte>, out int)) instead of the generic streaming pool, so the entire call — including small inputs — skips the incremental chunk-buffer bookkeeping.
Initialize()
Initializes an implementation of the HashAlgorithm class.
public override void Initialize()
Exceptions
- ObjectDisposedException
Thrown when the instance has been disposed.
Reset()
Resets the XOF state so the instance can be reused for a new computation.
public void Reset()
Remarks
After calling this method, the instance is in the same state as a newly constructed one. All previously absorbed data and squeezed output are discarded.
Squeeze(Span<byte>)
Finalizes the hash and squeezes output of the specified length.
public void Squeeze(Span<byte> output)
Parameters
Exceptions
- ObjectDisposedException
Thrown when the instance has been disposed.
TryHashData(in ReadOnlySequence<byte>, Span<byte>, out int)
Computes the BLAKE3 hash of source using the default output size (32 bytes)
and writes it into destination.
public static bool TryHashData(in ReadOnlySequence<byte> source, Span<byte> destination, out int bytesWritten)
Parameters
sourceReadOnlySequence<byte>The (possibly multi-segment) input sequence to hash.
destinationSpan<byte>The buffer to receive the hash value. Must be at least DefaultHashSizeBytes bytes.
bytesWrittenintWhen this method returns, the number of bytes written into
destination.
Returns
TryHashData(ReadOnlySpan<byte>, Span<byte>, out int)
Computes the BLAKE3 hash of source using the default output size (32 bytes)
and writes it into destination.
public static bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
Parameters
sourceReadOnlySpan<byte>The input data to hash.
destinationSpan<byte>The buffer to receive the hash value. Must be at least DefaultHashSizeBytes bytes.
bytesWrittenintWhen this method returns, the number of bytes written into
destination.
Returns
Remarks
Uses the dedicated one-shot path (see TryHashOneShot(ReadOnlySpan<byte>, Span<byte>, out int)) instead of the generic streaming pool, so the entire call — including small inputs — skips the incremental chunk-buffer bookkeeping.
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
destinationSpan<byte>The target buffer to write the hash to.
bytesWrittenintThe number of bytes written into the buffer.
Returns
Exceptions
- ObjectDisposedException
Thrown when the instance has been disposed.
TryHashOneShot(ReadOnlySpan<byte>, Span<byte>, out int)
Computes the BLAKE3 hash of source in a single call using
this instance's SIMD tier, without incremental-hashing bookkeeping.
public bool TryHashOneShot(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
Parameters
sourceReadOnlySpan<byte>The input data to hash.
destinationSpan<byte>The buffer to receive the hash value. Must be at least
HashSize/8 bytes.bytesWrittenintWhen this method returns, the number of bytes written into
destination.
Returns
Remarks
Unlike TryComputeHash(ReadOnlySpan{byte}, Span{byte}, out int),
which always routes through the streaming HashCore/TryHashFinal
pair, this calls a dedicated one-shot path directly — see
TryHashOneShot(ReadOnlySpan<byte>, Span<byte>, out int) for what it skips.
The instance must be freshly constructed or freshly Initialize()d; calling this after Absorb(ReadOnlySpan<byte>) or any streaming write produces incorrect results.
Exceptions
- ObjectDisposedException
Thrown when the instance has been disposed.
TryReset()
Resets this instance to its initial state so it can be returned to an object pool for reuse.
public override bool TryReset()
Returns
- bool
true if the instance was reset and may be returned to the pool; false if it must instead be disposed and discarded (see remarks).
Remarks
This method implements IResettable from
Microsoft.Extensions.ObjectPool, enabling any CryptoHives hash algorithm to be
used with DefaultObjectPool<T> without a custom policy.
Overridden by algorithms that can carry caller-supplied secret material (e.g. a keyed BLAKE3 instance): those return false so the pool's policy disposes the instance — erasing the secret — instead of recycling it for an unrelated caller.