Class CryptoGenericHash
- Namespace
- LibSodium
- Assembly
- LibSodium.Net.dll
Provides a high-level interface to the libsodium generic hash function, based on BLAKE2b.
public static class CryptoGenericHash
- Inheritance
-
CryptoGenericHash
- Inherited Members
Remarks
This class wraps the crypto_generichash
functions from libsodium, offering both one-shot and streaming hash computations.
The output length and key length can be customized within defined bounds. The hash can be computed over a byte span or a stream,
synchronously or asynchronously.
For additional details, see the official libsodium documentation: 🧂 https://libsodium.gitbook.io/doc/hashing/generic_hashing
Fields
HashLen
Default hash length in bytes (32).
public const int HashLen = 32
Field Value
KeyLen
Default key length in bytes (32).
public const int KeyLen = 32
Field Value
MaxHashLen
Maximum allowed length in bytes for the hash (64).
public const int MaxHashLen = 64
Field Value
MaxKeyLen
Maximum allowed key length in bytes (64 bytes).
public const int MaxKeyLen = 64
Field Value
MinHashLen
Minimum allowed length in bytes for the hash (16).
public const int MinHashLen = 16
Field Value
MinKeyLen
Minimum length in bytes for secret keys (16).
public const int MinKeyLen = 16
Field Value
Methods
ComputeHash(Span<byte>, Stream, SecureMemory<byte>?)
Computes a generic hash from the contents of a stream.
public static void ComputeHash(Span<byte> hash, Stream input, SecureMemory<byte>? key = null)
Parameters
hash
Span<byte>The buffer where the computed hash will be written. Its length defines the output size.
input
StreamThe input stream to read and hash.
key
SecureMemory<byte>An optional key for keyed hashing (HMAC-like). May be empty for unkeyed mode.
Exceptions
- ArgumentException
Thrown if
hash
has an invalid length, or ifkey
is too long.- LibSodiumException
Thrown if the hashing operation fails internally.
ComputeHash(Span<byte>, Stream, ReadOnlySpan<byte>)
Computes a generic hash from the contents of a stream.
public static void ComputeHash(Span<byte> hash, Stream input, ReadOnlySpan<byte> key = default)
Parameters
hash
Span<byte>The buffer where the computed hash will be written. Its length defines the output size.
input
StreamThe input stream to read and hash.
key
ReadOnlySpan<byte>An optional key for keyed hashing (HMAC-like). May be empty for unkeyed mode.
Exceptions
- ArgumentException
Thrown if
hash
has an invalid length, or ifkey
is too long.- LibSodiumException
Thrown if the hashing operation fails internally.
ComputeHash(Span<byte>, ReadOnlySpan<byte>, SecureMemory<byte>?)
Computes a generic hash of the specified message.
public static void ComputeHash(Span<byte> hash, ReadOnlySpan<byte> message, SecureMemory<byte>? key = null)
Parameters
hash
Span<byte>The buffer where the computed hash will be written. Its length defines the output size.
message
ReadOnlySpan<byte>The input message to hash.
key
SecureMemory<byte>An optional key for keyed hashing (HMAC-like). May be null for unkeyed mode.
Exceptions
- ArgumentException
Thrown if
hash
has an invalid length, or ifkey
is too long.- LibSodiumException
Thrown if the hashing operation fails internally.
ComputeHash(Span<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Computes a generic hash of the specified message.
public static void ComputeHash(Span<byte> hash, ReadOnlySpan<byte> message, ReadOnlySpan<byte> key = default)
Parameters
hash
Span<byte>The buffer where the computed hash will be written. Its length defines the output size.
message
ReadOnlySpan<byte>The input message to hash.
key
ReadOnlySpan<byte>An optional key for keyed hashing (HMAC-like). May be empty for unkeyed mode.
Exceptions
- ArgumentException
Thrown if
hash
has an invalid length, or ifkey
is too long.- LibSodiumException
Thrown if the hashing operation fails internally.
ComputeHashAsync(Memory<byte>, Stream, SecureMemory<byte>?, CancellationToken)
Asynchronously computes a generic hash from the contents of a stream.
public static Task ComputeHashAsync(Memory<byte> hash, Stream input, SecureMemory<byte>? key = null, CancellationToken cancellationToken = default)
Parameters
hash
Memory<byte>The memory buffer where the computed hash will be written. Its length defines the output size.
input
StreamThe input stream to read and hash.
key
SecureMemory<byte>An optional key for keyed hashing (HMAC-like). May be empty for unkeyed mode.
cancellationToken
CancellationTokenA cancellation token to cancel the operation.
Returns
- Task
A task representing the asynchronous hash computation.
Exceptions
- ArgumentException
Thrown if
hash
has an invalid length, or ifkey
is too long.- LibSodiumException
Thrown if the hashing operation fails internally.
ComputeHashAsync(Memory<byte>, Stream, ReadOnlyMemory<byte>, CancellationToken)
Asynchronously computes a generic hash from the contents of a stream.
public static Task ComputeHashAsync(Memory<byte> hash, Stream input, ReadOnlyMemory<byte> key = default, CancellationToken cancellationToken = default)
Parameters
hash
Memory<byte>The memory buffer where the computed hash will be written. Its length defines the output size.
input
StreamThe input stream to read and hash.
key
ReadOnlyMemory<byte>An optional key for keyed hashing (HMAC-like). May be empty for unkeyed mode.
cancellationToken
CancellationTokenA cancellation token to cancel the operation.
Returns
- Task
A task representing the asynchronous hash computation.
Exceptions
- ArgumentException
Thrown if
hash
has an invalid length, or ifkey
is too long.- LibSodiumException
Thrown if the hashing operation fails internally.
CreateIncrementalHash(SecureMemory<byte>?, int)
public static ICryptoIncrementalOperation CreateIncrementalHash(SecureMemory<byte>? key, int hashLen = 32)
Parameters
key
SecureMemory<byte>hashLen
int
Returns
CreateIncrementalHash(ReadOnlySpan<byte>, int)
public static ICryptoIncrementalOperation CreateIncrementalHash(ReadOnlySpan<byte> key = default, int hashLen = 32)
Parameters
key
ReadOnlySpan<byte>hashLen
int