| 1 |
// Copyright 2011 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.crypt.HmacTest'); |
| 16 |
goog.setTestOnly('goog.crypt.HmacTest'); |
| 17 |
|
| 18 |
goog.require('goog.crypt.Hmac'); |
| 19 |
goog.require('goog.crypt.Sha1'); |
| 20 |
goog.require('goog.crypt.hashTester'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
|
| 23 |
function stringToBytes(s) { |
| 24 |
var bytes = new Array(s.length); |
| 25 |
|
| 26 |
for (var i = 0; i < s.length; ++i) { |
| 27 |
bytes[i] = s.charCodeAt(i) & 255; |
| 28 |
} |
| 29 |
return bytes; |
| 30 |
} |
| 31 |
|
| 32 |
function hexToBytes(str) { |
| 33 |
var arr = []; |
| 34 |
|
| 35 |
for (var i = 0; i < str.length; i += 2) { |
| 36 |
arr.push(parseInt(str.substring(i, i + 2), 16)); |
| 37 |
} |
| 38 |
|
| 39 |
return arr; |
| 40 |
} |
| 41 |
|
| 42 |
function bytesToHex(b) { |
| 43 |
var hexchars = '0123456789abcdef'; |
| 44 |
var hexrep = new Array(b.length * 2); |
| 45 |
|
| 46 |
for (var i = 0; i < b.length; ++i) { |
| 47 |
hexrep[i * 2] = hexchars.charAt((b[i] >> 4) & 15); |
| 48 |
hexrep[i * 2 + 1] = hexchars.charAt(b[i] & 15); |
| 49 |
} |
| 50 |
return hexrep.join(''); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* helper to get an hmac of the given message with the given key. |
| 56 |
*/ |
| 57 |
function getHmac(key, message, opt_blockSize) { |
| 58 |
var hasher = new goog.crypt.Sha1(); |
| 59 |
var hmacer = new goog.crypt.Hmac(hasher, key, opt_blockSize); |
| 60 |
return bytesToHex(hmacer.getHmac(message)); |
| 61 |
} |
| 62 |
|
| 63 |
function testBasicOperations() { |
| 64 |
var hmac = new goog.crypt.Hmac(new goog.crypt.Sha1(), 'key', 64); |
| 65 |
goog.crypt.hashTester.runBasicTests(hmac); |
| 66 |
} |
| 67 |
|
| 68 |
function testBasicOperationsWithNoBlockSize() { |
| 69 |
var hmac = new goog.crypt.Hmac(new goog.crypt.Sha1(), 'key'); |
| 70 |
goog.crypt.hashTester.runBasicTests(hmac); |
| 71 |
} |
| 72 |
|
| 73 |
function testHmac() { |
| 74 |
// HMAC test vectors from: |
| 75 |
// http://tools.ietf.org/html/2202 |
| 76 |
|
| 77 |
assertEquals( |
| 78 |
'test 1 failed', 'b617318655057264e28bc0b6fb378c8ef146be00', |
| 79 |
getHmac( |
| 80 |
hexToBytes('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'), |
| 81 |
stringToBytes('Hi There'))); |
| 82 |
|
| 83 |
assertEquals( |
| 84 |
'test 2 failed', 'effcdf6ae5eb2fa2d27416d5f184df9c259a7c79', |
| 85 |
getHmac( |
| 86 |
stringToBytes('Jefe'), |
| 87 |
stringToBytes('what do ya want for nothing?'))); |
| 88 |
|
| 89 |
assertEquals( |
| 90 |
'test 3 failed', '125d7342b9ac11cd91a39af48aa17b4f63f175d3', |
| 91 |
getHmac( |
| 92 |
hexToBytes('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'), |
| 93 |
hexToBytes( |
| 94 |
'dddddddddddddddddddddddddddddddddddddddd' + |
| 95 |
'dddddddddddddddddddddddddddddddddddddddd' + |
| 96 |
'dddddddddddddddddddd'))); |
| 97 |
|
| 98 |
assertEquals( |
| 99 |
'test 4 failed', '4c9007f4026250c6bc8414f9bf50c86c2d7235da', |
| 100 |
getHmac( |
| 101 |
hexToBytes('0102030405060708090a0b0c0d0e0f10111213141516171819'), |
| 102 |
hexToBytes( |
| 103 |
'cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd' + |
| 104 |
'cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd' + |
| 105 |
'cdcdcdcdcdcdcdcdcdcd'))); |
| 106 |
|
| 107 |
assertEquals( |
| 108 |
'test 5 failed', '4c1a03424b55e07fe7f27be1d58bb9324a9a5a04', |
| 109 |
getHmac( |
| 110 |
hexToBytes('0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c'), |
| 111 |
stringToBytes('Test With Truncation'))); |
| 112 |
|
| 113 |
assertEquals( |
| 114 |
'test 6 failed', 'aa4ae5e15272d00e95705637ce8a3b55ed402112', |
| 115 |
getHmac( |
| 116 |
hexToBytes( |
| 117 |
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + |
| 118 |
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + |
| 119 |
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + |
| 120 |
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'), |
| 121 |
stringToBytes( |
| 122 |
'Test Using Larger Than Block-Size Key - Hash Key First'))); |
| 123 |
|
| 124 |
assertEquals( |
| 125 |
'test 7 failed', 'b617318655057264e28bc0b6fb378c8ef146be00', |
| 126 |
getHmac( |
| 127 |
hexToBytes('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'), |
| 128 |
stringToBytes('Hi There'), 64)); |
| 129 |
|
| 130 |
assertEquals( |
| 131 |
'test 8 failed', '941f806707826395dc510add6a45ce9933db976e', |
| 132 |
getHmac( |
| 133 |
hexToBytes('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'), |
| 134 |
stringToBytes('Hi There'), 32)); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
/** Regression test for Bug 12863104 */ |
| 139 |
function testUpdateWithLongKey() { |
| 140 |
// Calling update() then digest() should give the same result as just |
| 141 |
// calling getHmac() |
| 142 |
var key = hexToBytes( |
| 143 |
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + |
| 144 |
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + |
| 145 |
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + |
| 146 |
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'); |
| 147 |
var message = 'Secret Message'; |
| 148 |
var hmac = new goog.crypt.Hmac(new goog.crypt.Sha1(), key); |
| 149 |
hmac.update(message); |
| 150 |
var result1 = bytesToHex(hmac.digest()); |
| 151 |
hmac.reset(); |
| 152 |
var result2 = bytesToHex(hmac.getHmac(message)); |
| 153 |
assertEquals('Results must be the same', result1, result2); |
| 154 |
} |