Plaintext password to hash
Promise resolving to bcrypt hash string
// Hash password during user registration
const plainPassword = 'userPassword123!';
const hashedPassword = await hashPassword(plainPassword);
console.log(hashedPassword); // '$2b$12$...' (bcrypt hash format)
// Store hashedPassword in database, never store plaintext
await userRepository.create({
email: 'user@example.com',
password: hashedPassword
});
Hash password using bcrypt with configurable salt rounds
Securely hashes a plaintext password using the bcrypt algorithm with a configurable number of salt rounds. The salt rounds determine the computational cost of hashing, with higher values providing better security at the cost of performance.
The function uses the BCRYPT_SALT_ROUNDS environment variable (default 12) to configure the hashing strength. This allows for security tuning based on deployment requirements and hardware capabilities.