Table of Contents

CubeHash Class

Definition

Namespace
Bodu.Security.Cryptography
Assembly
Bodu.Security.Cryptography.dll
Package
Bodu.Security.Cryptography 1.2.0
Source
CubeHash.cs

Computes a hash using the CubeHash permutation-based hash algorithm designed by Daniel J. Bernstein and submitted to the NIST SHA-3 competition. This class cannot be inherited.

public sealed class CubeHash : HashAlgorithm, ICryptoTransform, IDisposable
Inheritance
CubeHash
Implements
Inherited Members
Extension Methods

Examples

using Bodu.Security.Cryptography;

// Default parameters: 512-bit output, 32-byte input block, 16/16/32 rounds.
using var cube = new CubeHash();
byte[] digest = cube.ComputeHash(message);

Remarks

CubeHash operates on a 1024-bit internal state updated through a sequence of ARX (Addition, Rotation, XOR) operations. The number of initialization, transformation, and finalization rounds, the hash output size, and the input block size are all configurable. See Wikipedia for an overview.

Parameters at a glance.

  • State size: 1024 bits (32 × 32-bit words).
  • Output size: 224, 256, 384, or 512 bits (default 512); pass the desired size to CubeHash(int).
  • Input block size: configurable, MinInputBlockSize-MaxInputBlockSize bytes (default 32).
  • Rounds: initialization, per-block, and finalization counts each independently configurable up to MaxRounds; defaults are 16 / 16 / 32.

When to choose CubeHash. Pick CubeHash for academic study, cryptographic competition reproducibility, or interop with code that has settled on a specific CubeHash parameterization. The defaults (CubeHash 16/32/+160/512) match the SHA-3 competition submission. For new general-purpose cryptographic hashing prefer SHA-2, SHA-3, or Blake2b.

Constructors

CubeHash()

Initializes a new instance of the CubeHash class with default parameters: 512-bit output, 32-byte input block, and 16 / 16 / 32 initialization / transform / finalization rounds.

public CubeHash()

CubeHash(int)

Initializes a new instance of the CubeHash class with the specified hash output size and default algorithm parameters: 32-byte input block and 16 / 16 / 32 initialization / transform / finalization rounds.

public CubeHash(int hashSize)

Parameters

hashSize int

The desired hash output size in bits. Must be one of: 224, 256, 384, or 512.

Exceptions

ArgumentOutOfRangeException

hashSize is not a permitted hash size.

CubeHash(int, int, int, int, int)

Initializes a new instance of the CubeHash class with fully specified algorithm parameters.

public CubeHash(int initializationRounds, int rounds, int transformBlockSize, int finalizationRounds, int hashSize)

Parameters

initializationRounds int

The number of initialization rounds to run before processing input data. Must be between MinRounds and MaxRounds inclusive.

rounds int

The number of transformation rounds applied to each full input block. Must be between MinRounds and MaxRounds inclusive.

transformBlockSize int

The size, in bytes, of the input block used to trigger a state transformation. Must be between MinInputBlockSize and MaxInputBlockSize inclusive.

finalizationRounds int

The number of finalization rounds applied after all input has been processed. Must be between MinRounds and MaxRounds inclusive.

hashSize int

The desired hash output size in bits. Must be one of: 224, 256, 384, or 512.

Exceptions

ArgumentOutOfRangeException

initializationRounds, rounds, or finalizationRounds is less than MinRounds or greater than MaxRounds.

-or-

transformBlockSize is less than MinInputBlockSize or greater than MaxInputBlockSize.

-or-

hashSize is not a permitted hash size.

Fields

MaxHashSize

The maximum allowable size of the computed hash, in bits.

public const int MaxHashSize = 512

Field Value

int

MaxInputBlockSize

The maximum allowable size of the input block, in bytes.

public const int MaxInputBlockSize = 128

Field Value

int

MaxRounds

The maximum number of rounds permitted for initialization, processing, or finalization.

public const int MaxRounds = 4096

Field Value

int

MinHashSize

The minimum allowable size of the computed hash, in bits.

public const int MinHashSize = 224

Field Value

int

MinInputBlockSize

The minimum allowable size of the input block, in bytes.

public const int MinInputBlockSize = 1

Field Value

int

MinRounds

The minimum number of rounds permitted for initialization, processing, or finalization.

public const int MinRounds = 1

Field Value

int

Properties

AlgorithmName

Gets the fully qualified algorithm name, including the variant and hash output size.

public string AlgorithmName { get; }

Property Value

string

Remarks

Follows the CubeHash naming convention from the original submission: CubeHashr+b/w+f-h, where:

  • r = number of initialization rounds
  • b = number of transformation rounds per block
  • w = block size in bytes
  • f = number of finalization rounds
  • h = hash size in bits

Example: CubeHash16+32/32+32-256.

CanReuseTransform

Gets a value indicating whether the current transform can be reused.

public override bool CanReuseTransform { get; }

Property Value

