Current section
Files
Jump to
Current section
Files
src/gblake3.gleam
import gleam/bit_array
/// Hashes the given message to a 32 byte array
@external(erlang, "Elixir.B3", "hash")
@external(javascript, "./gblake3_ffi.mjs", "hash")
pub fn hash(message b: BitArray) -> BitArray
/// Hashes the given message to the provided number of bytes
@external(erlang, "Elixir.Gblake3", "hash_len")
@external(javascript, "./gblake3_ffi.mjs", "hash_to_length")
pub fn hash_to_length(
message b: BitArray,
output_length_bytes l: Int,
) -> BitArray
/// Hashes the provided message to a 32 bytes array
pub fn hash_string(message s: String) -> BitArray {
s |> bit_array.from_string |> hash
}
/// Hashes the provided message to the provided number of bytes
pub fn hash_string_to_length(
message s: String,
output_length_bytes l: Int,
) -> BitArray {
s |> bit_array.from_string |> hash_to_length(l)
}
/// Derives a message hash using the given key as an IV.
///
/// Errors if key is not exactly 32 bytes long
pub fn keyed_hash(message b: BitArray, key k: BitArray) -> Result(BitArray, Nil) {
case k |> bit_array.byte_size {
32 -> Ok(keyed_hash_inner(b, k))
_ -> Error(Nil)
}
}
pub fn keyed_hash_to_length(
message b: BitArray,
key k: BitArray,
length l: Int,
) -> Result(BitArray, Nil) {
case k |> bit_array.byte_size {
32 -> Ok(keyed_hash_to_length_inner(b, k, l))
_ -> Error(Nil)
}
}
@external(erlang, "Elixir.B3", "keyed_hash")
@external(javascript, "./gblake3_ffi.mjs", "keyed_hash")
fn keyed_hash_inner(bits: BitArray, key: BitArray) -> BitArray
@external(erlang, "Elixir.Gblake3", "keyed_hash_to_length")
@external(javascript, "./gblake3_ffi.mjs", "keyed_hash_to_length")
fn keyed_hash_to_length_inner(
bits: BitArray,
key: BitArray,
length: Int,
) -> BitArray
/// Derives a 32 byte key from the given key material and context string.
///
/// The context string should be globally unique and application specific
///
/// Should not be used for passwords since BLAKE3 is not a password hash
@external(erlang, "Elixir.B3", "derive_key")
@external(javascript, "./gblake3_ffi.mjs", "derive_key")
pub fn derive_key(
key_material km: BitArray,
context context: String,
) -> BitArray
/// Derives a 32 byte key from the given key material and context string.
///
/// The context string should be globally unique and application specific
///
/// Should not be used for passwords since BLAKE3 is not a password hash
pub fn derive_key_from_strings(
key_material km: String,
context context: String,
) -> BitArray {
km |> bit_array.from_string |> derive_key(context)
}
/// Derives a key of the provided length from the given key material and context string.
///
/// The context string should be globally unique and application specific
///
/// Should not be used for passwords since BLAKE3 is not a password hash
@external(erlang, "Elixir.Gblake3", "derive_key_len")
@external(javascript, "./gblake3_ffi.mjs", "derive_key_of_length")
pub fn derive_key_with_length(
key_material km: BitArray,
context context: String,
length l: Int,
) -> BitArray