Cómo uso Node.js Crypto para crear un hash HMAC-SHA1?


Quiero crear un hash de I love cupcakes (firmado con la clave abcdeg)

Cómo puedo crear ese hash, usando Node.js Crypto?

Author: Xufox, 2011-09-20

3 answers

Documentación para cripto: 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')
 300
Author: Ricardo Tomasi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-22 03:40:50

Hace unos años se dijo que update() y digest() eran métodos heredados y se introdujo el nuevo enfoque de streaming API. Ahora los documentos dicen que cualquiera de los métodos puede ser utilizado. Por ejemplo:

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

Probado en el nodo v6.2.2 y v7.7.2

Véase https://nodejs.org/api/crypto.html#crypto_class_hmac . Da más ejemplos para usar el enfoque de streaming.

 84
Author: Adam Griffiths,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-25 07:58:36

La solución de Gwerder no funcionará porque hash = hmac.read(); sucede antes de que se finalice la secuencia. De ahí los problemas de AngraX. También la instrucción hmac.write no es necesaria en este ejemplo.

En su lugar haga esto:

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...
});

Más formalmente, si lo desea, la línea

hmac.end(text, function () {

Podría escribirse

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

Porque en este ejemplo el texto es una cadena utf

 20
Author: Dave,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-09 15:04:39