Class CShake128
- Namespace
- CryptoHives.Foundation.Security.Cryptography.Hash
- Assembly
- CryptoHives.Foundation.Security.Cryptography.dll
Computes a customizable SHAKE128 (cSHAKE128) output.
public sealed class CShake128 : KeccakCore, ICryptoTransform, IDisposable, IResettable, IExtendableOutput
- Inheritance
-
CShake128
- Implements
- Inherited Members
Remarks
This is a fully managed implementation of cSHAKE128 based on the Keccak sponge construction. It does not rely on OS or hardware cryptographic APIs, ensuring deterministic behavior across all platforms and runtimes.
cSHAKE128 is a customizable variant of SHAKE128 defined in NIST SP 800-185. It supports function name (N) and customization string (S) parameters for domain separation.
When both N and S are empty, cSHAKE128 is equivalent to SHAKE128.
Constructors
CShake128(int, byte[]?, byte[]?)
Initializes a new instance of the CShake128 class with byte array parameters.
public CShake128(int outputBytes, byte[]? functionName, byte[]? customization)
Parameters
outputBytesintThe desired output size in bytes.
functionNamebyte[]The function name bytes N.
customizationbyte[]The customization bytes S.
CShake128(int, string?, string?)
Initializes a new instance of the CShake128 class.
public CShake128(int outputBytes = 32, string? functionName = null, string? customization = null)
Parameters
outputBytesintThe desired output size in bytes.
functionNamestringThe function name string N (for NIST-defined functions).
customizationstringThe customization string S.
Fields
CapacityBytes
The capacity in bytes (256 bits for cSHAKE128).
public const int CapacityBytes = 32
Field Value
DefaultOutputBits
The default output size in bits.
public const int DefaultOutputBits = 256
Field Value
EncodeBufferLength
The minimum buffer size needed to call a TryEncodeLeft or TryEncodeRight function.
public const int EncodeBufferLength = 9
Field Value
RateBytes
The rate in bytes (1344 bits for cSHAKE128).
public const int RateBytes = 168
Field Value
Properties
AlgorithmName
Gets the name of the hash algorithm.
public override string AlgorithmName { get; }
Property Value
BlockSize
Gets the block size in bytes used by the hash algorithm.
public override int BlockSize { get; }
Property Value
Methods
Absorb(ReadOnlySpan<byte>)
Absorbs input data into the XOF state.
public void Absorb(ReadOnlySpan<byte> input)
Parameters
inputReadOnlySpan<byte>The input data to absorb.
Exceptions
- InvalidOperationException
Thrown when data is added after output has been squeezed.
Create()
Creates a new instance of the CShake128 class with default parameters.
public static CShake128 Create()
Returns
Create(int, byte[]?, byte[]?)
Creates a new instance of the CShake128 hash function with the specified output length, function name, and customization string.
public static CShake128 Create(int outputBytes, byte[]? functionName = null, byte[]? customization = null)
Parameters
outputBytesintThe desired length, in bytes, of the hash output. Must be a positive integer.
functionNamebyte[]An optional function name as a byte array, used to domain-separate the hash function. Can be null if no function name is required.
customizationbyte[]An optional customization string as a byte array, used to further parameterize the hash function. Can be null if no customization is required.
Returns
- CShake128
A new CShake128 instance configured with the specified output length, function name, and customization string.
Create(int, string, string?)
Creates a new instance of the CShake128 class with specified parameters.
public static CShake128 Create(int outputBytes, string functionName, string? customization = null)
Parameters
outputBytesintThe desired output size in bytes.
functionNamestringThe function name N (for NIST-defined functions).
customizationstringThe customization string S (user-defined).
Returns
HashCore(ReadOnlySpan<byte>)
When overridden in a derived class, routes data written to the object into the hash algorithm for computing the hash.
protected override void HashCore(ReadOnlySpan<byte> source)
Parameters
sourceReadOnlySpan<byte>The input to compute the hash code for.
Exceptions
- ObjectDisposedException
Thrown when the instance has been disposed.
Initialize()
Initializes an implementation of the HashAlgorithm class.
public override void Initialize()
Exceptions
- ObjectDisposedException
Thrown when the instance has been disposed.
Reset()
Resets the XOF state so the instance can be reused for a new computation.
public void Reset()
Remarks
After calling this method, the instance is in the same state as a newly constructed one. All previously absorbed data and squeezed output are discarded.
Squeeze(Span<byte>)
Squeezes output bytes from the XOF state.
public void Squeeze(Span<byte> output)
Parameters
Exceptions
- ObjectDisposedException
Thrown when the instance has been disposed.
TryHashFinal(Span<byte>, out int)
When overridden in a derived class, finalizes the hash computation and writes the result into the provided buffer.
protected override bool TryHashFinal(Span<byte> destination, out int bytesWritten)
Parameters
destinationSpan<byte>The target buffer to write the hash to.
bytesWrittenintThe number of bytes written into the buffer.