Repository navigation
OnixLabs.Security.Cryptography.HashAlgorithmExtensions
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
|
[EditorBrowsable(EditorBrowsableState.Never)]
public static class HashAlgorithmExtensionsHashAlgorithmExtensions 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.
| 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. |
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);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);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);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. |
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. |
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. |
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. |
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);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");
}ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions