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
hashSizeintThe desired hash output size in bits. Must be one of: 224, 256, 384, or 512.
Exceptions
- ArgumentOutOfRangeException
hashSizeis 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
initializationRoundsintThe number of initialization rounds to run before processing input data. Must be between MinRounds and MaxRounds inclusive.
roundsintThe number of transformation rounds applied to each full input block. Must be between MinRounds and MaxRounds inclusive.
transformBlockSizeintThe size, in bytes, of the input block used to trigger a state transformation. Must be between MinInputBlockSize and MaxInputBlockSize inclusive.
finalizationRoundsintThe number of finalization rounds applied after all input has been processed. Must be between MinRounds and MaxRounds inclusive.
hashSizeintThe desired hash output size in bits. Must be one of: 224, 256, 384, or 512.
Exceptions
- ArgumentOutOfRangeException
initializationRounds,rounds, orfinalizationRoundsis less than MinRounds or greater than MaxRounds.-or-
transformBlockSizeis less than MinInputBlockSize or greater than MaxInputBlockSize.-or-
hashSizeis not a permitted hash size.
Fields
MaxHashSize
The maximum allowable size of the computed hash, in bits.
public const int MaxHashSize = 512
Field Value
MaxInputBlockSize
The maximum allowable size of the input block, in bytes.
public const int MaxInputBlockSize = 128
Field Value
MaxRounds
The maximum number of rounds permitted for initialization, processing, or finalization.
public const int MaxRounds = 4096
Field Value
MinHashSize
The minimum allowable size of the computed hash, in bits.
public const int MinHashSize = 224
Field Value
MinInputBlockSize
The minimum allowable size of the input block, in bytes.
public const int MinInputBlockSize = 1
Field Value
MinRounds
The minimum number of rounds permitted for initialization, processing, or finalization.
public const int MinRounds = 1
Field Value
Properties
AlgorithmName
Gets the fully qualified algorithm name, including the variant and hash output size.
public string AlgorithmName { get; }
Property Value
Remarks
Follows the CubeHash naming convention from the original submission: CubeHashr+b/w+f-h,
where:
r= number of initialization roundsb= number of transformation rounds per blockw= block size in bytesf= number of finalization roundsh= 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
CanTransformMultipleBlocks
When overridden in a derived class, gets a value indicating whether multiple blocks can be transformed.
public override bool CanTransformMultipleBlocks { get; }
Property Value
FinalizationRounds
Gets or sets the number of finalization rounds applied after all input has been processed.
public int FinalizationRounds { get; set; }
Property Value
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
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
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
- 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
Remarks
A higher number of rounds provides greater mixing of the state per block, which improves security at the cost of speed.
Exceptions
- ArgumentOutOfRangeException
- 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
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
disposingbooltrue 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
arraybyte[]The input byte array containing the data to hash.
ibStartintThe zero-based index in
arrayat which to begin reading data.cbSizeintThe number of bytes to process from
array.
Exceptions
- ArgumentNullException
arrayis null.- ArgumentOutOfRangeException
ibStartis less than 0.-or-
cbSizeis less than 0.- ArgumentException
ibStartandcbSizespecify a range that exceeds the length ofarray.- 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
sourceReadOnlySpan<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
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
| Product | Versions |
|---|---|
| .NET | 8, 10 |