Skip to content

Bun.password.hash() 함수는 Bun 에서 비밀번호를 안전하게 해시하기 위한 빠르고 내장된 메커니즘을 제공합니다. 서드파티 의존성이 필요하지 않습니다.

ts
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/...

기본적으로 이는 Argon2id 알고리즘을 사용합니다. 다른 알고리즘을 사용하거나 해싱 매개변수를 구성하려면 Bun.password.hash() 에 두 번째 인수를 전달하세요.

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

// argon2 사용 (기본값)
const argonHash = await Bun.password.hash(password, {
  memoryCost: 4, // 키비바이트 단위의 메모리 사용량
  timeCost: 3, // 반복 횟수
});

Bun 은 또한 bcrypt 알고리즘을 구현합니다. 이를 사용하려면 algorithm: "bcrypt" 를 지정하세요.

ts
// bcrypt 사용
const bcryptHash = await Bun.password.hash(password, {
  algorithm: "bcrypt",
  cost: 4, // 4-31 사이의 숫자
});

Bun.password.verify() 를 사용하여 비밀번호를 검증하세요. 알고리즘과 그 매개변수는 해시 자체에 저장되므로 구성을 다시 지정할 필요가 없습니다.

ts
const password = "super-secure-pa$$word";
const hash = await Bun.password.hash(password);

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

전체 문서는 문서 > API > 해싱 을 참조하세요.

Bun by www.bunjs.com.cn 편집