Browse Source

crypto: Streaming api for Hmac

v0.9.4-release
isaacs 12 years ago
parent
commit
175f78c6ba
  1. 6
      lib/crypto.js
  2. 5
      test/simple/test-crypto.js

6
lib/crypto.js

@ -189,16 +189,20 @@ Hash.prototype.digest = function(outputEncoding) {
exports.createHmac = exports.Hmac = Hmac; exports.createHmac = exports.Hmac = Hmac;
function Hmac(hmac, key) { function Hmac(hmac, key, options) {
if (!(this instanceof Hmac)) if (!(this instanceof Hmac))
return new Hmac(hmac, key); return new Hmac(hmac, key);
this._binding = new binding.Hmac(); this._binding = new binding.Hmac();
this._binding.init(hmac, toBuf(key)); this._binding.init(hmac, toBuf(key));
stream.Transform.call(this, options);
} }
util.inherits(Hmac, stream.Transform);
Hmac.prototype.update = Hash.prototype.update; Hmac.prototype.update = Hash.prototype.update;
Hmac.prototype.digest = Hash.prototype.digest; Hmac.prototype.digest = Hash.prototype.digest;
Hmac.prototype._flush = Hash.prototype._flush;
Hmac.prototype._transform = Hash.prototype._transform;
function getDecoder(decoder, encoding) { function getDecoder(decoder, encoding) {

5
test/simple/test-crypto.js

@ -230,15 +230,20 @@ var rfc4231 = [
for (var i = 0, l = rfc4231.length; i < l; i++) { for (var i = 0, l = rfc4231.length; i < l; i++) {
for (var hash in rfc4231[i]['hmac']) { for (var hash in rfc4231[i]['hmac']) {
var str = crypto.createHmac(hash, rfc4231[i].key);
str.end(rfc4231[i].data);
var strRes = str.read().toString('hex');
var result = crypto.createHmac(hash, rfc4231[i]['key']) var result = crypto.createHmac(hash, rfc4231[i]['key'])
.update(rfc4231[i]['data']) .update(rfc4231[i]['data'])
.digest('hex'); .digest('hex');
if (rfc4231[i]['truncate']) { if (rfc4231[i]['truncate']) {
result = result.substr(0, 32); // first 128 bits == 32 hex chars result = result.substr(0, 32); // first 128 bits == 32 hex chars
strRes = strRes.substr(0, 32);
} }
assert.equal(rfc4231[i]['hmac'][hash], assert.equal(rfc4231[i]['hmac'][hash],
result, result,
'Test HMAC-' + hash + ': Test case ' + (i + 1) + ' rfc 4231'); 'Test HMAC-' + hash + ': Test case ' + (i + 1) + ' rfc 4231');
assert.equal(strRes, result, 'Should get same result from stream');
} }
} }

Loading…
Cancel
Save