Skip to content

OnixLabs.Security.Cryptography.Sha3

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

Sha3 Class

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

Definition

public abstract partial class Sha3 : HashAlgorithm

Remarks

Sha3 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, and Sha3Hash512 each produce a digest of a specific bit-length and use the domain-separation delimiter 0x06. The output length is fixed at construction and cannot be changed.
  • Extendable-output functions (XOFs) — Sha3Shake128 and Sha3Shake256 use the delimiter 0x1f and 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 HashDelimiter and ShakeDelimiter are exposed so that derived types can pass the correct domain-separation byte to the base constructor. They are not intended for general use.

Examples

Computing a SHA3-256 digest via the factory

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));
// 1AF17A664E3FA8E419B8BA05C2A173169DF76162A5A286E0C405B460D478F7EF

Computing a SHA3-256 digest directly from the concrete type

using OnixLabs.Security.Cryptography;

byte[] input = System.Text.Encoding.UTF8.GetBytes("Hello, World!");

using var sha3 = new Sha3Hash256();
byte[] hash = sha3.ComputeHash(input);

Producing a variable-length SHAKE128 output

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

Constants

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.

HashDelimiter

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.

ShakeDelimiter

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.

Constructors

Constructor Description
Sha3(int, int, int) Initializes a new instance of the Sha3 class. Protected; called from derived-type constructors.

Sha3(int, int, int)

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.

Methods

Static Methods

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.

Overridden HashAlgorithm Members

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.

CreateSha3Hash224()

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.

CreateSha3Hash256()

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.

CreateSha3Hash384()

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.

CreateSha3Hash512()

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.

CreateSha3Shake128(int)

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.

CreateSha3Shake256(int)

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.

Initialize()

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.

HashCore(byte[], int, int)

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.

HashCore(ReadOnlySpan<byte>)

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.

HashFinal()

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.

TryHashFinal(Span<byte>, out int)

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.


See Also

ONIXLabs .NET Library


Clone this wiki locally