Bcrypt Password Hash & Verify
Hash and verify passwords with bcrypt using configurable salt rounds and timing-safe comparison.
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.
In-Memory Rate Limiter for Express
Token bucket rate limiter middleware for Express with configurable window and max requests per IP.
JWT Refresh Token Rotation
Implement secure token rotation with short-lived access tokens and one-time-use refresh tokens.
Edge Middleware Rate Limiter
Rate limit API requests at the edge using a sliding window counter with configurable thresholds.
Row-Level Security Policies
Enforce data access rules at the database level with PostgreSQL Row-Level Security policies.