hmac_test.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. goog.provide('goog.crypt.HmacTest');
  15. goog.setTestOnly('goog.crypt.HmacTest');
  16. goog.require('goog.crypt.Hmac');
  17. goog.require('goog.crypt.Sha1');
  18. goog.require('goog.crypt.hashTester');
  19. goog.require('goog.testing.jsunit');
  20. function stringToBytes(s) {
  21. var bytes = new Array(s.length);
  22. for (var i = 0; i < s.length; ++i) {
  23. bytes[i] = s.charCodeAt(i) & 255;
  24. }
  25. return bytes;
  26. }
  27. function hexToBytes(str) {
  28. var arr = [];
  29. for (var i = 0; i < str.length; i += 2) {
  30. arr.push(parseInt(str.substring(i, i + 2), 16));
  31. }
  32. return arr;
  33. }
  34. function bytesToHex(b) {
  35. var hexchars = '0123456789abcdef';
  36. var hexrep = new Array(b.length * 2);
  37. for (var i = 0; i < b.length; ++i) {
  38. hexrep[i * 2] = hexchars.charAt((b[i] >> 4) & 15);
  39. hexrep[i * 2 + 1] = hexchars.charAt(b[i] & 15);
  40. }
  41. return hexrep.join('');
  42. }
  43. /**
  44. * helper to get an hmac of the given message with the given key.
  45. */
  46. function getHmac(key, message, opt_blockSize) {
  47. var hasher = new goog.crypt.Sha1();
  48. var hmacer = new goog.crypt.Hmac(hasher, key, opt_blockSize);
  49. return bytesToHex(hmacer.getHmac(message));
  50. }
  51. function testBasicOperations() {
  52. var hmac = new goog.crypt.Hmac(new goog.crypt.Sha1(), 'key', 64);
  53. goog.crypt.hashTester.runBasicTests(hmac);
  54. }
  55. function testBasicOperationsWithNoBlockSize() {
  56. var hmac = new goog.crypt.Hmac(new goog.crypt.Sha1(), 'key');
  57. goog.crypt.hashTester.runBasicTests(hmac);
  58. }
  59. function testHmac() {
  60. // HMAC test vectors from:
  61. // http://tools.ietf.org/html/2202
  62. assertEquals(
  63. 'test 1 failed', 'b617318655057264e28bc0b6fb378c8ef146be00',
  64. getHmac(
  65. hexToBytes('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'),
  66. stringToBytes('Hi There')));
  67. assertEquals(
  68. 'test 2 failed', 'effcdf6ae5eb2fa2d27416d5f184df9c259a7c79',
  69. getHmac(
  70. stringToBytes('Jefe'),
  71. stringToBytes('what do ya want for nothing?')));
  72. assertEquals(
  73. 'test 3 failed', '125d7342b9ac11cd91a39af48aa17b4f63f175d3',
  74. getHmac(
  75. hexToBytes('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'),
  76. hexToBytes(
  77. 'dddddddddddddddddddddddddddddddddddddddd' +
  78. 'dddddddddddddddddddddddddddddddddddddddd' +
  79. 'dddddddddddddddddddd')));
  80. assertEquals(
  81. 'test 4 failed', '4c9007f4026250c6bc8414f9bf50c86c2d7235da',
  82. getHmac(
  83. hexToBytes('0102030405060708090a0b0c0d0e0f10111213141516171819'),
  84. hexToBytes(
  85. 'cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd' +
  86. 'cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd' +
  87. 'cdcdcdcdcdcdcdcdcdcd')));
  88. assertEquals(
  89. 'test 5 failed', '4c1a03424b55e07fe7f27be1d58bb9324a9a5a04',
  90. getHmac(
  91. hexToBytes('0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c'),
  92. stringToBytes('Test With Truncation')));
  93. assertEquals(
  94. 'test 6 failed', 'aa4ae5e15272d00e95705637ce8a3b55ed402112',
  95. getHmac(
  96. hexToBytes(
  97. 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
  98. 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
  99. 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
  100. 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'),
  101. stringToBytes(
  102. 'Test Using Larger Than Block-Size Key - Hash Key First')));
  103. assertEquals(
  104. 'test 7 failed', 'b617318655057264e28bc0b6fb378c8ef146be00',
  105. getHmac(
  106. hexToBytes('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'),
  107. stringToBytes('Hi There'), 64));
  108. assertEquals(
  109. 'test 8 failed', '941f806707826395dc510add6a45ce9933db976e',
  110. getHmac(
  111. hexToBytes('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'),
  112. stringToBytes('Hi There'), 32));
  113. }
  114. /** Regression test for Bug 12863104 */
  115. function testUpdateWithLongKey() {
  116. // Calling update() then digest() should give the same result as just
  117. // calling getHmac()
  118. var key = hexToBytes(
  119. 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
  120. 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
  121. 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
  122. 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
  123. var message = 'Secret Message';
  124. var hmac = new goog.crypt.Hmac(new goog.crypt.Sha1(), key);
  125. hmac.update(message);
  126. var result1 = bytesToHex(hmac.digest());
  127. hmac.reset();
  128. var result2 = bytesToHex(hmac.getHmac(message));
  129. assertEquals('Results must be the same', result1, result2);
  130. }