2015-01-15 12 views
5

próbuję hash zmienną w NodeJS tak:Korzystanie SHA-256 z NodeJS Crypto

var crypto = require('crypto'); 

var hash = crypto.createHash('sha256'); 

var code = 'bacon'; 

code = hash.update(code); 
code = hash.digest(code); 

console.log(code); 

Ale wygląda na to, że nie zrozumiałeś docs jak console.log nie rejestruje wersję hashed boczku, ale tylko niektóre informacje o SlowBuffer.

Jaki jest prawidłowy sposób to zrobić?

+0

Co ten skrót? – ravisoni

Odpowiedz

8

Spróbuj var hash = crypto.createHash('sha256').update(pwd).digest('base64');

+2

// aby dostać się w hex: crypto.createHash ('sha256'). Update ('password'). Digest ('hex'); –

0

nodejs (8) ref

const crypto = require('crypto'); 
const hash = crypto.createHash('sha256'); 

hash.on('readable',() => { 
    const data = hash.read(); 
    if (data) { 
     console.log(data.toString('hex')); 
     // Prints: 
     // 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50 
    } 
}); 

hash.write('some data to hash'); 
hash.end();