我面临的问题是,当代码在其他计算机上使用时,bcrypt.comparesync可能会返回true。然而,在我的计算机上,无论被比较的文本是相同还是不同,它总是返回false。这是不是我面临的某种bug,因为我的代码在过去是可以工作的,但突然它停止了工作。为什么会这样呢?
我的代码:
const bCrypt = require('bcrypt');
var WebToken = require('jsonwebtoken');
var SecretKey = "Somesecretkey";
class ProfilesDB
{
getLoginCredentials(request, respond){
var username = request.body.username;
var password = request.body.password;
var sql = "SELECT password FROM restaurant_review.profile WHERE username = ?";
var profileValues = [username,password];
db.query(sql, profileValues, function(error, result)
{
if(error)
{
throw error;
}
else
{
//console.log(result[0].password);
const hash = result[0].password;
var flag = bCrypt.compareSync(profileValues[1],hash);
if (flag)
{
var token = WebToken.sign(username,SecretKey);
respond.json({result:token});
}
else
{
respond.json({result:"Invaild"});
}
}
});
}
getAllProfiles(request, respond)
{
var sql = "SELECT * FROM restaurant_review.profile";
db.query(sql, function(error, results){
if(error)
{
throw error;
}
else
{
respond.json(results);
}
});
}
addProfile(request, respond)
{
//Creating a new profile class, calls for a new profile, to create a new "profile"
var profileObject = new Profile(null, request.body.firstName,
request.body.lastName, request.body.username, request.body.password,
request.body.email);
//To encrypt the password
profileObject.password = bCrypt.hashSync(profileObject.password,10);
//Question mark is used as a place holder.
var sql = "INSERT INTO restaurant_review.profile (firstName, lastName, username, password, email) Values(?,?,?,?,?)";
var profileValues = [profileObject.getFirstName(),
profileObject.getLastName(), profileObject.getUsername(),
profileObject.getPassword(), profileObject.getEmail()];
db.query(sql, profileValues, function(error, result){
if(error)
{
throw error;
}
else
{
respond.json(result);
}
});
}发布于 2021-01-11 11:01:16
如果它可以在其他计算机上运行,但不能在您的计算机上运行,那么它可能来自bcrypt包以外的其他东西,例如,数据库未配置属性等。也许您的数据库表中的密码字段有字符数限制,而哈希密码超过了这个数字?检查表中密码字段的类型,并确保它不是具有小值的varchar( x )。
https://stackoverflow.com/questions/65660678
复制相似问题