Skip to main content
The second argument to
The algorithm used to create the hash is stored in the hash itself. When using
Synchronous versions of all functions are also available. Keep in mind that these functions are computationally expensive, so using a blocking API may degrade application performance.
Output:
The format is composed of:
So instead of sending bcrypt a 500-byte password silently truncated to 72 bytes, Bun will hash the password using SHA-512 and send the hashed password to bcrypt (only if it exceeds 72 bytes). This is a more secure default behavior.
Output:
The format is composed of:
The input can be a string,
Optionally, an integer seed can be specified as the second parameter. For 64-bit hashes seeds above
Additional hashing algorithms are available as properties on
Once initialized, data can be incrementally fed to to the hasher using
If a
After the data has been feed into the hasher, a final hash can be computed using
The
Alternatively, the method can write the hash into a pre-existing
HMAC in
When using HMAC, a more limited set of algorithms are supported:
Utilities
Hashing
Bun provides a set of utility functions for hashing and verifying passwords with various cryptographically secure algorithms
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.
Bun.password.hash accepts a params object that lets you pick and configure the hashing algorithm.
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 automatically detects the algorithm based on the input hash and use the correct verification method. It can correctly infer the algorithm from both PHC- or MCF-encoded hashes.
Salt
When you useBun.password.hash, a salt is automatically generated and included in the hash.
bcrypt - Modular Crypt Format
In the following Modular Crypt Format hash (used bybcrypt):
Input:
bcrypt:$2brounds:$10- rounds (log2 of the actual number of rounds)salt:$Lyj9kHYZtiyfxh2G60TEfeqs7xkkGiEFFDi3iJGc50ZG/XJ1sxIFihash:$GzJ8PuBi+K+BVojzPfS5mjnC8OpLGtv8KJqF99eP6a4
Bun.password.hash a password longer than 72 bytes and use the bcrypt algorithm, the password will be hashed via SHA-512 before being passed to bcrypt.
argon2 - PHC format
In the following PHC format hash (used byargon2):
Input:
algorithm:$argon2idversion:$v=19memory cost:65536iterations:t=2parallelism:p=1salt:$xXnlSvPh4ym5KYmxKAuuHVlDvy2QGHBNuI6bJJrRDOshash:$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 functions uses Wyhash to generate a 64-bit hash from an input of arbitrary size.
TypedArray, DataView, ArrayBuffer, or SharedArrayBuffer.
Number.MAX_SAFE_INTEGER should be given as BigInt to avoid loss of precision.
Bun.hash. The API is the same for each, only changing the return type from number for 32-bit hashes to bigint for 64-bit hashes.
Bun.CryptoHasher
Bun.CryptoHasher is a general-purpose utility class that lets you incrementally compute a hash of string or binary data using a range of cryptographic hash algorithms. The following algorithms are supported:
"blake2b256""blake2b512""md4""md5""ripemd160""sha1""sha224""sha256""sha384""sha512""sha512-224""sha512-256""sha3-224""sha3-256""sha3-384""sha3-512""shake128""shake256"
.update(). This method accepts string, TypedArray, and ArrayBuffer.
string is passed, an optional second parameter can be used to specify the encoding (default 'utf-8'). The following encodings are supported:
.digest(). By default, this method returns a Uint8Array containing the hash.
.digest() method can optionally return the hash as a string. To do so, specify an encoding:
TypedArray instance. This may be desirable in some performance-sensitive applications.
HMAC in Bun.CryptoHasher
Bun.CryptoHasher can be used to compute HMAC digests. To do so, pass the key to the constructor.
"blake2b512""md5""sha1""sha224""sha256""sha384""sha512-224""sha512-256""sha512"
Bun.CryptoHasher, the HMAC Bun.CryptoHasher instance is not reset after .digest() is called, and attempting to use the same instance again will throw 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.
