Bun

雜湊

Bun 實作了 createHashcreateHmac 函式,來自 node:crypto,除了下方文件中的 Bun 原生 API。

Bun.password

Bun.password 是用於雜湊和驗證密碼的實用函式集合,具備各種安全加密演算法。

const password = "super-secure-pa$$word";

const hash = await Bun.password.hash(password);
// => $argon2id$v=19$m=65536,t=2,p=1$tFq+9AVr1bfPxQdh6E8DQRhEXg/M/SqYCNu6gVdRRNs$GzJ8PuBi+K+BVojzPfS5mjnC8OpLGtv8KJqF99eP6a4

const isMatch = await Bun.password.verify(password, hash);
// => true

傳遞給 Bun.password.hash 的第二個參數接受一個參數物件,讓您可以選擇並設定雜湊演算法。

const password = "super-secure-pa$$word";

// use argon2 (default)
const argonHash = await Bun.password.hash(password, {
  algorithm: "argon2id", // "argon2id" | "argon2i" | "argon2d"
  memoryCost: 4, // memory usage in kibibytes
  timeCost: 3, // the number of iterations
});

// use bcrypt
const bcryptHash = await Bun.password.hash(password, {
  algorithm: "bcrypt",
  cost: 4, // number between 4-31
});

用於建立雜湊的演算法儲存在雜湊本身中。使用 bcrypt 時,傳回的雜湊會編碼成 模組化加密格式,以與大多數現有的 bcrypt 實作相容;使用 argon2 時,結果會編碼成較新的 PHC 格式

verify 函數會根據輸入雜湊自動偵測演算法,並使用正確的驗證方法。它可以從 PHC 或 MCF 編碼的雜湊正確推斷演算法。

const password = "super-secure-pa$$word";

const hash = await Bun.password.hash(password, {
  /* config */
});

const isMatch = await Bun.password.verify(password, hash);
// => true

所有函數的同步版本也可用。請記住,這些函數在運算上很耗費資源,因此使用封鎖 API 可能會降低應用程式的效能。

const password = "super-secure-pa$$word";

const hash = Bun.password.hashSync(password, {
  /* config */
});

const isMatch = Bun.password.verifySync(password, hash);
// => true

Bun.hash

Bun.hash 是用於非加密雜湊的公用程式集合。非加密雜湊演算法針對運算速度進行最佳化,而非抗碰撞或安全性。

標準的 Bun.hash 函數使用 Wyhash 從任意大小的輸入產生 64 位元雜湊。

Bun.hash("some data here");
// 11562320457524636935n

輸入可以是字串、TypedArrayDataViewArrayBufferSharedArrayBuffer

const arr = new Uint8Array([1, 2, 3, 4]);

Bun.hash("some data here");
Bun.hash(arr);
Bun.hash(arr.buffer);
Bun.hash(new DataView(arr.buffer));

選擇性地,可以將整數種子指定為第二個參數。對於 64 位元雜湊,Number.MAX_SAFE_INTEGER 以上的種子應給予 BigInt,以避免精確度損失。

Bun.hash("some data here", 1234);
// 15724820720172937558n

額外的雜湊演算法可用於 Bun.hash 的屬性。每個 API 都是相同的,只將 32 位元雜湊的回傳類型從數字變更為 64 位元雜湊的 bigint。

Bun.hash.wyhash("data", 1234); // equivalent to Bun.hash()
Bun.hash.crc32("data", 1234);
Bun.hash.adler32("data", 1234);
Bun.hash.cityHash32("data", 1234);
Bun.hash.cityHash64("data", 1234);
Bun.hash.murmur32v3("data", 1234);
Bun.hash.murmur32v2("data", 1234);
Bun.hash.murmur64v2("data", 1234);

Bun.CryptoHasher

Bun.CryptoHasher 是通用公用程式類別,可讓您使用一系列加密雜湊演算法遞增計算字串或二進位資料的雜湊。支援下列演算法

  • "blake2b256"
  • "blake2b512"
  • "md4"
  • "md5"
  • "ripemd160"
  • "sha1"
  • "sha224"
  • "sha256"
  • "sha384"
  • "sha512"
  • "sha512-224"
  • "sha512-256"
  • "sha3-224"
  • "sha3-256"
  • "sha3-384"
  • "sha3-512"
const hasher = new Bun.CryptoHasher("sha256");
hasher.update("hello world");
hasher.digest();
// Uint8Array(32) [ <byte>, <byte>, ... ]

初始化後,資料可以使用 .update() 遞增提供給雜湊器。此方法接受 stringTypedArrayArrayBuffer

const hasher = new Bun.CryptoHasher("sha256");

hasher.update("hello world");
hasher.update(new Uint8Array([1, 2, 3]));
hasher.update(new ArrayBuffer(10));

如果傳遞 string,可以使用選擇性的第二個參數指定編碼(預設為 'utf-8')。支援下列編碼

二進位編碼"base64" "base64url" "hex" "binary"
字元編碼"utf8" "utf-8" "utf16le" "latin1"
舊式字元編碼"ascii" "binary" "ucs2" "ucs-2"
hasher.update("hello world"); // defaults to utf8
hasher.update("hello world", "hex");
hasher.update("hello world", "base64");
hasher.update("hello world", "latin1");

資料輸入雜湊器後,可以使用 .digest() 計算最終雜湊值。預設情況下,此方法會傳回包含雜湊值的 Uint8Array

const hasher = new Bun.CryptoHasher("sha256");
hasher.update("hello world");

hasher.digest();
// => Uint8Array(32) [ 185, 77, 39, 185, 147, ... ]

.digest() 方法可以選擇將雜湊值傳回字串。若要這麼做,請指定編碼

hasher.digest("base64");
// => "uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="

hasher.digest("hex");
// => "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"

或者,此方法可以將雜湊值寫入預先存在的 TypedArray 實例。這在某些效能敏感的應用程式中可能是必要的。

const arr = new Uint8Array(32);

hasher.digest(arr);

console.log(arr);
// => Uint8Array(32) [ 185, 77, 39, 185, 147, ... ]