跳转到内容

密码哈希

createPasswordHasher 提供了密码哈希与验证能力,基于 Bun.password 实现,默认使用 bcrypt 算法。禁止明文存储密码。

import { createPasswordHasher } from "@ventostack/auth";
const hasher = createPasswordHasher();
// 哈希密码
const hash = await hasher.hash("my-secure-password");
// "$2b$10$..."
// 验证密码
const valid = await hasher.verify("my-secure-password", hash); // true
const invalid = await hasher.verify("wrong-password", hash); // false
选项类型默认值说明
algorithm"bcrypt""bcrypt"哈希算法,目前仅支持 bcrypt
costnumber10bcrypt 成本因子,值越大越安全但越慢
// 提高安全强度(适合后台服务)
const hasher = createPasswordHasher({ cost: 12 });
// 开发环境可降低成本加快速度
const hasher = createPasswordHasher({ cost: 4 });
interface PasswordHasher {
hash(password: string): Promise<string>;
verify(password: string, hash: string): Promise<boolean>;
}
interface PasswordHasherOptions {
algorithm?: "bcrypt";
cost?: number;
}
  • 基于 Bun.password,仅在 Bun 运行时下可用
  • bcrypt 成本因子每增加 1,哈希时间约翻倍,生产环境建议 10-12
  • hashverify 均为异步方法,避免在主线程阻塞
  • algorithm 传入非 "bcrypt" 的值会抛出异常