Class KT128
- Namespace
- CryptoHives.Foundation.Security.Cryptography.Hash
- Assembly
- CryptoHives.Foundation.Security.Cryptography.dll
Computes a variable-length hash using KT128 (KangarooTwelve) per RFC 9861.
public sealed class KT128 : HashAlgorithm, ICryptoTransform, IDisposable, IResettable, IExtendableOutput
- Inheritance
-
KT128
- Implements
- Inherited Members
Remarks
KT128 is a high-performance extendable-output function (XOF) that applies tree hashing on top of TurboSHAKE128. It provides 128-bit security and supports parallel hashing for large inputs using Sakura-compatible tree mode.
KT128 uses a rate of 168 bytes (same as TurboSHAKE128) and splits input into 8192-byte chunks. For inputs ≤ 8192 bytes, it operates as a single TurboSHAKE128 call with domain separator 0x07. For larger inputs, it uses tree hashing with 32-byte chaining values.
This implementation is specified in RFC 9861 Section 3.2.
Constructors
KT128()
Initializes a new instance of the KT128 class with default output size.
public KT128()
KT128(int)
Initializes a new instance of the KT128 class with specified output size.
public KT128(int outputBytes)
Parameters
outputBytesintThe desired output size in bytes.
KT128(int, ReadOnlySpan<byte>)
Initializes a new instance of the KT128 class with customization bytes.
public KT128(int outputBytes, ReadOnlySpan<byte> customization)
Parameters
outputBytesintThe desired output size in bytes.
customizationReadOnlySpan<byte>The customization bytes for domain separation.
KT128(int, string)
Initializes a new instance of the KT128 class with customization string.
public KT128(int outputBytes, string customization)
Parameters
outputBytesintThe desired output size in bytes.
customizationstringThe customization string for domain separation.
Fields
ChainingValueSize
The chaining value size in bytes (32 bytes for KT128).
public const int ChainingValueSize = 32
Field Value
ChunkSize
The chunk size for tree hashing (8192 bytes).
public const int ChunkSize = 8192
Field Value
RateBytes
The rate in bytes for KT128 (1344 bits = 168 bytes, same as TurboSHAKE128).
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 KT128 class with default output size (32 bytes).
public static KT128 Create()
Returns
- KT128
A new KT128 instance.
Create(int)
Creates a new instance of the KT128 class with specified output size.
public static KT128 Create(int outputBytes)
Parameters
outputBytesintThe desired output size in bytes.
Returns
- KT128
A new KT128 instance.
Create(int, ReadOnlySpan<byte>)
Creates a new instance with customization bytes.
public static KT128 Create(int outputBytes, ReadOnlySpan<byte> customization)
Parameters
outputBytesintThe desired output size in bytes.
customizationReadOnlySpan<byte>The customization bytes for domain separation.
Returns
- KT128
A new KT128 instance.
Create(int, string)
Creates a new instance with customization string.
public static KT128 Create(int outputBytes, string customization)
Parameters
outputBytesintThe desired output size in bytes.
customizationstringThe customization string for domain separation.
Returns
- KT128
A new KT128 instance.
Dispose(bool)
Releases the unmanaged resources used by the HashAlgorithm and optionally releases the managed resources.
protected override void Dispose(bool disposing)
Parameters
disposingbooltrue to release both managed and unmanaged resources; false to release only unmanaged resources.
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.
HashData(in ReadOnlySequence<byte>)
Computes the KT128 hash of source using the default output size (32 bytes)
and returns it as a new byte array.
public static byte[] HashData(in ReadOnlySequence<byte> source)
Parameters
sourceReadOnlySequence<byte>The (possibly multi-segment) input sequence to hash.
Returns
- byte[]
A new byte array containing the KT128 hash.
HashData(ReadOnlySpan<byte>)
Computes the KT128 hash of source using the default output size (32 bytes)
and returns it as a new byte array.
public static byte[] HashData(ReadOnlySpan<byte> source)
Parameters
sourceReadOnlySpan<byte>The input data to hash.
Returns
- byte[]
A new byte array containing the KT128 hash.
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 KT128 state.
public void Squeeze(Span<byte> output)
Parameters
Exceptions
- ObjectDisposedException
Thrown when the instance has been disposed.
TryHashData(in ReadOnlySequence<byte>, Span<byte>, out int)
Computes the KT128 hash of source using the default output size (32 bytes)
and writes it into destination.
public static bool TryHashData(in ReadOnlySequence<byte> source, Span<byte> destination, out int bytesWritten)
Parameters
sourceReadOnlySequence<byte>The (possibly multi-segment) input sequence to hash.
destinationSpan<byte>The buffer to receive the hash value. Must be at least
32bytes.bytesWrittenintWhen this method returns, the number of bytes written into
destination.
Returns
TryHashData(ReadOnlySpan<byte>, Span<byte>, out int)
Computes the KT128 hash of source using the default output size (32 bytes)
and writes it into destination.
public static bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
Parameters
sourceReadOnlySpan<byte>The input data to hash.
destinationSpan<byte>The buffer to receive the hash value. Must be at least
32bytes.bytesWrittenintWhen this method returns, the number of bytes written into
destination.
Returns
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.