如何使用Node。js加密创建HMAC-SHA1散列?

时间:2021-10-08 18:28:35

I want to create a hash of I love cupcakes (signed with the key abcdeg)

我想创建一个我喜欢的小蛋糕散列(用关键的abcdeg签名)

How can I create that hash, using Node.js Crypto?

如何使用Node创建哈希。js加密?

3 个解决方案

#1


294  

Documentation for crypto: http://nodejs.org/api/crypto.html

加密的文档:http://nodejs.org/api/crypto.html

var crypto = require('crypto')
  , text = 'I love cupcakes'
  , key = 'abcdeg'
  , hash

hash = crypto.createHmac('sha1', key).update(text).digest('hex')

#2


82  

A few years ago it was said that update() and digest() were legacy methods and the new streaming API approach was introduced. Now the docs say that either method can be used. For example:

几年前,有人说update()和digest()是遗留方法,并引入了新的流API方法。现在,医生说这两种方法都可以使用。例如:

var crypto    = require('crypto');
var text      = 'I love cupcakes';
var secret    = 'abcdeg'; //make this your secret!!
var algorithm = 'sha1';   //consider using sha256
var hash, hmac;

// Method 1 - Writing to a stream
hmac = crypto.createHmac(algorithm, secret);    
hmac.write(text); // write in to the stream
hmac.end();       // can't read from the stream until you call end()
hash = hmac.read().toString('hex');    // read out hmac digest
console.log("Method 1: ", hash);

// Method 2 - Using update and digest:
hmac = crypto.createHmac(algorithm, secret);
hmac.update(text);
hash = hmac.digest('hex');
console.log("Method 2: ", hash);

Tested on node v6.2.2 and v7.7.2

在节点v6.2.2和v7.7.2上测试。

See https://nodejs.org/api/crypto.html#crypto_class_hmac. Gives more examples for using the streaming approach.

见https://nodejs.org/api/crypto.html # crypto_class_hmac。提供更多使用流方法的示例。

#3


20  

Gwerder's solution wont work because hash = hmac.read(); happens before the stream is done being finalized. Thus AngraX's issues. Also the hmac.write statement is un-necessary in this example.

Gwerder的解决方案不起作用,因为hash = hmac.read();在完成流之前发生。因此AngraX问题。也了hmac。在本例中,写语句是不必要的。

Instead do this:

而是这样做:

var crypto    = require('crypto');
var hmac;
var algorithm = 'sha1';
var key       = 'abcdeg';
var text      = 'I love cupcakes';
var hash;

hmac = crypto.createHmac(algorithm, key);

// readout format:
hmac.setEncoding('hex');
//or also commonly: hmac.setEncoding('base64');

// callback is attached as listener to stream's finish event:
hmac.end(text, function () {
    hash = hmac.read();
    //...do something with the hash...
});

More formally, if you wish, the line

如果你愿意,可以更正式地说一句

hmac.end(text, function () {

could be written

可以写成

hmac.end(text, 'utf8', function () {

because in this example text is a utf string

因为在这个例子中,文本是一个utf字符串

#1


294  

Documentation for crypto: http://nodejs.org/api/crypto.html

加密的文档:http://nodejs.org/api/crypto.html

var crypto = require('crypto')
  , text = 'I love cupcakes'
  , key = 'abcdeg'
  , hash

hash = crypto.createHmac('sha1', key).update(text).digest('hex')

#2


82  

A few years ago it was said that update() and digest() were legacy methods and the new streaming API approach was introduced. Now the docs say that either method can be used. For example:

几年前,有人说update()和digest()是遗留方法,并引入了新的流API方法。现在,医生说这两种方法都可以使用。例如:

var crypto    = require('crypto');
var text      = 'I love cupcakes';
var secret    = 'abcdeg'; //make this your secret!!
var algorithm = 'sha1';   //consider using sha256
var hash, hmac;

// Method 1 - Writing to a stream
hmac = crypto.createHmac(algorithm, secret);    
hmac.write(text); // write in to the stream
hmac.end();       // can't read from the stream until you call end()
hash = hmac.read().toString('hex');    // read out hmac digest
console.log("Method 1: ", hash);

// Method 2 - Using update and digest:
hmac = crypto.createHmac(algorithm, secret);
hmac.update(text);
hash = hmac.digest('hex');
console.log("Method 2: ", hash);

Tested on node v6.2.2 and v7.7.2

在节点v6.2.2和v7.7.2上测试。

See https://nodejs.org/api/crypto.html#crypto_class_hmac. Gives more examples for using the streaming approach.

见https://nodejs.org/api/crypto.html # crypto_class_hmac。提供更多使用流方法的示例。

#3


20  

Gwerder's solution wont work because hash = hmac.read(); happens before the stream is done being finalized. Thus AngraX's issues. Also the hmac.write statement is un-necessary in this example.

Gwerder的解决方案不起作用,因为hash = hmac.read();在完成流之前发生。因此AngraX问题。也了hmac。在本例中,写语句是不必要的。

Instead do this:

而是这样做:

var crypto    = require('crypto');
var hmac;
var algorithm = 'sha1';
var key       = 'abcdeg';
var text      = 'I love cupcakes';
var hash;

hmac = crypto.createHmac(algorithm, key);

// readout format:
hmac.setEncoding('hex');
//or also commonly: hmac.setEncoding('base64');

// callback is attached as listener to stream's finish event:
hmac.end(text, function () {
    hash = hmac.read();
    //...do something with the hash...
});

More formally, if you wish, the line

如果你愿意,可以更正式地说一句

hmac.end(text, function () {

could be written

可以写成

hmac.end(text, 'utf8', function () {

because in this example text is a utf string

因为在这个例子中,文本是一个utf字符串