bool

Always true.

CanTransformMultipleBlocks

When overridden in a derived class, gets a value indicating whether multiple blocks can be transformed.

public override bool CanTransformMultipleBlocks { get; }

Property Value

bool

true if multiple blocks can be transformed; otherwise, false.

FinalizationRounds

Gets or sets the number of finalization rounds applied after all input has been processed.

public int FinalizationRounds { get; set; }

Property Value

int

Remarks

Finalization rounds provide additional mixing of the internal state to ensure that the final hash output is highly sensitive to every bit of input data. Increasing this value strengthens final-state diffusion.

Exceptions

ObjectDisposedException

Instance has been disposed and its members are accessed.

CryptographicUnexpectedOperationException

The hash computation has already started.

ArgumentOutOfRangeException

Value is less than MinRounds or greater than MaxRounds.

HashSize

Gets or sets the size, in bits, of the final computed hash output.

public int HashSize { get; set; }

Property Value

int

The hash output size in bits.

Remarks

The hash size determines the length of the digest returned by the algorithm. Valid values are 224, 256, 384, and 512 bits.

Exceptions

ArgumentOutOfRangeException

Value is not one of the permitted sizes (224, 256, 384, 512).

ObjectDisposedException

Instance has been disposed and its members are accessed.

CryptographicUnexpectedOperationException

The hash computation has already started.

InitializationRounds

Gets or sets the number of initialization rounds to run before processing input data.

public int InitializationRounds { get; set; }

Property Value

int

Remarks

Initialization rounds mix the initial state of the algorithm before the first input byte is processed. Increasing this value enhances initial diffusion but increases computation time.

Exceptions

ArgumentOutOfRangeException

Value is less than MinRounds or greater than MaxRounds.

ObjectDisposedException

Instance has been disposed and its members are accessed.

CryptographicUnexpectedOperationException

The hash computation has already started.

Rounds

Gets or sets the number of transformation rounds applied to each full input block.

public int Rounds { get; set; }

Property Value

int

Remarks

A higher number of rounds provides greater mixing of the state per block, which improves security at the cost of speed.

Exceptions

ArgumentOutOfRangeException

Value is less than MinRounds or greater than MaxRounds.

ObjectDisposedException

Instance has been disposed and its members are accessed.

CryptographicUnexpectedOperationException

The hash computation has already started.

TransformBlockSize

Gets or sets the size, in bytes, of the input block used by the CubeHash algorithm to determine when to perform a state transformation.

public int TransformBlockSize { get; set; }

Property Value

int

Remarks

Unlike InputBlockSize, which is advisory, this property directly affects the output of the hash function. When the number of accumulated input bytes reaches TransformBlockSize, a transformation round is triggered. Modifying this value changes the frequency of internal state updates, impacting both performance and security characteristics.

Exceptions

ArgumentOutOfRangeException

Value is not within range MinInputBlockSize to MaxInputBlockSize.

ObjectDisposedException

Instance has been disposed and its members are accessed.

CryptographicUnexpectedOperationException

The hash computation has already started.

Methods

Dispose(bool)

Releases the unmanaged resources used by the algorithm and clears the key from memory.

protected override void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Remarks

Ensures all internal secrets are overwritten with zeros before releasing resources.

HashCore(byte[], int, int)

Processes a segment of the input byte array and feeds it into the CubeHash hashing algorithm. This method updates the internal state by processing cbSize bytes starting at the specified ibStart offset.

protected override void HashCore(byte[] array, int ibStart, int cbSize)

Parameters

array byte[]

The input byte array containing the data to hash.

ibStart int

The zero-based index in array at which to begin reading data.

cbSize int

The number of bytes to process from array.

Exceptions

ArgumentNullException

array is null.

ArgumentOutOfRangeException

ibStart is less than 0.

-or-

cbSize is less than 0.

ArgumentException

ibStart and cbSize specify a range that exceeds the length of array.

CryptographicUnexpectedOperationException

The hash algorithm has already been finalized and cannot accept more input data.

HashCore(ReadOnlySpan<byte>)

Processes the entirety of the input source and feeds it into the CubeHash hashing algorithm. This method updates the internal hash state accordingly by consuming the entire input span.

protected override void HashCore(ReadOnlySpan<byte> source)

Parameters

source ReadOnlySpan<byte>

The input byte span containing the data to hash.

Exceptions

CryptographicUnexpectedOperationException

The hash algorithm has already been finalized and cannot accept more input data.

HashFinal()

Finalizes the hash computation and returns the computed digest in little-endian byte order.

protected override byte[] HashFinal()

Returns

byte[]

A byte array containing the computed hash value. Its length is HashSize divided by 8.

Exceptions

CryptographicUnexpectedOperationException

Thrown when the hash algorithm has been disposed or has produced an unexpected finalization state.

Initialize()

Resets the hash algorithm to its initial state.

public override void Initialize()

Applies to

ProductVersions
.NET8, 10