24 lines
649 B
JavaScript
24 lines
649 B
JavaScript
const bcrypt = require('bcrypt')
|
|
|
|
async function hashPassword(password) {
|
|
try {
|
|
const hashedPassword = await bcrypt.hash(password, 10)
|
|
return hashedPassword
|
|
} catch (error) {
|
|
console.error('Error hashing password:', error)
|
|
throw error // 重新抛出错误以便调用方处理
|
|
}
|
|
}
|
|
|
|
async function comparePassword(password, hashedPassword) {
|
|
try {
|
|
const isMatch = await bcrypt.compare(password, hashedPassword)
|
|
return isMatch
|
|
} catch (error) {
|
|
console.error('Error comparing password:', error)
|
|
throw error // 重新抛出错误以便调用方处理
|
|
}
|
|
}
|
|
|
|
module.exports = { hashPassword, comparePassword }
|