Skip to main content
Bun implements the createHash and createHmac functions from node:crypto in addition to the Bun-native APIs documented below.

Bun.password

Bun.password is a collection of utility functions for hashing and verifying passwords with various cryptographically secure algorithms.
The second argument to Bun.password.hash is a params object that selects and configures the hashing algorithm.
The algorithm used to create the hash is stored in the hash itself. When using bcrypt, the returned hash is encoded in Modular Crypt Format for compatibility with most existing bcrypt implementations; with argon2 the result is encoded in the newer PHC format. The verify function detects the algorithm from the input hash, whether PHC- or MCF-encoded, and uses the matching verification method.
Synchronous versions of all functions are also available. These functions are computationally expensive, so a blocking API can degrade application performance.

Salt

Bun.password.hash generates a salt automatically and includes it in the hash.

bcrypt - Modular Crypt Format

In the following Modular Crypt Format hash (used by bcrypt): Input:
Output:
The format is composed of:
  • bcrypt: $2b
  • rounds: $10 - rounds (log2 of the actual number of rounds)
  • salt: Lyj9kHYZtiyfxh2G60TEfe
  • hash: qs7xkkGiEFFDi3iJGc50ZG/XJ1sxIFi
By default, the bcrypt library truncates passwords longer than 72 bytes. Instead of silently truncating, Bun.password.hash with the bcrypt algorithm hashes any password longer than 72 bytes with SHA-512 before passing it to bcrypt.

argon2 - PHC format

In the following PHC format hash (used by argon2): Input:
Output:
The format is composed of:
  • algorithm: $argon2id
  • version: $v=19
  • memory cost: 65536
  • iterations: t=2
  • parallelism: p=1
  • salt: $xXnlSvPh4ym5KYmxKAuuHVlDvy2QGHBNuI6bJJrRDOs
  • hash: $2YY6M48XmHn+s5NoBaL+ficzXajq2Yj8wut3r0vnrwI

Bun.hash

Bun.hash is a collection of utilities for non-cryptographic hashing. Non-cryptographic hashing algorithms are optimized for speed of computation over collision-resistance or security. The standard Bun.hash function uses Wyhash to generate a 64-bit hash from an input of arbitrary size.
The input can be a string, TypedArray, DataView, ArrayBuffer, or SharedArrayBuffer.
The second parameter is an optional integer seed. For 64-bit hashes, pass seeds above Number.MAX_SAFE_INTEGER as BigInt to avoid loss of precision.
Additional hashing algorithms are available as properties on Bun.hash. The API is the same for each; 32-bit hashes return a number and 64-bit hashes return a bigint.

Bun.CryptoHasher

Bun.CryptoHasher incrementally computes a hash of string or binary data with a cryptographic hash algorithm. The following algorithms are supported:
  • "blake2b256"
  • "blake2b512"
  • "blake2s256"
  • "md4"
  • "md5"
  • "ripemd160"
  • "sha1"
  • "sha224"
  • "sha256"
  • "sha384"
  • "sha512"
  • "sha512-224"
  • "sha512-256"
  • "sha3-224"
  • "sha3-256"
  • "sha3-384"
  • "sha3-512"
  • "shake128"
  • "shake256"
Feed data to the hasher incrementally with .update(), which accepts string, TypedArray, and ArrayBuffer.
For strings, an optional second parameter specifies the encoding (default 'utf-8'). The following encodings are supported:
Once all the data is fed in, compute the final hash with .digest(). By default, this method returns a Uint8Array containing the hash.
To get the hash as a string, pass an encoding to .digest():
Alternatively, .digest() can write the hash into an existing TypedArray instead of allocating a new one.

HMAC in Bun.CryptoHasher

Bun.CryptoHasher can compute HMAC digests. Pass the key to the constructor.
HMAC supports a more limited set of algorithms:
  • "blake2b256"
  • "blake2b512"
  • "md4"
  • "md5"
  • "ripemd160"
  • "sha1"
  • "sha224"
  • "sha256"
  • "sha384"
  • "sha512"
  • "sha512-224"
  • "sha512-256"
  • "sha3-224"
  • "sha3-256"
  • "sha3-384"
  • "sha3-512"
Unlike the non-HMAC Bun.CryptoHasher, the HMAC Bun.CryptoHasher instance is not reset after .digest() is called, and using the same instance again throws an error. Other methods like .copy() and .update() are supported (as long as it’s before .digest()), but methods like .digest() that finalize the hasher are not.