Skip to content

OnixLabs.Security.Cryptography.HashAlgorithmExtensions

Matthew Layton edited this page Nov 15, 2026 · 2 revisions

HashAlgorithmExtensions Class

Provides extension methods for HashAlgorithm instances, offering multi-round hash computation over byte spans, sliced byte spans, text, IBinaryConvertible data, streams, and a low-level try-pattern overload.

Namespace OnixLabs.Security.Cryptography
Assembly OnixLabs.Security.Cryptography.dll (NuGet)
Inheritance object → HashAlgorithmExtensions

Definition

[EditorBrowsable(EditorBrowsableState.Never)]
public static class HashAlgorithmExtensions

Remarks

HashAlgorithmExtensions is a public static extension container decorated with [EditorBrowsable(EditorBrowsableState.Never)], which keeps the class itself out of IntelliSense lists while its extension methods remain fully usable on HashAlgorithm receivers. Bring the methods into scope with using OnixLabs.Security.Cryptography;.

The class uses C# 14 extension blocks (extension(HashAlgorithm algorithm) { ... }), so the receiver type appears in the block declaration rather than as a this parameter. The signatures below are the call-site signatures as consumed by user code.

The rounds parameter available on most overloads enables iterative re-hashing: when rounds is 2, the output of the first hash pass is hashed again, yielding double-hash patterns such as double-SHA-256 used in Bitcoin.

The low-level TryComputeHash overload writes directly into a caller-allocated Span<byte> and follows the standard .NET try-pattern: it returns false without throwing when the destination buffer is too short, rounds is less than 1, or any error occurs during computation.

Methods

Method Returns Description
ComputeHash(ReadOnlySpan<byte>, int) byte[] Computes the hash value for the specified ReadOnlySpan<byte>, optionally repeating for multiple rounds.
ComputeHash(ReadOnlySpan<byte>, int, int, int) byte[] Computes the hash value for a sliced region of the specified ReadOnlySpan<byte>.
ComputeHash(ReadOnlySpan<char>, Encoding?, int) byte[] Computes the hash value for the specified ReadOnlySpan<char> by encoding it to bytes first.
ComputeHash(IBinaryConvertible, int) byte[] Computes the hash value for the specified IBinaryConvertible value.
ComputeHash(IBinaryConvertible, int, int, int) byte[] Computes the hash value for a sliced region of the specified IBinaryConvertible value.
ComputeHash(ISpanBinaryConvertible, int) byte[] Computes the hash value for the specified ISpanBinaryConvertible value.
ComputeHash(ISpanBinaryConvertible, int, int, int) byte[] Computes the hash value for a sliced region of the specified ISpanBinaryConvertible value.
ComputeHash(Stream, int) byte[] Computes the hash value for the specified Stream, optionally repeating for multiple rounds.
TryComputeHash(ReadOnlySpan<byte>, Span<byte>, int, int, int, out int) bool Attempts to compute the hash value for the specified ReadOnlySpan<byte> into a pre-allocated destination span.

ComputeHash(ReadOnlySpan<byte>, int)

public byte[] ComputeHash(ReadOnlySpan<byte> data, int rounds = 1)

Computes the hash value for the specified ReadOnlySpan<byte> value. When rounds is greater than 1, the hash is re-applied to its own output for the specified number of iterations.

Parameter Type Description
data ReadOnlySpan<byte> The input data to compute the hash for.
rounds int The number of rounds that the input data should be hashed. Defaults to 1.

Returns — A new byte[] containing the computed hash value.

Exceptions

Exception Condition
CryptographicException The hash could not be computed for the specified input data.
using System.Security.Cryptography;

byte[] data = [0x01, 0x02, 0x03];
byte[] hash = SHA256.Create().ComputeHash((ReadOnlySpan<byte>)data);
byte[] doubleHash = SHA256.Create().ComputeHash((ReadOnlySpan<byte>)data, rounds: 2);

ComputeHash(ReadOnlySpan<byte>, int, int, int)

public byte[] ComputeHash(ReadOnlySpan<byte> data, int offset, int count, int rounds = 1)

Computes the hash value for a contiguous region of the specified ReadOnlySpan<byte> value, defined by offset and count. When rounds is greater than 1, the hash is re-applied to its own output for the specified number of iterations.

Parameter Type Description
data ReadOnlySpan<byte> The input data to compute the hash for.
offset int The offset into the input from which to begin using data.
count int The number of bytes in the input to use as data.
rounds int The number of rounds that the input data should be hashed. Defaults to 1.

Returns — A new byte[] containing the computed hash value.

Exceptions

Exception Condition
CryptographicException The hash could not be computed for the specified input data.
byte[] data = [0x00, 0x01, 0x02, 0x03, 0x04];
// Hash only bytes at index 1, 2, 3 (count = 3)
byte[] hash = SHA256.Create().ComputeHash((ReadOnlySpan<byte>)data, offset: 1, count: 3);

ComputeHash(ReadOnlySpan<char>, Encoding?, int)

public byte[] ComputeHash(ReadOnlySpan<char> data, Encoding? encoding = null, int rounds = 1)

