Table of Contents

Class KT256

Namespace
CryptoHives.Foundation.Security.Cryptography.Hash
Assembly
CryptoHives.Foundation.Security.Cryptography.dll

Computes a variable-length hash using KT256 per RFC 9861.

public sealed class KT256 : HashAlgorithm, ICryptoTransform, IDisposable, IResettable, IExtendableOutput
Inheritance
KT256
Implements
Inherited Members

Remarks

KT256 is a high-performance extendable-output function (XOF) that applies tree hashing on top of TurboSHAKE256. It provides 256-bit security and supports parallel hashing for large inputs using Sakura-compatible tree mode.

KT256 uses a rate of 136 bytes (same as TurboSHAKE256) and splits input into 8192-byte chunks. For inputs ≤ 8192 bytes, it operates as a single TurboSHAKE256 call with domain separator 0x07. For larger inputs, it uses tree hashing with 64-byte chaining values.

This implementation is specified in RFC 9861 Section 3.4.

Constructors

KT256()

Initializes a new instance of the KT256 class with default output size.

public KT256()

KT256(int)

Initializes a new instance of the KT256 class with specified output size.

public KT256(int outputBytes)

Parameters

outputBytes int

The desired output size in bytes.

KT256(int, ReadOnlySpan<byte>)

Initializes a new instance of the KT256 class with customization bytes.

public KT256(int outputBytes, ReadOnlySpan<byte> customization)

Parameters

outputBytes int

The desired output size in bytes.

customization ReadOnlySpan<byte>

The customization bytes for domain separation.

KT256(int, string)

Initializes a new instance of the KT256 class with customization string.

public KT256(int outputBytes, string customization)

Parameters

outputBytes int

The desired output size in bytes.

customization string

The customization string for domain separation.

Fields

ChainingValueSize

The chaining value size in bytes (64 bytes for KT256).

public const int ChainingValueSize = 64

Field Value

int

ChunkSize

The chunk size for tree hashing (8192 bytes).

public const int ChunkSize = 8192

Field Value

int

RateBytes

The rate in bytes for KT256 (1088 bits = 136 bytes, same as TurboSHAKE256).

public const int RateBytes = 136

Field Value

int

Properties

AlgorithmName

Gets the name of the hash algorithm.

public override string AlgorithmName { get; }

Property Value

string

BlockSize

Gets the block size in bytes used by the hash algorithm.

public override int BlockSize { get; }

Property Value

int

Methods

Absorb(ReadOnlySpan<byte>)

Absorbs input data into the XOF state.

public void Absorb(ReadOnlySpan<byte> input)

Parameters

input ReadOnlySpan<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 KT256 class with default output size (64 bytes).

public static KT256 Create()

Returns

KT256

A new KT256 instance.

Create(int)

Creates a new instance of the KT256 class with specified output size.

public static KT256 Create(int outputBytes)

Parameters

outputBytes int

The desired output size in bytes.

Returns

KT256

A new KT256 instance.

Create(int, ReadOnlySpan<byte>)

Creates a new instance with customization bytes.

public static KT256 Create(int outputBytes, ReadOnlySpan<byte> customization)

Parameters

outputBytes int

The desired output size in bytes.

customization ReadOnlySpan<byte>

The customization bytes for domain separation.

Returns

KT256

A new KT256 instance.

Create(int, string)

Creates a new instance with customization string.

public static KT256 Create(int outputBytes, string customization)

Parameters

outputBytes int

The desired output size in bytes.

customization string

The customization string for domain separation.

Returns

KT256

A new KT256 instance.

Dispose(bool)

Releases the unmanaged resources used by the HashAlgorithm and optionally releases the managed resources.

protected override void Dispose(bool disposing)

Parameters

disposing bool

true 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

source ReadOnlySpan<byte>

The input to compute the hash code for.

Exceptions

ObjectDisposedException

Thrown when the instance has been disposed.

HashData(in ReadOnlySequence<byte>)

Computes the KT256 hash of source using the default output size (64 bytes) and returns it as a new byte array.

public static byte[] HashData(in ReadOnlySequence<byte> source)

Parameters

source ReadOnlySequence<byte>

The (possibly multi-segment) input sequence to hash.

Returns

byte[]

A new byte array containing the KT256 hash.

HashData(ReadOnlySpan<byte>)

Computes the KT256 hash of source using the default output size (64 bytes) and returns it as a new byte array.

public static byte[] HashData(ReadOnlySpan<byte> source)

Parameters

source ReadOnlySpan<byte>

The input data to hash.

Returns

byte[]

A new byte array containing the KT256 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 KT256 state.

public void Squeeze(Span<byte> output)

Parameters

output Span<byte>

The buffer to receive the output.

Exceptions

ObjectDisposedException

Thrown when the instance has been disposed.

TryHashData(in ReadOnlySequence<byte>, Span<byte>, out int)

Computes the KT256 hash of source using the default output size (64 bytes) and writes it into destination.

public static bool TryHashData(in ReadOnlySequence<byte> source, Span<byte> destination, out int bytesWritten)

Parameters

source ReadOnlySequence<byte>

The (possibly multi-segment) input sequence to hash.

destination Span<byte>

The buffer to receive the hash value. Must be at least 64 bytes.

bytesWritten int

When this method returns, the number of bytes written into destination.

Returns

bool

true if destination was large enough; otherwise, false.

TryHashData(ReadOnlySpan<byte>, Span<byte>, out int)

Computes the KT256 hash of source using the default output size (64 bytes) and writes it into destination.

public static bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)

Parameters

source ReadOnlySpan<byte>

The input data to hash.

destination Span<byte>

The buffer to receive the hash value. Must be at least 64 bytes.

bytesWritten int

When this method returns, the number of bytes written into destination.

Returns

bool

true if destination was large enough; otherwise, false.

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

destination Span<byte>

The target buffer to write the hash to.

bytesWritten int

The number of bytes written into the buffer.

Returns

bool

true if the destination buffer was large enough; otherwise false.