Ho a che fare con un vecchio database con hash $2y
. Ho scavato un po 'su questo, anche inciampato su the stack overflow sulla differenza tra $2a
e $2y
.
Ho esaminato il modulo nodo per bcrypt
che sembra generare e confrontare solo gli hash $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
ho trovato un sito che genera $2y
hash così li ho possono testare con bcrypt
.
Ecco un esempio di una $2y
hash della stringa helloworld
.
helloworld:$2y$10$tRM7x9gGKhcAmpeqKEdhj.qRWCr4qoV1FU9se0Crx2hkMVNL2ktEW
Sembra il modulo non ha modo di convalidare $2y
hash.
Ecco il mio test.
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)
Ecco i risultati:
// hashesGeneratedUsingAspirineDotOrg
// [ false, false ]
// hashesGeneratedUsingBcryptModule
// [ true, true, true, true, true ]
// hashesGeneratedUsingAspirineDotOrgSwippedYForA
// [ false, false ]
stumped su come posso confrontare $2y
hash a nodo.
C'è il another Stack Overflow question/answer che dice che puoi semplicemente cambiare il $2y
in $2a
ma che non funziona ancora per me.
!
Stavo usando the generator in modo errato perché è un generatore di password .htpasswd
che devi inserire il nome utente e la password in questo formato.
reggi helloworld
E l'uscita corrisponde qui:
reggi:$2y$10$iuC7GYH/h1Gl1aDmcpLFpeJXN9OZXZUYnaqD2NnGLQiVGQYBDtbtO
Prima come mettere solo
helloword
che sto assumendo hash di una stringa vuota.
Con queste modifiche, il y
a a
funziona in bcrypt
. E twin-bcrypt
funziona.
Ricordo vagamente avere più fortuna a lavorare nella direzione opposta - prendendo il '$ 2a $' hash generati da 'bcrypt' in javascript, sostituendo' 2a' con '2y ', e poi confrontando usando le librerie' 2y' in altre lingue (php nativamente e 'BCrypt' da .net poteva gestirli entrambi, cosa che mi sembrava molto strana). Posso scavare il codice di prova che avevo, se fosse utile per te. – dvlsg
@dvlsg Capito. Ciò ha senso. Quindi ho bisogno di confrontare gli hash di $ 2y nel nodo, non gli hash di $ 2a in php, che suppongo lavori sostituendo 'a' a' y'. – ThomasReggi
Sì, stavo effettivamente memorizzando gli hash come '$ 2y' nel database, usandoli come-è sia per PHP che per .NET, ma quando li ho usati nel nodo ho avuto un passaggio di conversione extra che è stato scambiato per 'a' prima del confronto. Sembrava sbagliato, ma sembrava che '2a' e' 2y' usassero la stessa struttura per il resto del sale/hash. – dvlsg