在Node.js中$ 2y bcrypt哈希

我正在处理与$2y哈希旧数据库。 我已经深入了解了这一点,还偶然发现了 $2a$2y之间的差异。

我查看了bcrypt的节点模块,它似乎生成并比较了只有$2a哈希值。

  • https://github.com/ncb000gt/node.bcrypt.js/issues/175
  • https://github.com/ncb000gt/node.bcrypt.js/issues/349
  • https://github.com/ncb000gt/node.bcrypt.js/issues/213

我发现一个网站,生成$2y哈希,所以我可以testing他们与bcrypt

  • http://aspirine.org/htpasswd_en.html

以下是stringhelloworld$2y哈希示例。

 helloworld:$2y$10$tRM7x9gGKhcAmpeqKEdhj.qRWCr4qoV1FU9se0Crx2hkMVNL2ktEW 

似乎模块无法validation$2y哈希值。

这是我的testing。

 var Promise = require('bluebird') var bcrypt = require('bcrypt') var string = 'helloworld' Promise.promisifyAll(bcrypt) // bcrypt.genSalt(10, function(err, salt) { // bcrypt.hash(string, salt, function(err, hash) { // console.log(hash) // }) // }) var hashesGeneratedUsingBcryptModule = [ '$2a$10$6ppmIdlNEPwxWJskPaQ7l.d2fblh.GO6JomzrcpiD/hxGPOXA3Bsq', '$2a$10$YmpoYCDHzdAPMbd9B8l48.hkSnylnAPbOym367FKIEPa0ixY.o4b.', '$2a$10$Xfy3OPurrZEmbmmO0x1wGuFMdRTlmOgEMS0geg4wTj1vKcvXXjk06', '$2a$10$mYgwmdPZjiEncp7Yh5UB1uyPkoyavxrYcOIzzY4mzSniGpI9RbhL.', '$2a$10$dkBVTe2A2DAn24PUq1GZYe7AqL8WQqwOi8ZWBJAauOg60sk44DkOC' ] var hashesGeneratedUsingAspirineDotOrg = [ '$2y$10$MKgpAXLJkwx5tpijWX99Qek2gf/irwvp5iSfxuFoDswIjMIbj2.Ma', '$2y$10$tRM7x9gGKhcAmpeqKEdhj.qRWCr4qoV1FU9se0Crx2hkMVNL2ktEW' ] var hashesGeneratedUsingAspirineDotOrgSwippedYForA = [ '$2a$10$MKgpAXLJkwx5tpijWX99Qek2gf/irwvp5iSfxuFoDswIjMIbj2.Ma', '$2a$10$tRM7x9gGKhcAmpeqKEdhj.qRWCr4qoV1FU9se0Crx2hkMVNL2ktEW' ] hashesGeneratedUsingBcryptModule = hashesGeneratedUsingBcryptModule.map(hash => bcrypt.compareAsync(string, hash)) hashesGeneratedUsingAspirineDotOrg = hashesGeneratedUsingAspirineDotOrg.map(hash => bcrypt.compareAsync(string, hash)) hashesGeneratedUsingAspirineDotOrgSwippedYForA = hashesGeneratedUsingAspirineDotOrgSwippedYForA.map(hash => bcrypt.compareAsync(string, hash)) Promise.all(hashesGeneratedUsingBcryptModule) .tap(() => console.log('hashesGeneratedUsingBcryptModule')) .then(console.log) Promise.all(hashesGeneratedUsingAspirineDotOrg) .tap(() => console.log('hashesGeneratedUsingAspirineDotOrg')) .then(console.log) Promise.all(hashesGeneratedUsingAspirineDotOrgSwippedYForA) .tap(() => console.log('hashesGeneratedUsingAspirineDotOrgSwippedYForA')) .then(console.log) 

结果如下:

 // hashesGeneratedUsingAspirineDotOrg // [ false, false ] // hashesGeneratedUsingBcryptModule // [ true, true, true, true, true ] // hashesGeneratedUsingAspirineDotOrgSwippedYForA // [ false, false ] 

我很难以比较节点中的$2y哈希值。

还有另一个堆栈溢出问题/答案 ,说你可以将$2y更改为$2a但仍然失败。

更新!

我错误地使用了生成器,因为它是一个.htpasswd密码生成器,您必须以这种格式input用户名和密码。

 reggi helloworld 

和输出相对应的是:

 reggi:$2y$10$iuC7GYH/h1Gl1aDmcpLFpeJXN9OZXZUYnaqD2NnGLQiVGQYBDtbtO 

之前,我只是把

 helloword 

我正在假设一个空string。

随着这些变化, y变成了a bcrypt a作品。 而twin-bcrypt只是工作。

  • 当使用bcrypty更改为a
  • 使用twin-bcrypt ,散列正常。

使用http://aspirine.org/htpasswd_en.html时 ,请确保您提供了用户名和密码。

 reggi helloworld 

然后:

 reggi:$2y$10$Am0Nf/B6.S/Wkpr6IVdIZeuHWNa/fqoLyTNmlyrSg22AjRf2vS.T. 

这里有一个使用bcrypttwin-bcrypt例子。

 var twinBcrypt = require('twin-bcrypt') var bcrypt = require('bcrypt') var string = 'helloworld' var bcryptAttempt = bcrypt.compareSync(string, "$2y$10$Am0Nf/B6.S/Wkpr6IVdIZeuHWNa/fqoLyTNmlyrSg22AjRf2vS.T.".replace(/^\$2y/, "$2a")) console.log(bcryptAttempt) var twinBcryptAttempt = twinBcrypt.compareSync(string, "$2y$10$Am0Nf/B6.S/Wkpr6IVdIZeuHWNa/fqoLyTNmlyrSg22AjRf2vS.T.") console.log(twinBcryptAttempt) 

输出:

 true true