Computes the hash value for the specified ReadOnlySpan<char> value by first encoding it to bytes using the specified encoding. When encoding is null, a default encoding is used. Because the text is often a password, it is encoded into a temporary stack or pooled buffer that is zeroed after hashing, rather than into a new array that would stay on the heap until collected.

Parameter Type Description
data ReadOnlySpan<char> The input data to compute the hash for.
encoding Encoding? The Encoding used to convert the character span to bytes. Defaults to null.
rounds int The number of rounds that the input data should be hashed. Defaults to 1.

Returns — A new byte[] containing the computed hash value.

Exceptions

Exception Condition
CryptographicException The hash could not be computed for the specified input data.
byte[] hash = SHA256.Create().ComputeHash("hello world".AsSpan(), Encoding.UTF8);

ComputeHash(IBinaryConvertible, int)

public byte[] ComputeHash(IBinaryConvertible data, int rounds = 1)

Computes the hash value for the specified IBinaryConvertible value by obtaining its underlying byte span. When rounds is greater than 1, the hash is re-applied to its own output for the specified number of iterations.

Parameter Type Description
data IBinaryConvertible The input data to compute the hash for.
rounds int The number of rounds that the input data should be hashed. Defaults to 1.

Returns — A new byte[] containing the computed hash value.

Exceptions

Exception Condition
CryptographicException The hash could not be computed for the specified input data.

ComputeHash(IBinaryConvertible, int, int, int)

public byte[] ComputeHash(IBinaryConvertible data, int offset, int count, int rounds = 1)

Computes the hash value for a contiguous region of the specified IBinaryConvertible value, defined by offset and count. When rounds is greater than 1, the hash is re-applied to its own output for the specified number of iterations.

Parameter Type Description
data IBinaryConvertible The input data to compute the hash for.
offset int The offset into the input from which to begin using data.
count int The number of bytes in the input to use as data.
rounds int The number of rounds that the input data should be hashed. Defaults to 1.

Returns — A new byte[] containing the computed hash value.

Exceptions

Exception Condition
CryptographicException The hash could not be computed for the specified input data.

ComputeHash(ISpanBinaryConvertible, int)

public byte[] ComputeHash(ISpanBinaryConvertible data, int rounds = 1)

Computes the hash value for the specified ISpanBinaryConvertible value by obtaining its underlying byte span. When rounds is greater than 1, the hash is re-applied to its own output for the specified number of iterations.

Parameter Type Description
data ISpanBinaryConvertible The input data to compute the hash for.
rounds int The number of rounds that the input data should be hashed. Defaults to 1.

Returns — A new byte[] containing the computed hash value.

Exceptions

Exception Condition
CryptographicException The hash could not be computed for the specified input data.

ComputeHash(ISpanBinaryConvertible, int, int, int)

public byte[] ComputeHash(ISpanBinaryConvertible data, int offset, int count, int rounds = 1)

Computes the hash value for a contiguous region of the specified ISpanBinaryConvertible value, defined by offset and count. When rounds is greater than 1, the hash is re-applied to its own output for the specified number of iterations.

Parameter Type Description
data ISpanBinaryConvertible The input data to compute the hash for.
offset int The offset into the input from which to begin using data.
count int The number of bytes in the input to use as data.
rounds int The number of rounds that the input data should be hashed. Defaults to 1.

Returns — A new byte[] containing the computed hash value.

Exceptions

Exception Condition
CryptographicException The hash could not be computed for the specified input data.

ComputeHash(Stream, int)

public byte[] ComputeHash(Stream stream, int rounds)

Computes the hash value for the specified Stream. When rounds is greater than 1, the hash is re-applied to its own output for the specified number of iterations.

Parameter Type Description
stream Stream The input data to compute the hash for.
rounds int The number of rounds that the input data should be hashed.

Returns — A new byte[] containing the computed hash value.

Exceptions

Exception Condition
CryptographicException The hash could not be computed for the specified input data.
using FileStream fs = File.OpenRead("data.bin");
byte[] hash = SHA256.Create().ComputeHash(fs, rounds: 1);

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

public bool TryComputeHash(ReadOnlySpan<byte> source, Span<byte> destination, int offset, int count, int rounds, out int bytesWritten)

Attempts to compute the hash value for the specified ReadOnlySpan<byte> into a caller-allocated destination span, supporting multi-round hashing. Returns false without throwing if the destination is too short, rounds is less than 1, or an error occurs during computation.

Parameter Type Description
source ReadOnlySpan<byte> The input to compute the hash code for.
destination Span<byte> The buffer to receive the hash value.
offset int The offset into the input from which to begin using data.
count int The number of bytes in the input to use as data.
rounds int The number of rounds that the input data should be hashed.
bytesWritten out int When this method returns, the total number of bytes written into destination. This parameter is treated as uninitialized.

Returns — true if destination is long enough to receive the hash value and computation succeeded; otherwise, false.

HashAlgorithm sha = SHA256.Create();
byte[] data = [0x01, 0x02, 0x03];
Span<byte> dest = stackalloc byte[32];

if (sha.TryComputeHash(data, dest, offset: 0, count: data.Length, rounds: 1, out int written))
{
    Console.WriteLine($"Hash written: {written} bytes");
}

See Also

ONIXLabs .NET Library


Clone this wiki locally