升级现有的bcrypt散列轮数

我有一个数据库,对于我们的用例来说,大多数哈希值的循环次数太less。 我想升级这些到更高的轮次/迭代计数,希望与node.bcrypt.js库。

例:

我在我的数据库中有这样的东西

'$2a$05$Ss068.p/.i4IRzrYoLM/U.ETLpzwrBs2vVfViqgfC5bI4i3BGClZC' //From: bcrypt.hashSync("hello world", 5) 

我希望它成为这样的东西:

 '$2a$10$6sZOFUEWdVMHoCsgF0k1..RhwoD7VmLlLc5.67/Qw81/XuSuNIOcO' //From: bcrypt.hashSync("hello world", 10) 

有没有办法做到这一点? 我在想这个API可能是这样的:

 //Non-existing api; var hash = '$2a$05$Ss068.p/.i4IRzrYoLM/U.ETLpzwrBs2vVfViqgfC5bI4i3BGClZC'; const roundCount = 10 bcrypt.upgradeHashRoundCount(hash, roundCount) .then(function(upgradedHash){ console.log(upgradedHash) }) .catch(function(error){ console.error("Not a bcrypt hash, or has higher round-count than provided round count") }) 

编辑澄清:

我想做一个批处理的工作,我得到所有的哈希,并进行每一个升级,没有原始密码可用。 由于bcrypt本质上是循环的,所以我认为理论上可以多走一些,然后存储回去。

想到几种方法。 关于bcrypt的好处是,这些回合存储在盐本身中,因此是相互独立的。 意思是,你可以顺利过渡而不破坏旧的密码。

所以有两点build议:

  • 你可以开始使用更高的salt来改变所有新的密码/密码。 这个职业很明显,你只需要join哈希轮,你就完成了。 骗人的是,它可能需要永远,直到所有的密码存储与更高的回合。

  • 如果他们仍然有更低的总数,您可以更新每次成功login的密码。 你可以使用getRounds(hash) 。 这样你的密码会很快更新(一旦成功使用一次)

这样的:

 function checkPw(pw, user) { return bcrypt.compare(pw, user.hash) .then(success => { if(success && bcrypt.getRounds(hash) < 10) { return updateHash(pw, user).then(() => success); } return success; }) } function updateHash(pw, user) { return bcrypt.hash(pw, 10).then((newHash) => { user.hash = newHash; // update user in db return user.save(); }); } checkPw('abc', { id: 123, hash: '$2a$04$7AiVQRTAEPWFwldS7CB6VuQcMSenrPlpoEEGdMyQDE8BxcxcJXPgG' })