Skip to content

Instantly share code, notes, and snippets.

@Gwash3189
Created November 1, 2024 00:11
Show Gist options
  • Save Gwash3189/2b7096a4fc5d540e94bb6350ea265638 to your computer and use it in GitHub Desktop.
Save Gwash3189/2b7096a4fc5d540e94bb6350ea265638 to your computer and use it in GitHub Desktop.
const bcrypt = require('bcrypt');
// Generate a salt and hash password
async function hashPassword(password) {
const COST_FACTOR = 12; // Higher = more secure but slower
try {
const hash = await bcrypt.hash(password, COST_FACTOR);
return hash;
} catch (err) {
console.error('Hashing error:', err);
throw new Error('Password hashing failed');
}
}
// Verify password
async function verifyPassword(password, storedHash) {
try {
return await bcrypt.compare(password, storedHash);
} catch (err) {
console.error('Verification error:', err);
throw new Error('Password verification failed');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment