public
Last active

  • Download Gist
rfc4231.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
var crypto = require("crypto"),
assert = require("assert");
 
var hexstr2bytearray = function(str) {
var bytestrs = [];
for(var i=0;i<str.length;i+=2) {
bytestrs.push(str.substr(i,2));
}
return eval("[0x" + bytestrs.join(",0x") + "]");
}
 
 
var data1 = '4869205468657265';
var key1 = '0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b';
var res1 = 'b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7';
console.log("Test 1: key length - " + hexstr2bytearray(key1).length + " bytes, data text - '" + new Buffer(hexstr2bytearray(data1)).toString() + "'");
assert.equal(res1,(new crypto.Hmac).init("sha256",new Buffer(hexstr2bytearray(key1))).update(new Buffer(hexstr2bytearray(data1))).digest("hex"));
 
var data2 = '7768617420646f2079612077616e7420666f72206e6f7468696e673f';
var key2 = '4a656665';
var res2 = '5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843';
console.log("Test 2: key text - '" + new Buffer(hexstr2bytearray(key2)).toString() + "', data text - '" + new Buffer(hexstr2bytearray(data2)).toString() + "'");
assert.equal(res2,(new crypto.Hmac).init("sha256",new Buffer(hexstr2bytearray(key2))).update(new Buffer(hexstr2bytearray(data2))).digest("hex"));
 
var data3 = 'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd';
var key3 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
var res3 = '773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe';
console.log("Test 3: key length - " + hexstr2bytearray(key3).length + " bytes, data length - " + hexstr2bytearray(data3).length + " bytes");
assert.equal(res3,(new crypto.Hmac).init("sha256",new Buffer(hexstr2bytearray(key3))).update(new Buffer(hexstr2bytearray(data3))).digest("hex"));
 
var data4 = 'cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd';
var key4 = '0102030405060708090a0b0c0d0e0f10111213141516171819';
var res4 = '82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b';
console.log("Test 4: key length - " + hexstr2bytearray(key4).length + " bytes, data length - " + hexstr2bytearray(data4).length + " bytes");
assert.equal(res4,(new crypto.Hmac).init("sha256",new Buffer(hexstr2bytearray(key4))).update(new Buffer(hexstr2bytearray(data4))).digest("hex"));
 
var data5 = '546573742057697468205472756e636174696f6e';
var key5 = '0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c';
var res5 = 'a3b6167473100ee06e0c796c2955552b';
console.log("Test 5: key length - " + hexstr2bytearray(key5).length + " bytes, data text - '" + new Buffer(hexstr2bytearray(data5)).toString() + "'");
assert.equal(res5,(new crypto.Hmac).init("sha256",new Buffer(hexstr2bytearray(key5))).update(new Buffer(hexstr2bytearray(data5))).digest("hex"));
 
var data6 = '54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a65204b6579202d2048617368204b6579204669727374';
var key6 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
var res6 = '60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54';
console.log("Test 6: key length - " + hexstr2bytearray(key6).length + " bytes, data text - '" + new Buffer(hexstr2bytearray(data6)).toString() + "'");
assert.equal(res6,(new crypto.Hmac).init("sha256",new Buffer(hexstr2bytearray(key6))).update(new Buffer(hexstr2bytearray(data6))).digest("hex"));
 
var data7 = '5468697320697320612074657374207573696e672061206c6172676572207468616e20626c6f636b2d73697a65206b657920616e642061206c6172676572207468616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565647320746f20626520686173686564206265666f7265206265696e6720757365642062792074686520484d414320616c676f726974686d2e';
var key7 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
var res7 = '9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2';
console.log("Test 7: key length - " + hexstr2bytearray(key7).length + " bytes, data text - '" + new Buffer(hexstr2bytearray(data7)).toString() + "'");
assert.equal(res7,(new crypto.Hmac).init("sha256",new Buffer(hexstr2bytearray(key7))).update(new Buffer(hexstr2bytearray(data7))).digest("hex"));

Transcriptions of the 7 hmac test vectors for HMACSHA256 in RFC4231 http://tools.ietf.org/html/rfc4231

As of Node 0.2.2, test vectors 3,5,6 & 7 are failing.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.