From 20854fb285a31f7c8940ceced0d3e3b2f5ffdea8 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Thu, 25 Sep 2025 14:28:42 -0800 Subject: [PATCH] node:crypto: add blake2s256 hasher (#22958) --- docs/api/hashing.md | 1 + packages/bun-types/bun.d.ts | 1 + src/bun.js/api/crypto/CryptoHasher.zig | 1 + src/bun.js/api/crypto/EVP.zig | 2 ++ test/js/bun/util/bun-cryptohasher.test.ts | 1 + test/js/node/crypto/crypto-hmac-algorithm.test.ts | 1 + test/js/node/crypto/crypto-oneshot.test.ts | 1 + test/js/node/crypto/crypto.test.ts | 3 +++ test/js/node/crypto/node-crypto.test.js | 1 - 9 files changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/api/hashing.md b/docs/api/hashing.md index 79d3c54e86..384a20243c 100644 --- a/docs/api/hashing.md +++ b/docs/api/hashing.md @@ -184,6 +184,7 @@ Bun.hash.rapidhash("data", 1234); - `"blake2b256"` - `"blake2b512"` +- `"blake2s256"` - `"md4"` - `"md5"` - `"ripemd160"` diff --git a/packages/bun-types/bun.d.ts b/packages/bun-types/bun.d.ts index 89f318c0cc..406e3d8466 100644 --- a/packages/bun-types/bun.d.ts +++ b/packages/bun-types/bun.d.ts @@ -5047,6 +5047,7 @@ declare module "bun" { type SupportedCryptoAlgorithms = | "blake2b256" | "blake2b512" + | "blake2s256" | "md4" | "md5" | "ripemd160" diff --git a/src/bun.js/api/crypto/CryptoHasher.zig b/src/bun.js/api/crypto/CryptoHasher.zig index e46b30f6b4..f40c324f0b 100644 --- a/src/bun.js/api/crypto/CryptoHasher.zig +++ b/src/bun.js/api/crypto/CryptoHasher.zig @@ -488,6 +488,7 @@ const CryptoHasherZig = struct { .{ "sha3-512", std.crypto.hash.sha3.Sha3_512 }, .{ "shake128", std.crypto.hash.sha3.Shake128 }, .{ "shake256", std.crypto.hash.sha3.Shake256 }, + .{ "blake2s256", std.crypto.hash.blake2.Blake2s256 }, }; inline fn digestLength(Algorithm: type) comptime_int { diff --git a/src/bun.js/api/crypto/EVP.zig b/src/bun.js/api/crypto/EVP.zig index 14a976997f..2d3f3b088f 100644 --- a/src/bun.js/api/crypto/EVP.zig +++ b/src/bun.js/api/crypto/EVP.zig @@ -21,6 +21,7 @@ pub const Algorithm = enum { // @"ecdsa-with-SHA1", blake2b256, blake2b512, + blake2s256, md4, md5, ripemd160, @@ -69,6 +70,7 @@ pub const Algorithm = enum { pub const map = bun.ComptimeStringMap(Algorithm, .{ .{ "blake2b256", .blake2b256 }, .{ "blake2b512", .blake2b512 }, + .{ "blake2s256", .blake2s256 }, .{ "ripemd160", .ripemd160 }, .{ "rmd160", .ripemd160 }, .{ "md4", .md4 }, diff --git a/test/js/bun/util/bun-cryptohasher.test.ts b/test/js/bun/util/bun-cryptohasher.test.ts index aa159a65e6..545c3f884e 100644 --- a/test/js/bun/util/bun-cryptohasher.test.ts +++ b/test/js/bun/util/bun-cryptohasher.test.ts @@ -185,6 +185,7 @@ describe("CryptoHasher", () => { const algorithms = [ "blake2b256", "blake2b512", + "blake2s256", "ripemd160", "rmd160", "md4", diff --git a/test/js/node/crypto/crypto-hmac-algorithm.test.ts b/test/js/node/crypto/crypto-hmac-algorithm.test.ts index 9125077470..88d7f38c73 100644 --- a/test/js/node/crypto/crypto-hmac-algorithm.test.ts +++ b/test/js/node/crypto/crypto-hmac-algorithm.test.ts @@ -21,6 +21,7 @@ test("createHmac works with various algorithm names", () => { const toRemove = [ "blake2b256", "blake2b512", + "blake2s256", "md4", "sha512-224", "sha512-256", diff --git a/test/js/node/crypto/crypto-oneshot.test.ts b/test/js/node/crypto/crypto-oneshot.test.ts index eeedbde20d..99529c3de3 100644 --- a/test/js/node/crypto/crypto-oneshot.test.ts +++ b/test/js/node/crypto/crypto-oneshot.test.ts @@ -39,6 +39,7 @@ describe("crypto.hash", () => { [ "blake2b256", "blake2b512", + "blake2s256", "ripemd160", "rmd160", "md4", diff --git a/test/js/node/crypto/crypto.test.ts b/test/js/node/crypto/crypto.test.ts index 9384543b93..16336cdcda 100644 --- a/test/js/node/crypto/crypto.test.ts +++ b/test/js/node/crypto/crypto.test.ts @@ -11,6 +11,7 @@ describe("CryptoHasher", () => { expect(CryptoHasher.algorithms).toEqual([ "blake2b256", "blake2b512", + "blake2s256", "md4", "md5", "ripemd160", @@ -34,6 +35,7 @@ describe("CryptoHasher", () => { const expected = { blake2b256: "256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610", blake2b512: "021ced8799296ceca557832ab941a50b4a11f83478cf141f51f933f653ab9fbcc05a037cddbed06e309bf334942c4e58cdf1a46e237911ccd7fcf9787cbc7fd0", + blake2s256: "9aec6806794561107e594b1f6a8a6b0c92a0cba9acf5e5e93cca06f781813b0b", md4: "aa010fbc1d14c795d86ef98c95479d17", md5: "5eb63bbbe01eeed093cb22bb8f5acdc3", ripemd160: "98c615784ccb5fe5936fbc0cbe9dfdb408d92f0f", @@ -55,6 +57,7 @@ describe("CryptoHasher", () => { const expectedBitLength = { blake2b256: 256, blake2b512: 512, + blake2s256: 256, md4: 128, md5: 128, ripemd160: 160, diff --git a/test/js/node/crypto/node-crypto.test.js b/test/js/node/crypto/node-crypto.test.js index e4b8874a40..2316d0ba5e 100644 --- a/test/js/node/crypto/node-crypto.test.js +++ b/test/js/node/crypto/node-crypto.test.js @@ -255,7 +255,6 @@ describe("createHash", () => { }; const unsupported = [ - "blake2s256", "id-rsassa-pkcs1-v1_5-with-sha3-224", "id-rsassa-pkcs1-v1_5-with-sha3-256", "id-rsassa-pkcs1-v1_5-with-sha3-384",