我如何将密码与哈希密码数组进行比较
How can i compare a password with array of hash passowrds
"previousPasswords": [
"a$.6cY1diu9kkAhQCBxlVb7unBCxHGCblucOHp4g/z6rE9a3/YQEHqq",
"a$T.G1BZ3SCCY4p1H6sXlpz.daJuX6s/YbFReGmQlWIOMCZcwUPIhE6",
"a$ZeW2A6YQw4dI07PDwwql/.vA4tJdvkq9EAcduIEnpFuAzjvXURi5a",
"a$iuHu21hA9J55ai1gWrJ96OKfl7X0sD/FzT7nd0gKzw38NTYOXdiWG",
"a$ID/fYCOJ0KOb010f7OZf1ON7RNQJwcMT1px5dBpQx2.juoBugiEQe",
"a$lIvpPNK6lMs4CpAzBl1wZOjq9HF12lIffs7TybaWqo8v7g76KJ3s2",
"a$Brz/WRZGKBEzLJEpzrFEwuRccVAI1K7KEXfv5GVxFV4H34r9WhMke",
"a$QD/6DDQD2n1KWoYO6PBPtu4rp1HfX2sSy2uBbWj5d0tw.EmaEl/Yy",
"a$sP03DLwC.yPt.c3i.CdD0OmYBS6m6XQpPycuFE49GLPWvHEvjiBXW"
],
**现在我想将传入的密码与哈希密码数组进行比较**
const ifPrevPass = await Users.findOne({
_id: req.user._id,
previousPasswords: body.password,
});
我想出了这个解决方案,现在可以使用了
async function checkIfPassExists(user, body) {
for (var i = 0; i < user.previousPasswords.length; i++) {
const resultOfCompa = await bcrypt.compare(
body.password,
user.previousPasswords[i]
);
if (resultOfCompa) {
passMatched = true;
}
}
return passMatched;
}
const ifPrevPass = await checkIfPassExists(user, body);
if (ifPrevPass) {
return res.status(400).json({
status: "error",
message: "You have already used this password",
});
}
"previousPasswords": [
"a$.6cY1diu9kkAhQCBxlVb7unBCxHGCblucOHp4g/z6rE9a3/YQEHqq",
"a$T.G1BZ3SCCY4p1H6sXlpz.daJuX6s/YbFReGmQlWIOMCZcwUPIhE6",
"a$ZeW2A6YQw4dI07PDwwql/.vA4tJdvkq9EAcduIEnpFuAzjvXURi5a",
"a$iuHu21hA9J55ai1gWrJ96OKfl7X0sD/FzT7nd0gKzw38NTYOXdiWG",
"a$ID/fYCOJ0KOb010f7OZf1ON7RNQJwcMT1px5dBpQx2.juoBugiEQe",
"a$lIvpPNK6lMs4CpAzBl1wZOjq9HF12lIffs7TybaWqo8v7g76KJ3s2",
"a$Brz/WRZGKBEzLJEpzrFEwuRccVAI1K7KEXfv5GVxFV4H34r9WhMke",
"a$QD/6DDQD2n1KWoYO6PBPtu4rp1HfX2sSy2uBbWj5d0tw.EmaEl/Yy",
"a$sP03DLwC.yPt.c3i.CdD0OmYBS6m6XQpPycuFE49GLPWvHEvjiBXW"
],
**现在我想将传入的密码与哈希密码数组进行比较**
const ifPrevPass = await Users.findOne({
_id: req.user._id,
previousPasswords: body.password,
});
我想出了这个解决方案,现在可以使用了
async function checkIfPassExists(user, body) {
for (var i = 0; i < user.previousPasswords.length; i++) {
const resultOfCompa = await bcrypt.compare(
body.password,
user.previousPasswords[i]
);
if (resultOfCompa) {
passMatched = true;
}
}
return passMatched;
}
const ifPrevPass = await checkIfPassExists(user, body);
if (ifPrevPass) {
return res.status(400).json({
status: "error",
message: "You have already used this password",
});
}