sha2_64bit_test.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // Copyright 2014 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.Sha2_64bit_test');
  15. goog.setTestOnly('goog.crypt.Sha2_64bit_test');
  16. goog.require('goog.array');
  17. goog.require('goog.crypt');
  18. goog.require('goog.crypt.Sha384');
  19. goog.require('goog.crypt.Sha512');
  20. goog.require('goog.crypt.Sha512_256');
  21. goog.require('goog.crypt.hashTester');
  22. goog.require('goog.testing.jsunit');
  23. goog.require('goog.userAgent');
  24. /**
  25. * Each object in the test vector array is a source text and one or more
  26. * hashes of that source text. The source text is either a string or a
  27. * byte array.
  28. * <p>
  29. * All hash values, except for the empty string, are from public sources:
  30. * csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
  31. * csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA384.pdf
  32. * csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA512_256.pdf
  33. * csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA2_Additional.pdf
  34. * en.wikipedia.org/wiki/SHA-2#Examples_of_SHA-2_variants
  35. */
  36. var TEST_VECTOR = [
  37. {
  38. // Make sure the algorithm correctly handles the empty string
  39. source: '',
  40. 512: 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce' +
  41. '47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'
  42. },
  43. {
  44. source: 'abc',
  45. 512: 'ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a' +
  46. '2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f',
  47. 384: 'cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163' +
  48. '1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7',
  49. 256: '53048e2681941ef99b2e29b76b4c7dabe4c2d0c634fc6d46e0e2f13107e7af23'
  50. },
  51. {
  52. source: 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn' +
  53. 'hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu',
  54. 512: '8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018' +
  55. '501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909',
  56. 384: '09330c33f71147e83d192fc782cd1b4753111b173b3b05d2' +
  57. '2fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039',
  58. 256: '3928e184fb8690f840da3988121d31be65cb9d3ef83ee6146feac861e19b563a'
  59. },
  60. {
  61. source: 'The quick brown fox jumps over the lazy dog',
  62. 512: '07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb64' +
  63. '2e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6',
  64. 384: 'ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c49' +
  65. '4011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1',
  66. 256: 'dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d'
  67. }
  68. ];
  69. /**
  70. * For each integer key N, the value is the SHA-512 value of a string
  71. * consisting of N repetitions of the character 'a'.
  72. */
  73. var TEST_FENCEPOST_VECTOR = {
  74. 110: 'c825949632e509824543f7eaf159fb6041722fce3c1cdcbb613b3d37ff107c51' +
  75. '9417baac32f8e74fe29d7f4823bf6886956603dca5354a6ed6e4a542e06b7d28',
  76. 111: 'fa9121c7b32b9e01733d034cfc78cbf67f926c7ed83e82200ef8681819692176' +
  77. '0b4beff48404df811b953828274461673c68d04e297b0eb7b2b4d60fc6b566a2',
  78. 112: 'c01d080efd492776a1c43bd23dd99d0a2e626d481e16782e75d54c2503b5dc32' +
  79. 'bd05f0f1ba33e568b88fd2d970929b719ecbb152f58f130a407c8830604b70ca',
  80. 113: '55ddd8ac210a6e18ba1ee055af84c966e0dbff091c43580ae1be703bdb85da31' +
  81. 'acf6948cf5bd90c55a20e5450f22fb89bd8d0085e39f85a86cc46abbca75e24d'
  82. };
  83. /**
  84. * Simple sanity tests for hash functions.
  85. */
  86. function testBasicOperations() {
  87. var sha512 = new goog.crypt.Sha512();
  88. goog.crypt.hashTester.runBasicTests(sha512);
  89. var sha384 = new goog.crypt.Sha384();
  90. goog.crypt.hashTester.runBasicTests(sha384);
  91. var sha256 = new goog.crypt.Sha512_256();
  92. goog.crypt.hashTester.runBasicTests(sha256);
  93. }
  94. /**
  95. * Function called by the actual testers to ensure that specific strings
  96. * hash to specific published values.
  97. *
  98. * Each item in the vector has a "source" and one or more additional keys.
  99. * If the item has a key matching the key argument passed to this
  100. * function, it is the expected value of the hash function.
  101. *
  102. * @param {!goog.crypt.Sha2_64bit} hasher The hasher to test
  103. * @param {number} length The length of the resulting hash, in bits.
  104. * Also the key to use in TEST_VECTOR for the expected hash value
  105. */
  106. function hashGoldenTester(hasher, length) {
  107. goog.array.forEach(TEST_VECTOR, function(data) {
  108. hasher.update(data.source);
  109. var digest = hasher.digest();
  110. assertEquals('Hash digest has the wrong length', length, digest.length * 8);
  111. if (data[length]) {
  112. // We're given an expected value
  113. var expected = goog.crypt.hexToByteArray(data[length]);
  114. assertElementsEquals(
  115. 'Wrong result for hash' + length + '(\'' + data.source + '\')',
  116. expected, digest);
  117. }
  118. hasher.reset();
  119. });
  120. }
  121. /** Test that Sha512() returns the published values */
  122. function testHashing512() {
  123. hashGoldenTester(new goog.crypt.Sha512(), 512);
  124. }
  125. /** Test that Sha384 returns the published values */
  126. function testHashing384() {
  127. hashGoldenTester(new goog.crypt.Sha384(), 384);
  128. }
  129. /** Test that Sha512_256 returns the published values */
  130. function testHashing256() {
  131. hashGoldenTester(new goog.crypt.Sha512_256(), 256);
  132. }
  133. /** Test that the opt_length works */
  134. function testHashing_optLength() {
  135. var hasher = new goog.crypt.Sha512();
  136. hasher.update('1234567890');
  137. var digest1 = hasher.digest();
  138. hasher.reset();
  139. hasher.update('12345678901234567890', 10);
  140. var digest2 = hasher.digest();
  141. assertElementsEquals(digest1, digest2);
  142. }
  143. /**
  144. * Make sure that we correctly handle strings whose length is 110-113.
  145. * This is the area where we are likely to hit fencepost errors in the padding
  146. * code.
  147. */
  148. function testFencepostErrors() {
  149. var hasher = new goog.crypt.Sha512();
  150. A64 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
  151. A128 = A64 + A64;
  152. for (var i = 110; i <= 113; i++) {
  153. hasher.update(A128, i);
  154. var digest = hasher.digest();
  155. var expected = goog.crypt.hexToByteArray(TEST_FENCEPOST_VECTOR[i]);
  156. assertElementsEquals('Fencepost ' + i, expected, digest);
  157. hasher.reset();
  158. }
  159. }
  160. /** Test one really large string using SHA512 */
  161. function testHashing512Large() {
  162. // This test tends to time out on IE7 and IE8. See b/22873770.
  163. if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher('9')) {
  164. var hasher = new goog.crypt.Sha512();
  165. hasher.update(goog.array.repeat(0, 1000000));
  166. var digest = hasher.digest();
  167. var expected = goog.crypt.hexToByteArray(
  168. 'ce044bc9fd43269d5bbc946cbebc3bb711341115cc4abdf2edbc3ff2c57ad4b1' +
  169. '5deb699bda257fea5aef9c6e55fcf4cf9dc25a8c3ce25f2efe90908379bff7ed');
  170. assertElementsEquals(expected, digest);
  171. }
  172. }
  173. /** Check that the code throws an error for bad input */
  174. function testBadInput_nullNotAllowed() {
  175. var hasher = new goog.crypt.Sha512();
  176. assertThrows('Null input not allowed', function() { hasher.update({}); });
  177. }
  178. function testBadInput_noFloatingPoint() {
  179. var hasher = new goog.crypt.Sha512();
  180. assertThrows('Floating point not allows', function() {
  181. hasher.update([1, 2, 3, 4, 4.5]);
  182. });
  183. }
  184. function testBadInput_negativeNotAllowed() {
  185. var hasher = new goog.crypt.Sha512();
  186. assertThrows('Negative not allowed', function() {
  187. hasher.update([1, 2, 3, 4, -10]);
  188. });
  189. }
  190. function testBadInput_mustBeByteArray() {
  191. var hasher = new goog.crypt.Sha512();
  192. assertThrows('Must be byte array', function() {
  193. hasher.update([1, 2, 3, 4, {}]);
  194. });
  195. }
  196. function testBadInput_byteTooLarge() {
  197. var hasher = new goog.crypt.Sha512();
  198. assertThrows('>255 not allowed', function() {
  199. hasher.update([1, 2, 3, 4, 256]);
  200. });
  201. }
  202. function testBadInput_characterTooLarge() {
  203. var hasher = new goog.crypt.Sha512();
  204. assertThrows('>255 not allowed', function() {
  205. hasher.update('abc' + String.fromCharCode(256));
  206. });
  207. }
  208. function testHasherNeedsReset_beforeDigest() {
  209. var hasher = new goog.crypt.Sha512();
  210. hasher.update('abc');
  211. hasher.digest();
  212. assertThrows('Need reset after digest', function() { hasher.digest(); });
  213. }
  214. function testHasherNeedsReset_beforeUpdate() {
  215. var hasher = new goog.crypt.Sha512();
  216. hasher.update('abc');
  217. hasher.digest();
  218. assertThrows('Need reset after digest', function() { hasher.update('abc'); });
  219. }
  220. function testHashingArrayLike() {
  221. // Create array-like object
  222. var obj = {};
  223. obj.length = 26;
  224. for (var i = 0; i < 26; i++) {
  225. obj[i] = 97 + i;
  226. }
  227. // Check hashing
  228. var hasher = new goog.crypt.Sha512();
  229. hasher.update(obj);
  230. var digest1 = hasher.digest();
  231. hasher.reset();
  232. hasher.update('abcdefghijklmnopqrstuvwxyz', 26);
  233. var digest2 = hasher.digest();
  234. assertElementsEquals(digest1, digest2);
  235. }