libsodium_crypto_xof_shake128 (libsodium v3.0.1)
View SourceSHAKE128 (FIPS 202) is an extendable-output function (XOF) built on the Keccak-f[1600] permutation with 24 rounds. It hashes an input of any length into an output of any requested length and offers 128-bit security against collision and preimage attacks.
The standard domain byte domain_standard/0 (16#1F) produces
SHAKE128 as specified; init_with_domain/1 accepts another domain
byte to derive an independent, non-standard function for domain
separation.
The streaming functions thread an opaque state/0 through init/0,
update/2, and squeeze/2. Every call returns a new state and leaves its
argument unchanged. Squeezing continues the output stream: consecutive
squeeze/2 calls on the returned states produce the same bytes as one
longer squeeze, so output can be read incrementally. Updates over large
inputs yield to other processes between chunks.
Out = libsodium_crypto_xof_shake128:crypto_xof_shake128(32, <<"hello world">>),
32 = byte_size(Out),
State0 = libsodium_crypto_xof_shake128:init(),
State1 = libsodium_crypto_xof_shake128:update(State0, <<"hello ">>),
State2 = libsodium_crypto_xof_shake128:update(State1, <<"world">>),
{State3, First} = libsodium_crypto_xof_shake128:squeeze(State2, 16),
{_State4, Second} = libsodium_crypto_xof_shake128:squeeze(State3, 16),
Out = <<First/binary, Second/binary>>.
Summary
Functions
Return crypto_xof_shake128_blockbytes(), in bytes.
Hash In with SHAKE128 and the standard domain byte, returning
Outlen bytes of output.
Return crypto_xof_shake128_domain_standard().
Start a streaming SHAKE128 hash with the standard domain byte domain_standard/0.
Start a streaming SHAKE128 hash with the domain separation byte
Domain (an integer from 0 to 255).
Read the next Outlen bytes of output from State.
Return crypto_xof_shake128_statebytes(), in bytes.
Absorb In into State and return the updated state.
Types
-type state() :: libsodium_nif:crypto_xof_shake128_state().
An opaque native crypto_xof_shake128_state.
Operations never modify a state: each returns a new one, so earlier states remain valid and can be reused or shared between processes. States cannot be serialized; they exist only within the running VM.
Functions
-spec blockbytes() -> Result when Result :: 168.
Return crypto_xof_shake128_blockbytes(), in bytes.
-spec crypto_xof_shake128(Outlen, In) -> Result when Outlen :: non_neg_integer(), In :: binary(), Result :: binary() | -1.
Hash In with SHAKE128 and the standard domain byte, returning
Outlen bytes of output.
-spec domain_standard() -> Result when Result :: 31.
Return crypto_xof_shake128_domain_standard().
-spec init() -> Result when Result :: state() | -1.
Start a streaming SHAKE128 hash with the standard domain byte domain_standard/0.
-spec init_with_domain(Domain) -> Result when Domain :: 0..255, Result :: state() | -1.
Start a streaming SHAKE128 hash with the domain separation byte
Domain (an integer from 0 to 255).
-spec squeeze(State, Outlen) -> Result when State :: state(), Outlen :: non_neg_integer(), Result :: {state(), binary()} | -1.
Read the next Outlen bytes of output from State.
Returns {NewState, Out}. Squeezing NewState again continues the
output stream where Out ended, so successive squeezes produce the
same bytes as a single longer one. State itself is unchanged and
squeezing it again repeats Out.
-spec statebytes() -> Result when Result :: 256.
Return crypto_xof_shake128_statebytes(), in bytes.
Absorb In into State and return the updated state.
Returns -1 if State has already been squeezed; absorbing after
squeezing is not supported.