将SHA-256与NodeJS加密一起使用

时间:2020-11-24 18:26:47

I'm trying to hash a variable in NodeJS like so:

我试图在NodeJS中散列变量,如下所示:

var crypto = require('crypto');

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

var code = 'bacon';

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

console.log(code);

But looks like I have misunderstood the docs as the console.log doesn't log a hashed version of bacon but just some information about SlowBuffer.

但看起来我误解了文档,因为console.log不会记录一个哈希版本的培根,而只是一些关于SlowBuffer的信息。

What's the correct way to do this?

这样做的正确方法是什么?

2 个解决方案

#1


11  

Try var hash = crypto.createHash('sha256').update(pwd).digest('base64');

尝试var hash = crypto.createHash('sha256')。update(pwd).digest('base64');

#2


0  

nodejs (8) ref

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();

#1


11  

Try var hash = crypto.createHash('sha256').update(pwd).digest('base64');

尝试var hash = crypto.createHash('sha256')。update(pwd).digest('base64');

#2


0  

nodejs (8) ref

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();