Repository navigation
OnixLabs.Security.Cryptography.Sha3
Represents the abstract base class for the FIPS 202 SHA-3 family of hash algorithms, providing fixed-length digest variants (SHA3-224, SHA3-256, SHA3-384, SHA3-512) and variable-length extendable-output functions (SHAKE128, SHAKE256). All variants integrate with the BCL HashAlgorithm API.
| Namespace | OnixLabs.Security.Cryptography |
| Assembly | OnixLabs.Security.Cryptography.dll (NuGet) |
| Inheritance |
object → HashAlgorithm → Sha3
|
| Derived |
Sha3Hash224, Sha3Hash256, Sha3Hash384, Sha3Hash512, Sha3Shake128, Sha3Shake256
|
public abstract partial class Sha3 : HashAlgorithmSha3 implements the Keccak-based sponge construction specified in FIPS 202. The sponge absorbs input in rate-sized blocks, applies the 24-round Keccak-f[1600] permutation (theta, rho-pi, chi, iota steps), and squeezes out the digest during finalisation.
Two sub-families are provided:
-
Fixed-length hash functions —
Sha3Hash224,Sha3Hash256,Sha3Hash384, andSha3Hash512each produce a digest of a specific bit-length and use the domain-separation delimiter0x06. The output length is fixed at construction and cannot be changed. -
Extendable-output functions (XOFs) —
Sha3Shake128andSha3Shake256use the delimiter0x1fand accept a caller-supplied output length in bytes, so an arbitrary number of output bytes can be produced from the same input.
Because Sha3 extends HashAlgorithm, all derived types integrate transparently with BCL idioms: ComputeHash, TransformBlock / TransformFinalBlock, and the using-disposable pattern all work without modification.
Static factory methods on Sha3 (CreateSha3Hash224, CreateSha3Hash256, and so on) provide the preferred way to instantiate each variant without requiring a direct reference to the concrete type.
Note The two protected constants
HashDelimiterandShakeDelimiterare exposed so that derived types can pass the correct domain-separation byte to the base constructor. They are not intended for general use.
using OnixLabs.Security.Cryptography;
byte[] input = System.Text.Encoding.UTF8.GetBytes("Hello, World!");
using Sha3Hash256 sha3 = Sha3.CreateSha3Hash256();
byte[] hash = sha3.ComputeHash(input);
Console.WriteLine(Convert.ToHexString(hash));
// 1AF17A664E3FA8E419B8BA05C2A173169DF76162A5A286E0C405B460D478F7EFusing OnixLabs.Security.Cryptography;
byte[] input = System.Text.Encoding.UTF8.GetBytes("Hello, World!");
using var sha3 = new Sha3Hash256();
byte[] hash = sha3.ComputeHash(input);SHAKE128 accepts an output length in bytes. The example below requests 32 bytes (256 bits) of output.
using OnixLabs.Security.Cryptography;
byte[] input = System.Text.Encoding.UTF8.GetBytes("Hello, World!");
using Sha3Shake128 shake = Sha3.CreateSha3Shake128(length: 32);
byte[] hash = shake.ComputeHash(input);
Console.WriteLine(Convert.ToHexString(hash)); // 32 bytes of XOF output| Constant | Type | Value | Description |
|---|---|---|---|
HashDelimiter |
int |
0x06 |
The domain-separation delimiter for the fixed-length hash variants. |
ShakeDelimiter |
int |
0x1f |
The domain-separation delimiter for the SHAKE extendable-output variants. |
protected const int HashDelimiter = 0x06;The domain-separation delimiter byte applied during padding for the SHA3-224, SHA3-256, SHA3-384, and SHA3-512 fixed-length hash variants, as specified in FIPS 202.
protected const int ShakeDelimiter = 0x1f;The domain-separation delimiter byte applied during padding for the SHAKE128 and SHAKE256 extendable-output variants, as specified in FIPS 202.
| Constructor | Description |
|---|---|
Sha3(int, int, int) |
Initializes a new instance of the Sha3 class. Protected; called from derived-type constructors. |
protected Sha3(int rateBytes, int delimiter, int bitLength)Initializes a new instance of the Sha3 class and resets the sponge state via Initialize.
| Parameter | Type | Description |
|---|---|---|
rateBytes |
int |
The rate in bytes of the Keccak sponge state. Controls how many bytes are absorbed per permutation. |
delimiter |
int |
The domain-separation delimiter byte. Pass HashDelimiter for the fixed-length hash variants or ShakeDelimiter for XOFs. |
bitLength |
int |
The desired output length in bits. Stored as HashSizeValue and determines how many bytes are squeezed from the sponge. |
| Method | Returns | Description |
|---|---|---|
CreateSha3Hash224() |
Sha3Hash224 |
Creates an instance of the SHA3-224 algorithm. |
CreateSha3Hash256() |
Sha3Hash256 |
Creates an instance of the SHA3-256 algorithm. |
CreateSha3Hash384() |
Sha3Hash384 |
Creates an instance of the SHA3-384 algorithm. |
CreateSha3Hash512() |
Sha3Hash512 |
Creates an instance of the SHA3-512 algorithm. |
CreateSha3Shake128(int) |
Sha3Shake128 |
Creates an instance of the SHAKE128 extendable-output algorithm. |
CreateSha3Shake256(int) |
Sha3Shake256 |
Creates an instance of the SHAKE256 extendable-output algorithm. |
| Method | Returns | Description |
|---|---|---|
Initialize() |
void |
Resets the hash algorithm to its initial state. |
HashCore(byte[], int, int) |
void |
Routes data written to the object into the sponge absorb phase. |
HashCore(ReadOnlySpan<byte>) |
void |
Routes span data written to the object into the sponge absorb phase. |
HashFinal() |
byte[] |
Finalises the hash computation and returns the digest. |
TryHashFinal(Span<byte>, out int) |
bool |
Finalises the hash computation and writes the digest into the specified destination. |
public static Sha3Hash224 CreateSha3Hash224()Creates and returns a new instance of the Sha3Hash224 algorithm.
Returns — A new Sha3Hash224 instance ready to compute a 224-bit SHA-3 digest.
public static Sha3Hash256 CreateSha3Hash256()Creates and returns a new instance of the Sha3Hash256 algorithm.
Returns — A new Sha3Hash256 instance ready to compute a 256-bit SHA-3 digest.
public static Sha3Hash384 CreateSha3Hash384()Creates and returns a new instance of the Sha3Hash384 algorithm.
Returns — A new Sha3Hash384 instance ready to compute a 384-bit SHA-3 digest.
public static Sha3Hash512 CreateSha3Hash512()Creates and returns a new instance of the Sha3Hash512 algorithm.
Returns — A new Sha3Hash512 instance ready to compute a 512-bit SHA-3 digest.
public static Sha3Shake128 CreateSha3Shake128(int length)Creates and returns a new instance of the Sha3Shake128 extendable-output algorithm with the specified output length.
| Parameter | Type | Description |
|---|---|---|
length |
int |
The desired output length of the hash in bytes. |
Returns — A new Sha3Shake128 instance configured to produce length bytes of output.
public static Sha3Shake256 CreateSha3Shake256(int length)Creates and returns a new instance of the Sha3Shake256 extendable-output algorithm with the specified output length.
| Parameter | Type | Description |
|---|---|---|
length |
int |
The desired output length of the hash in bytes. |
Returns — A new Sha3Shake256 instance configured to produce length bytes of output.
public sealed override void Initialize()Resets the hash algorithm to its initial state, clearing the sponge state array and the block-size counter. Called automatically by the constructor and by HashAlgorithm after each ComputeHash call.
protected override void HashCore(byte[] array, int ibStart, int cbSize)Routes data written to the object into the Keccak sponge absorb phase. Input is XORed into the rate portion of the state in rate-sized chunks; a permutation is applied each time a full block accumulates. This method is called by the BCL HashAlgorithm infrastructure when ComputeHash or TransformBlock is used.
| Parameter | Type | Description |
|---|---|---|
array |
byte[] |
The input data to absorb. |
ibStart |
int |
The offset into array at which to begin reading. |
cbSize |
int |
The number of bytes to read from array. |
protected override void HashCore(ReadOnlySpan<byte> source)Routes span data written to the object into the Keccak sponge absorb phase, in the same way as HashCore(byte[], int, int), which delegates to this overload. The span-based HashAlgorithm members, such as TryComputeHash(ReadOnlySpan<byte>, Span<byte>, out int), call this method without copying the input to an array.
| Parameter | Type | Description |
|---|---|---|
source |
ReadOnlySpan<byte> |
The input data to absorb. |
protected override byte[] HashFinal()Finalises the sponge computation. Applies FIPS 202 multi-rate padding using the configured delimiter, performs the final Keccak-f[1600] permutation(s), and squeezes the requested number of bytes from the state.
Returns — A byte[] containing the computed hash. The array length equals HashSize / 8.
protected override bool TryHashFinal(Span<byte> destination, out int bytesWritten)Finalises the sponge computation in the same way as HashFinal(), writing the digest directly into destination instead of allocating an array. HashFinal() delegates to this method.
| Parameter | Type | Description |
|---|---|---|
destination |
Span<byte> |
The buffer that receives the computed hash. |
bytesWritten |
out int |
When this method returns, contains HashSize / 8 on success; otherwise, 0. |
Returns — true if destination is at least HashSize / 8 bytes long and the hash was written; otherwise, false.
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