View Source B3 (B3 v0.1.0)

B3 is a pure Elixir implementation of BLAKE3, a modern cryptographic hash function.

As well as regular hash, B3 is a PRF, MAC, KDF, and XOF. That's a lot of acronyms!

B3 is a pure Elixir port of the BLAKE3 reference implementation. Zero dependencies will make compilation and deployments simple, but if its performance you're after, you may want to check out the blake3 package which uses Rustler to bind to the Rust BLAKE3 implementation.

Link to this section Summary

Functions

Derives a key from the given key material and context string.

Returns a hash of the given message.

Returns a keyed hash of the given message. Key must be 32 bytes.

Link to this section Functions

Link to this function

derive_key(material, context, opts \\ [])

View Source
@spec derive_key(binary(), String.t(), keyword()) :: binary()

Derives a key from the given key material and context string.

The context string should be globally unique and application specific.

accepted-options

Accepted options

  • :length - length of key (default: 32)
  • :encoding - encode key as (:hex or :base64)

example

Example

iex> B3.derive_key("test", "[Test app] 1 Oct 2022 - Test keys", encoding: :hex)
"79bb09c3d5f99890ef4a24316036dd7707e9c0e9d3315de168248e666639438d"
Link to this function

hash(message, opts \\ [])

View Source
@spec hash(
  binary(),
  keyword()
) :: binary()

Returns a hash of the given message.

accepted-options

Accepted options

  • :length - length of digest (default: 32)
  • :encoding - encode digest as (:hex or :base64)

example

Example

iex> B3.hash("test", encoding: :hex)
"4878ca0425c739fa427f7eda20fe845f6b2e46ba5fe2a14df5b1e32f50603215"
Link to this function

keyed_hash(message, key, opts \\ [])

View Source
@spec keyed_hash(binary(), binary(), keyword()) :: binary()

Returns a keyed hash of the given message. Key must be 32 bytes.

This mode removes the need for a seperate HMAC function.

accepted-options

Accepted options

  • :length - length of digest (default: 32)
  • :encoding - encode digest as (:hex or :base64)

example

Example

iex> B3.keyed_hash("test", "testkeytestkeytestkeytestkeytest", encoding: :hex)
"8bacb5b968184e269491c5022ec75d6b599ecf210ee3bb3a5208c1376f919202"