Created
November 1, 2024 00:11
-
-
Save Gwash3189/2b7096a4fc5d540e94bb6350ea265638 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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