sha256_test.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2012 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.Sha256Test');
  15. goog.setTestOnly('goog.crypt.Sha256Test');
  16. goog.require('goog.crypt');
  17. goog.require('goog.crypt.Sha256');
  18. goog.require('goog.crypt.hashTester');
  19. goog.require('goog.testing.jsunit');
  20. function testBasicOperations() {
  21. var sha256 = new goog.crypt.Sha256();
  22. goog.crypt.hashTester.runBasicTests(sha256);
  23. }
  24. function testHashing() {
  25. // Some test vectors from:
  26. // csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
  27. var sha256 = new goog.crypt.Sha256();
  28. // Empty message.
  29. sha256.update([]);
  30. assertEquals(
  31. 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
  32. goog.crypt.byteArrayToHex(sha256.digest()));
  33. // NIST one block test vector.
  34. sha256.reset();
  35. sha256.update(goog.crypt.stringToByteArray('abc'));
  36. assertEquals(
  37. 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad',
  38. goog.crypt.byteArrayToHex(sha256.digest()));
  39. // NIST multi-block test vector.
  40. sha256.reset();
  41. sha256.update(
  42. goog.crypt.stringToByteArray(
  43. 'abcdbcdecdefdefgefghfghighij' +
  44. 'hijkijkljklmklmnlmnomnopnopq'));
  45. assertEquals(
  46. '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1',
  47. goog.crypt.byteArrayToHex(sha256.digest()));
  48. // Message larger than one block (but less than two).
  49. sha256.reset();
  50. var biggerThanOneBlock = 'abcdbcdecdefdefgefghfghighij' +
  51. 'hijkijkljklmklmnlmnomnopnopq' +
  52. 'asdfljhr78yasdfljh45opa78sdf' +
  53. '120839414104897aavnasdfafasd';
  54. assertTrue(
  55. biggerThanOneBlock.length > goog.crypt.Sha2.BLOCKSIZE_ &&
  56. biggerThanOneBlock.length < 2 * goog.crypt.Sha2.BLOCKSIZE_);
  57. sha256.update(goog.crypt.stringToByteArray(biggerThanOneBlock));
  58. assertEquals(
  59. '390a5035433e46b740600f3117d11ece3c64706dc889106666ac04fe4f458abc',
  60. goog.crypt.byteArrayToHex(sha256.digest()));
  61. // Message larger than two blocks.
  62. sha256.reset();
  63. var biggerThanTwoBlocks = 'abcdbcdecdefdefgefghfghighij' +
  64. 'hijkijkljklmklmnlmnomnopnopq' +
  65. 'asdfljhr78yasdfljh45opa78sdf' +
  66. '120839414104897aavnasdfafasd' +
  67. 'laasdouvhalacbnalalseryalcla';
  68. assertTrue(biggerThanTwoBlocks.length > 2 * goog.crypt.Sha2.BLOCKSIZE_);
  69. sha256.update(goog.crypt.stringToByteArray(biggerThanTwoBlocks));
  70. assertEquals(
  71. 'd655c513fd347e9be372d891f8bb42895ca310fabf6ead6681ebc66a04e84db5',
  72. goog.crypt.byteArrayToHex(sha256.digest()));
  73. }
  74. /** Check that the code checks for bad input */
  75. function testBadInput() {
  76. assertThrows('Bad input', function() { new goog.crypt.Sha256().update({}); });
  77. assertThrows('Floating point not allows', function() {
  78. new goog.crypt.Sha256().update([1, 2, 3, 4, 4.5]);
  79. });
  80. assertThrows('Negative not allowed', function() {
  81. new goog.crypt.Sha256().update([1, 2, 3, 4, -10]);
  82. });
  83. assertThrows('Must be byte array', function() {
  84. new goog.crypt.Sha256().update([1, 2, 3, 4, {}]);
  85. });
  86. }