2014-04-11 10 views
10

Il mio codice php genera un hash utilizzando password_hash che memorizzo in un database. Di seguito è riportato il codice PHP:Verificare l'hash della password in nodejs che è stato generato in php

$hash = password_hash($value, PASSWORD_BCRYPT, array('cost' => $cost)); 

Vorrei verificare/verificare la password contro questo hash in nodejs.

Ho visto molti moduli di nodi (bcrypt, phpass, node-bcrypt), ma tutti mi danno false. Di seguito è riportato un hash di esempio generato in php e che sto cercando di verificare in nodejs.

var hash = '$2y$08$9TTThrthZhTOcoHELRjuN.3mJd2iKYIeNlV/CYJUWWRnDfRRw6fD2'; 

var bcrypt = require('bcrypt'); 

bcrypt.compare("secret", hash, function(err, res) { 
    console.log(res); 
}); 

(Qui segreto è vera password)

mia soluzione attuale è quella di chiamare uno script php tramite nodo per verificare (per chi ha bisogno di una soluzione alternativa)

var exec = require('child_process').exec; 
var cmd = 'php verify.php password encryped_pasword'; 
exec(cmd, function (error, stdout, stderr) { 
    // output is in stdout 
    console.log(stdout); 
//If stdout has 1 it satisfies else false 
}); 

Si tratta di un hack e non una buona risposta a questo problema. C'è un modo per verificare la password in nodejs senza utilizzare una soluzione alternativa come questa?

+0

Avete dato un'occhiata a https://www.npmjs.org/package/bcrypt-nodejs – o0rebelious0o

+0

@ o0rebelious0o I ho provato a fare un paragone usandolo, non dà nulla nemmeno falso e nessun errore – Sudesh

+0

Attenzione, $ costo deve corrispondere a getRounds(), questo è h ow bcrypt funziona. – erenon

risposta

18

Sostituire $ 2y $ nella password con hash con $ 2a $, quindi bcrypt.compare dovrebbe fornire risultati corretti.

var hash = '$2y$08$9TTThrthZhTOcoHELRjuN.3mJd2iKYIeNlV/CYJUWWRnDfRRw6fD2'; 
var bcrypt = require('bcrypt'); 
hash = hash.replace(/^\$2y(.+)$/i, '$2a$1'); 
bcrypt.compare("secret", hash, function(err, res) { 
    console.log(res); 
}); 

su ES6:

import bcrypt from 'bcrypt'; 
let hash = '$2y$08$9TTThrthZhTOcoHELRjuN.3mJd2iKYIeNlV/CYJUWWRnDfRRw6fD2'; 
hash = hash.replace(/^\$2y(.+)$/i, '$2a$1'); 
bcrypt.compare('secret', hash, function(err, res) { 
    console.log(res); 
}); 
+2

Questo mi ha aiutato molto, grazie! Anche se sarebbe interessato a sapere perché è necessario se qualcuno lo sa? – iamjonesy

+0

ha funzionato! qualcuno può spiegare perché è così? –

+1

Commentando per ottenere aggiornamenti su questo (se ce ne sono stati da più di un anno!) – Sushruth

9

So che questo è stato risposto, ma sembra dai commenti che è necessario un po 'più in dettaglio.

hash bcrypt prodotte dalla funzione() password_hash php sono suddivise come segue:

$2y$08$9TTThrthZhTOcoHELRjuN.3mJd2iKYIeNlV/CYJUWWRnDfRRw6fD2

|  |  |      | 
|  |  Salt     Hashed Password 
|  | 
|  Algorithm options (cost, in this case) 
| 
Algorithm type 

Sembra da altre risposte qui in modo che, mentre le versioni di PHP e il nodo di Bcrypt utilizza algoritmi diversi, l'unica differenza nell'output hash è il prefisso. Quindi, tutto ciò che è richiesto è, come indicato da @Sudesh, per scambiare il $2y$ per un $2a$ e lo stesso zio di Bob.

Fonti

http://php.net/manual/en/faq.passwords.php

$2y bcrypt hashes in Node.js

Comparing BCrypt hash between PHP and NodeJS

Problemi correlati