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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
(function() {
function Tests(ASSERT, HMAC, UTIL) {
describe('hmac', function() {
it('should md5 hash "Hi There", 16-byte key', function() {
var key = UTIL.hexToBytes('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b');
var hmac = HMAC.create();
hmac.start('MD5', key);
hmac.update('Hi There');
ASSERT.equal(hmac.digest().toHex(), '9294727a3638bb1c13f48ef8158bfc9d');
});
it('should md5 hash "what do ya want for nothing?", "Jefe" key', function() {
var hmac = HMAC.create();
hmac.start('MD5', 'Jefe');
hmac.update('what do ya want for nothing?');
ASSERT.equal(hmac.digest().toHex(), '750c783e6ab0b503eaa86e310a5db738');
});
it('should md5 hash "Test Using Larger Than Block-Size Key - Hash Key First", 80-byte key', function() {
var key = UTIL.hexToBytes(
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
var hmac = HMAC.create();
hmac.start('MD5', key);
hmac.update('Test Using Larger Than Block-Size Key - Hash Key First');
ASSERT.equal(hmac.digest().toHex(), '6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd');
});
it('should sha1 hash "Hi There", 20-byte key', function() {
var key = UTIL.hexToBytes('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b');
var hmac = HMAC.create();
hmac.start('SHA1', key);
hmac.update('Hi There');
ASSERT.equal(
hmac.digest().toHex(), 'b617318655057264e28bc0b6fb378c8ef146be00');
});
it('should sha1 hash "what do ya want for nothing?", "Jefe" key', function() {
var hmac = HMAC.create();
hmac.start('SHA1', 'Jefe');
hmac.update('what do ya want for nothing?');
ASSERT.equal(
hmac.digest().toHex(), 'effcdf6ae5eb2fa2d27416d5f184df9c259a7c79');
});
it('should sha1 hash "Test Using Larger Than Block-Size Key - Hash Key First", 80-byte key', function() {
var key = UTIL.hexToBytes(
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
var hmac = HMAC.create();
hmac.start('SHA1', key);
hmac.update('Test Using Larger Than Block-Size Key - Hash Key First');
ASSERT.equal(
hmac.digest().toHex(), 'aa4ae5e15272d00e95705637ce8a3b55ed402112');
});
});
}
// check for AMD
if(typeof define === 'function') {
define([
'forge/hmac',
'forge/util'
], function(HMAC, UTIL) {
Tests(
// Global provided by test harness
ASSERT,
HMAC(),
UTIL()
);
});
} else if(typeof module === 'object' && module.exports) {
// assume NodeJS
Tests(
require('assert'),
require('../../js/hmac')(),
require('../../js/util')());
}
})();
|