BLAKE2b implemented in WebAssembly.
All credit to the original authors Jean-Philippe Aumasson, Samuel Neves, Zooko Wilcox-O'Hearn, and Christian Winnerlein, as well as open-source contributors dcposch, mafintosh, and emilbayes for porting the reference implementation to JavaScript and WebAssembly.
import { blake2b } from "https://deno.land/x/blake2b/mod.ts";
console.log('BLAKE2b512 of msg "food":', blake2b("food", "utf8", "hex"));
with one function call:
export function blake2b(
msg: string | Uint8Array,
inputEncoding?: string,
outputEncoding?: string,
bytes: number = BYTES_MAX,
key?: string | Uint8Array,
salt?: string | Uint8Array,
personal?: string | Uint8Array
): string | Uint8Arrays
If any multiple of msg
, key
, salt
or personal
are passed as strings they must have the same encoding.
Example
const msg: Uint8Array = Uint8Array.from([ 65, 67, 65, 66 ]);
const digest: Uint8Array = blake2b(msg);
Create a Blake2b
instance. bytes
must indicate the desired digest length. If in doubt about your digest length requirements, just fall back to Blake2b.BYTES_MAX
, which yields a 64-byte digest. If key
is given the digest is essentially a MAC. The key
length can be any integer in 0..64
. Again, if in doubt about your key
length requirements, settle for a paranoid 64
which is Blake2b.KEYBYTES_MAX
and sleep tight. salt
and personal
must both have length 16
if set. They can be used for salting and defining unique hash functions for multiple applications respectively.
Update a Blake2b
instance. Can be called multiple times. inputEncoding
can be one of "utf8"
, "hex"
, or "base64"
. If the input is string and no inputEncoding
is provided utf8
-encoding is assumed.
Obtain a hash digest. To get a string digest set outputEncoding
to any of "utf8"
, "hex"
, or "base64"
.
A readonly
instance property indicating the digest length defined at instantiation.
There are a couple handy exported constants you should be aware of:
BYTES_MIN // 1
BYTES_MAX // 64
INPUTBYTES_MIN // 0
INPUTBYTES_MAX // 2n ** 128n - 1n
KEYBYTES_MIN // 0
KEYBYTES_MAX // 64
SALTBYTES // 16
PERSONALBYTES // 16