javascript - password - Hash bcrypt $ 2y en Node.js
crypto npm (1)
Estoy tratando con una base de datos vieja con hashes $2y . Me he metido en esto un poco, también tropecé con el desbordamiento de la pila en la diferencia entre $2a y $2y .
Busqué en el módulo de nodos de bcrypt que parece generar y comparar solo hashes de $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
Encontré un sitio web que genera hashes de $2y así puedo probarlos con bcrypt .
Aquí hay un ejemplo de un hash $2y de la cadena helloworld .
helloworld:$2y$10$tRM7x9gGKhcAmpeqKEdhj.qRWCr4qoV1FU9se0Crx2hkMVNL2ktEW
Parece que el módulo no tiene forma de validar hashes $2y .
Aquí está mi prueba.
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)
Aquí están los resultados:
// hashesGeneratedUsingAspirineDotOrg
// [ false, false ]
// hashesGeneratedUsingBcryptModule
// [ true, true, true, true, true ]
// hashesGeneratedUsingAspirineDotOrgSwippedYForA
// [ false, false ]
Estoy perplejo sobre cómo puedo comparar hashes $2y en el nodo.
Hay otra pregunta / respuesta de desbordamiento de pila que dice que puedes cambiar los $2y a $2a pero eso aún falla para mí.
¡Actualizar!
Estaba usando el generador de forma incorrecta porque es un generador de contraseñas .htpasswd el nombre de usuario y la contraseña en este formato.
reggi helloworld
Y el resultado corresponde aquí:
reggi:$2y$10$iuC7GYH/h1Gl1aDmcpLFpeJXN9OZXZUYnaqD2NnGLQiVGQYBDtbtO
Antes yo como poniendo solo
helloword
Lo cual supongo que ha destruido una cadena vacía.
Con estos cambios cambiando el y a un a funciona en bcrypt . Y twin-bcrypt solo funciona.
- Al usar
bcryptcambiaya ana. - Al usar
twin-bcryptel hash simplemente funciona.
Cuando use http://aspirine.org/htpasswd_en.html, asegúrese de proporcionar un nombre de usuario y contraseña.
reggi helloworld
Entonces:
reggi:$2y$10$Am0Nf/B6.S/Wkpr6IVdIZeuHWNa/fqoLyTNmlyrSg22AjRf2vS.T.
Aquí hay un ejemplo de trabajo con bcrypt y twin-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)
Productos:
true
true