typescriptbeginner

Bcrypt Password Hash & Verify

Hash and verify passwords with bcrypt using configurable salt rounds and timing-safe comparison.

typescript
import bcrypt from 'bcrypt';

const SALT_ROUNDS = 12;

export async function hashPassword(plain: string): Promise<string> {
  return bcrypt.hash(plain, SALT_ROUNDS);
}

export async function verifyPassword(
  plain: string,
  hashed: string
): Promise<boolean> {
  return bcrypt.compare(plain, hashed);
}

// Usage
// const hash = await hashPassword('my-secure-password');
// const isValid = await verifyPassword('my-secure-password', hash);

Use Cases

  • User registration
  • Login authentication
  • Password reset

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.