Shake Class

Computes the extendable-output functions SHAKE128 and SHAKE256 (FIPS 202), and cSHAKE128 and cSHAKE256 (NIST SP 800-185) when a function name or a customization string is given.

public sealed class Shake

Remarks

An extendable-output function produces an output of any length: a shorter output is a prefix of a longer one. Use the static Shake.HashData methods to hash data in one call, or an instance to append the data in several calls and read the output in several calls.

cSHAKE with an empty function name and an empty customization string is SHAKE. The implementation is fully managed: it does not depend on the cryptographic library of the operating system, so it is available on every platform.

Instances are not thread-safe.

Constructors

NameDescription
Shake(int, ReadOnlySpan<byte>, ReadOnlySpan<byte>)Initializes a new instance of the Shake class.

Shake(int, ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Shake.cs

Initializes a new instance of the Shake class.

public Shake(int securityStrength, ReadOnlySpan<byte> functionName = default, ReadOnlySpan<byte> customization = default)

Parameters

securityStrength int
The security strength, in bits: 128 (SHAKE128, cSHAKE128) or 256 (SHAKE256, cSHAKE256).
functionName ReadOnlySpan<byte>
The function name N of cSHAKE, reserved for the functions defined by NIST; usually empty.
customization ReadOnlySpan<byte>
The customization string S of cSHAKE.

Exceptions

ArgumentOutOfRangeException
securityStrength is not 128 or 256.

Properties

NameDescription
SecurityStrengthGets the security strength, in bits: 128 or 256.

SecurityStrength

Shake.cs

Gets the security strength, in bits: 128 or 256.

public int SecurityStrength { get; }

Property value

int

Methods

NameDescription
AppendData(byte)Appends a byte to the input.
AppendData(ReadOnlySpan<byte>)Appends data to the input.
HashData(int, ReadOnlySpan<byte>, int, ReadOnlySpan<byte>, ReadOnlySpan<byte>)Computes the output of SHAKE or cSHAKE for data.
HashData(int, ReadOnlySpan<byte>, Span<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)Computes the output of SHAKE or cSHAKE for data into a buffer.
Read(Span<byte>)Reads the next bytes of the output. The first call ends the input: no more data can be appended.
ReadByte()Reads the next byte of the output. The first call ends the input: no more data can be appended.
Reset()Discards the input and the output, which may depend on secret data, so that the instance can hash new data.

AppendData overloads

AppendData(byte)

Shake.cs

Appends a byte to the input.

public void AppendData(byte value)
Parameters
value byte
The byte to append.
Exceptions
InvalidOperationException
The output was read: call Shake.Reset to hash new data.

AppendData(ReadOnlySpan<byte>)

Shake.cs

Appends data to the input.

public void AppendData(ReadOnlySpan<byte> data)
Parameters
data ReadOnlySpan<byte>
The data to append.
Exceptions
InvalidOperationException
The output was read: call Shake.Reset to hash new data.

HashData overloads

HashData(int, ReadOnlySpan<byte>, int, ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Shake.cs

Computes the output of SHAKE or cSHAKE for data.

public static byte[] HashData(int securityStrength, ReadOnlySpan<byte> source, int outputLength, ReadOnlySpan<byte> functionName = default, ReadOnlySpan<byte> customization = default)
Parameters
securityStrength int
The security strength, in bits: 128 (SHAKE128, cSHAKE128) or 256 (SHAKE256, cSHAKE256).
source ReadOnlySpan<byte>
The data to hash.
outputLength int
The length of the output, in bytes.
functionName ReadOnlySpan<byte>
The function name N of cSHAKE, reserved for the functions defined by NIST; usually empty.
customization ReadOnlySpan<byte>
The customization string S of cSHAKE.
Returns

byte[] The output, of outputLength bytes.

Exceptions
ArgumentOutOfRangeException
securityStrength is not 128 or 256, or outputLength is negative.

HashData(int, ReadOnlySpan<byte>, Span<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Shake.cs

Computes the output of SHAKE or cSHAKE for data into a buffer.

public static void HashData(int securityStrength, ReadOnlySpan<byte> source, Span<byte> destination, ReadOnlySpan<byte> functionName = default, ReadOnlySpan<byte> customization = default)
Parameters
securityStrength int
The security strength, in bits: 128 (SHAKE128, cSHAKE128) or 256 (SHAKE256, cSHAKE256).
source ReadOnlySpan<byte>
The data to hash.
destination Span<byte>
The buffer to fill with the output.
functionName ReadOnlySpan<byte>
The function name N of cSHAKE, reserved for the functions defined by NIST; usually empty.
customization ReadOnlySpan<byte>
The customization string S of cSHAKE.
Exceptions
ArgumentOutOfRangeException
securityStrength is not 128 or 256.

Read(Span<byte>)

Shake.cs

Reads the next bytes of the output. The first call ends the input: no more data can be appended.

public void Read(Span<byte> destination)

Parameters

destination Span<byte>
The buffer to fill with the next bytes of the output.

ReadByte()

Shake.cs

Reads the next byte of the output. The first call ends the input: no more data can be appended.

public byte ReadByte()

Returns

byte The next byte of the output.

Reset()

Shake.cs

Discards the input and the output, which may depend on secret data, so that the instance can hash new data.

public void Reset()