UNPKG

3.44 kBJavaScriptView Raw
1/**
2 * Test HMAC functionality.
3 */
4var assert = require('assert');
5var jsHashes = require('../');
6
7var MD5 = new jsHashes.MD5;
8MD5.setUTF8(false);
9
10// test vectors from http://www.ietf.org/rfc/rfc2104.txt
11describe('Test MD5 HMAC (rfc2104)', function() {
12 it("should pass test vector #1", function() {
13 var key = '\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b';
14 assert.equal(MD5.hex_hmac(key, 'Hi There'),
15 "9294727a3638bb1c13f48ef8158bfc9d");
16 });
17 it("should pass test vector #2", function() {
18 assert.equal(MD5.hex_hmac("Jefe", "what do ya want for nothing?"),
19 "750c783e6ab0b503eaa86e310a5db738");
20 });
21 it("should pass test vector #3", function() {
22 var key = '', data = '', i;
23 for (i=0; i<16; i++) {
24 key += '\xAA';
25 }
26 for (i=0; i<50; i++) {
27 data += '\xDD';
28 }
29 assert.equal(MD5.hex_hmac(key, data),
30 '56be34521d144c88dbb8c733f0e8b3f6');
31 });
32});
33
34// test vectors from
35// http://en.wikipedia.org/wiki/Hash-based_message_authentication_code
36// SHA512/RMD160 from https://quickhash.com/
37var testVectors = [
38 {
39 key: "",
40 data: "",
41 hmac: {
42 MD5: "74e6f7298a9c2d168935f58c001bad88",
43 SHA1: "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d",
44 SHA256: "b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad",
45 SHA512: "b936cee86c9f87aa5d3c6f2e84cb5a4239a5fe50480a6ec66b70ab5b1f4ac6730c6c515421b327ec1d69402e53dfb49ad7381eb067b338fd7b0cb22247225d47",
46 RMD160: "44d86b658a3e7cbc1a2010848b53e35c917720ca"
47 }
48 },
49 {
50 key: "key",
51 data: "The quick brown fox jumps over the lazy dog",
52 hmac: {
53 MD5: "80070713463e7749b90c2dc24911e275",
54 SHA1: "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9",
55 SHA256: "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8",
56 SHA512: "b42af09057bac1e2d41708e48a902e09b5ff7f12ab428a4fe86653c73dd248fb82f948a549f7b791a5b41915ee4d1ec3935357e4e2317250d0372afa2ebeeb3a",
57 RMD160: "50278a77d4d7670561ab72e867383aef6ce50b3e"
58 }
59 },
60 // Test key > block size
61 // These vectors are from https://quickhash.com/
62 {
63 key: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
64 data: "The quick brown fox jumps over the lazy dog",
65 hmac: {
66 MD5: "8e86bf7840bbe52ba3f45030dba9d39a",
67 SHA1: "ee114807434bea4ab839b940286f0c3f5b4f8a11",
68 SHA256: "359706cae34991529dbf545ed055bed283da8b7339807db6affa2ae517d8b389",
69 SHA512: "3135e1514cd8f6b471feb6980eedd1858047dd0c1fd44b135fade32d053b9a649f6c448fb81a6f0dc77f28f7d2505cd475aea018f90ff6961bd775acf3b8daad",
70 RMD160: "5031d8b3399e949d4a48c9fcf10ae537b7294cbb"
71 }
72 }
73];
74describe('Test HMAC (wikipedia test vectors)', function() {
75 testVectors.forEach(function(tv) {
76 var key = tv.key, data = tv.data;
77 describe('key='+JSON.stringify(key)+" data="+JSON.stringify(data), function() {
78 Object.keys(tv.hmac).forEach(function(h) {
79 it('should have the correct HMAC_'+h, function() {
80 var HF = new jsHashes[h]();
81 assert.equal(HF.hex_hmac(key, data), tv.hmac[h]);
82 });
83 });
84 });
85 });
86});