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 ShakeRemarks
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
| Name | Description |
|---|---|
| Shake(int, ReadOnlySpan<byte>, ReadOnlySpan<byte>) | Initializes a new instance of the Shake class. |
Shake(int, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Shake.csInitializes a new instance of the Shake class.
public Shake(int securityStrength, ReadOnlySpan<byte> functionName = default, ReadOnlySpan<byte> customization = default)Parameters
securityStrengthint- The security strength, in bits: 128 (SHAKE128, cSHAKE128) or 256 (SHAKE256, cSHAKE256).
functionNameReadOnlySpan<byte>- The function name N of cSHAKE, reserved for the functions defined by NIST; usually empty.
customizationReadOnlySpan<byte>- The customization string S of cSHAKE.
Exceptions
ArgumentOutOfRangeExceptionsecurityStrengthis not 128 or 256.
Properties
| Name | Description |
|---|---|
| SecurityStrength | Gets the security strength, in bits: 128 or 256. |
SecurityStrength
Shake.csMethods
| Name | Description |
|---|---|
| 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.csAppends a byte to the input.
Parameters
valuebyte- The byte to append.
Exceptions
InvalidOperationException- The output was read: call
Shake.Resetto hash new data.
AppendData(ReadOnlySpan<byte>)
Shake.csAppends data to the input.
public void AppendData(ReadOnlySpan<byte> data)Parameters
dataReadOnlySpan<byte>- The data to append.
Exceptions
InvalidOperationException- The output was read: call
Shake.Resetto hash new data.
HashData overloads
HashData(int, ReadOnlySpan<byte>, int, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Shake.csComputes 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
securityStrengthint- The security strength, in bits: 128 (SHAKE128, cSHAKE128) or 256 (SHAKE256, cSHAKE256).
sourceReadOnlySpan<byte>- The data to hash.
outputLengthint- The length of the output, in bytes.
functionNameReadOnlySpan<byte>- The function name N of cSHAKE, reserved for the functions defined by NIST; usually empty.
customizationReadOnlySpan<byte>- The customization string S of cSHAKE.
Returns
byte[] The output, of outputLength bytes.
Exceptions
ArgumentOutOfRangeExceptionsecurityStrengthis not 128 or 256, oroutputLengthis negative.
HashData(int, ReadOnlySpan<byte>, Span<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Shake.csComputes 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
securityStrengthint- The security strength, in bits: 128 (SHAKE128, cSHAKE128) or 256 (SHAKE256, cSHAKE256).
sourceReadOnlySpan<byte>- The data to hash.
destinationSpan<byte>- The buffer to fill with the output.
functionNameReadOnlySpan<byte>- The function name N of cSHAKE, reserved for the functions defined by NIST; usually empty.
customizationReadOnlySpan<byte>- The customization string S of cSHAKE.
Exceptions
ArgumentOutOfRangeExceptionsecurityStrengthis not 128 or 256.
Read(Span<byte>)
Shake.csReads the next bytes of the output. The first call ends the input: no more data can be appended.
Parameters
destinationSpan<byte>- The buffer to fill with the next bytes of the output.
ReadByte()
Shake.csReset()
Shake.csDiscards the input and the output, which may depend on secret data, so that the instance can hash new data.
public void Reset()