new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Bun · Runtime · all subjects

bun apis/hashing

16 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Hashing APIs in Bun

Bun provides multiple hashing APIs: Bun.password() for password hashing, Bun.hash() for general hashing, Bun.CryptoHasher for cryptographic hashing, and Bun.sha for SHA hashing.

Bun.password.verify async and verifySync

Bun.password.verify(password, hash) is an async function that verifies a password against a hash. It automatically detects the algorithm from the hash (whether PHC or MCF encoded) and uses the matching verification method. Bun.password.verifySync(password, hash) is the synchronous equivalent. Both return a boolean.

Bun.password.hashSync synchronous hashing

Bun.password.hashSync(password, params) is a synchronous version of Bun.password.hash that accepts the same params object for algorithm configuration. These functions are computationally expensive and can degrade application performance, so async versions are preferred.

bcrypt password hashing with SHA-512 prefix for long passwords

When using Bun.password.hash with algorithm "bcrypt", if the password is longer than 72 bytes, Bun automatically hashes it with SHA-512 before passing it to bcrypt. This prevents silent truncation that the standard bcrypt library performs on passwords over 72 bytes.

bcrypt Modular Crypt Format structure

The bcrypt hash format in Modular Crypt Format is: $2b$ROUNDS$SALT$HASH, where $2b is the algorithm identifier, ROUNDS is the log2 of the actual number of rounds (e.g., $10 means 2^10 = 1024 rounds), SALT is the 22-character salt, and HASH is the 31-character hash digest.

argon2 PHC format structure

The argon2 hash format in PHC (Password Hashing Competition) format is: $argon2id$v=VERSION$m=MEMORYCOST,t=TIMECOST,p=PARALLELISM$SALT$HASH. The algorithm identifier is $argon2id (or $argon2i/$argon2d), version is $v=19, memory cost, time cost (iterations), and parallelism are comma-separated parameters, followed by the salt and hash.

Bun.hash non-cryptographic hashing with Wyhash

Bun.hash(data, seed) computes a non-cryptographic 64-bit hash using Wyhash. The input can be a string, TypedArray, DataView, ArrayBuffer, or SharedArrayBuffer. The optional seed parameter is an integer; for seeds above Number.MAX_SAFE_INTEGER, pass a BigInt to avoid precision loss. Returns a bigint.

Bun.hash alternative non-cryptographic algorithms

Bun.hash provides additional non-cryptographic hashing algorithms as properties: Bun.hash.wyhash, Bun.hash.crc32, Bun.hash.adler32, Bun.hash.cityHash32, Bun.hash.cityHash64, Bun.hash.xxHash32, Bun.hash.xxHash64, Bun.hash.xxHash3, Bun.hash.murmur32v3, Bun.hash.murmur32v2, Bun.hash.murmur64v2, Bun.hash.rapidhash. Each accepts the same (data, seed) parameters. 32-bit hashes return a number and 64-bit hashes return a bigint.

Bun.password.hash async with argon2 and bcrypt

Bun.password.hash(password, params) is an async function that hashes a password using either argon2 or bcrypt. For argon2 variants, params accepts: algorithm ("argon2id" | "argon2i" | "argon2d"), memoryCost (memory usage in kibibytes, minimum 8), and timeCost (number of iterations). For bcrypt, params accepts: algorithm ("bcrypt") and cost (number between 4-31). The salt is generated automatically and included in the hash. The returned hash for bcrypt is encoded in Modular Crypt Format, and for argon2 in PHC format.

Bun.CryptoHasher constructor and update method

new Bun.CryptoHasher(algorithm) creates a hasher instance for the specified algorithm. The .update(data, encoding) method feeds data incrementally; data can be a string, TypedArray, or ArrayBuffer. For strings, encoding defaults to 'utf-8' and can also be: base64, base64url, hex, binary, utf8, utf-8, utf16le, latin1, ascii, ucs2, ucs-2.

Bun.CryptoHasher digest method

The .digest() method finalizes the hash. By default it returns a Uint8Array. Pass an encoding string (hex, base64, etc.) to get the hash as a string. Alternatively, pass a TypedArray to write the hash into an existing buffer instead of allocating a new one.

Bun.CryptoHasher HMAC mode with key

To compute HMAC digests with Bun.CryptoHasher, pass the key as a second parameter to the constructor: new Bun.CryptoHasher(algorithm, key). 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.

Bun.CryptoHasher HMAC instance is not reset after digest

Unlike non-HMAC Bun.CryptoHasher instances, HMAC instances are not reset after .digest() is called. Using the same HMAC instance again after .digest() throws an error. Methods like .copy() and .update() are supported before .digest() is called, but finalizing methods like .digest() cannot be called twice on the same instance.

Bun.CryptoHasher copy method for HMAC

The .copy() method creates a copy of the hasher instance, including for HMAC hashers. This allows you to branch off from the current state, update the copy, and call .digest() on it independently.

Bun implements Node.js crypto createHash and createHmac

Bun implements the createHash and createHmac functions from node:crypto in addition to the Bun-native hashing APIs, providing Node.js compatibility for cryptographic hashing.

Bun.CryptoHasher supported algorithms

Bun.CryptoHasher supports the following cryptographic hash algorithms: 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.

Give your agent this brain