libsodium_crypto_hash_sha256 (libsodium v3.0.1)

View Source

SHA-256, producing bytes/0 byte digests, either in one shot or incrementally.

The streaming functions thread an opaque state/0 through init/0, update/2, and final/1; each call returns a new state and leaves its argument unchanged. Updates over large inputs yield to other processes between chunks.

SHA-2 is vulnerable to length extension attacks; unless SHA-256 is required for interoperability, prefer libsodium_crypto_generichash.

Digest = libsodium_crypto_hash_sha256:crypto_hash_sha256(<<"hello world">>),
32 = byte_size(Digest),
State0 = libsodium_crypto_hash_sha256:init(),
State1 = libsodium_crypto_hash_sha256:update(State0, <<"hello ">>),
State2 = libsodium_crypto_hash_sha256:update(State1, <<"world">>),
Digest = libsodium_crypto_hash_sha256:final(State2).

Summary

Types

A crypto_hash_sha256_BYTES (32-byte) binary.

An opaque native crypto_hash_sha256_state.

Functions

Return crypto_hash_sha256_bytes(), in bytes.

Hash In, returning a bytes/0 byte digest.

Finish the streaming hash in State, returning a bytes/0 byte digest.

Start a streaming hash, returning a statebytes/0 byte state.

Return crypto_hash_sha256_statebytes(), in bytes.

Absorb In into State and return the updated state.

Types

out()

-type out() :: <<_:256>>.

A crypto_hash_sha256_BYTES (32-byte) binary.

state()

An opaque native crypto_hash_sha256_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

bytes()

-spec bytes() -> Result when Result :: 32.

Return crypto_hash_sha256_bytes(), in bytes.

crypto_hash_sha256(In)

-spec crypto_hash_sha256(In) -> Result when In :: binary(), Result :: out() | -1.

Hash In, returning a bytes/0 byte digest.

final(State)

-spec final(State) -> Result when State :: state(), Result :: out() | -1.

Finish the streaming hash in State, returning a bytes/0 byte digest.

init()

-spec init() -> Result when Result :: state() | -1.

Start a streaming hash, returning a statebytes/0 byte state.

statebytes()

-spec statebytes() -> Result when Result :: 104.

Return crypto_hash_sha256_statebytes(), in bytes.

update(State, In)

-spec update(State, In) -> Result when State :: state(), In :: binary(), Result :: state() | -1.

Absorb In into State and return the updated state.