2016-03-16 27 views
7

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.

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.

Aggiornamento

!

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.

+1

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

+1

@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

+0

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

risposta

1
  • Quando si utilizza bcrypt cambiamento del y a un a.
  • Quando si utilizza twin-bcrypt, l'hash funziona correttamente.

Quando si utilizza http://aspirine.org/htpasswd_en.html assicurarsi di fornire un nome utente e una password.

reggi helloworld 

Poi:

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

Ecco un esempio di lavoro sia con bcrypt e 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) 

Uscite:

true 
true 
+0

vuoi dire falso, vero? – ShrekOverflow