sha224_test.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.Sha224Test');
  15. goog.setTestOnly('goog.crypt.Sha224Test');
  16. goog.require('goog.crypt');
  17. goog.require('goog.crypt.Sha224');
  18. goog.require('goog.crypt.hashTester');
  19. goog.require('goog.testing.jsunit');
  20. function testBasicOperations() {
  21. var sha224 = new goog.crypt.Sha224();
  22. goog.crypt.hashTester.runBasicTests(sha224);
  23. }
  24. function testHashing() {
  25. // Some test vectors from:
  26. // csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
  27. var sha224 = new goog.crypt.Sha224();
  28. // NIST one block test vector.
  29. sha224.update(goog.crypt.stringToByteArray('abc'));
  30. assertEquals(
  31. '23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7',
  32. goog.crypt.byteArrayToHex(sha224.digest()));
  33. // NIST multi-block test vector.
  34. sha224.reset();
  35. sha224.update(
  36. goog.crypt.stringToByteArray(
  37. 'abcdbcdecdefdefgefghfghighij' +
  38. 'hijkijkljklmklmnlmnomnopnopq'));
  39. assertEquals(
  40. '75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525',
  41. goog.crypt.byteArrayToHex(sha224.digest()));
  42. // Message larger than one block (but less than two).
  43. sha224.reset();
  44. var biggerThanOneBlock = 'abcdbcdecdefdefgefghfghighij' +
  45. 'hijkijkljklmklmnlmnomnopnopq' +
  46. 'asdfljhr78yasdfljh45opa78sdf' +
  47. '120839414104897aavnasdfafasd';
  48. assertTrue(
  49. biggerThanOneBlock.length > goog.crypt.Sha2.BLOCKSIZE_ &&
  50. biggerThanOneBlock.length < 2 * goog.crypt.Sha2.BLOCKSIZE_);
  51. sha224.update(goog.crypt.stringToByteArray(biggerThanOneBlock));
  52. assertEquals(
  53. '27c9b678012becd6891bac653f355b2d26f63132e840644d565f5dac',
  54. goog.crypt.byteArrayToHex(sha224.digest()));
  55. // Message larger than two blocks.
  56. sha224.reset();
  57. var biggerThanTwoBlocks = 'abcdbcdecdefdefgefghfghighij' +
  58. 'hijkijkljklmklmnlmnomnopnopq' +
  59. 'asdfljhr78yasdfljh45opa78sdf' +
  60. '120839414104897aavnasdfafasd' +
  61. 'laasdouvhalacbnalalseryalcla';
  62. assertTrue(biggerThanTwoBlocks.length > 2 * goog.crypt.Sha2.BLOCKSIZE_);
  63. sha224.update(goog.crypt.stringToByteArray(biggerThanTwoBlocks));
  64. assertEquals(
  65. '1c2c1455cc984eef6f25ec9d79b1c661b3794887c3d0b24111ed9803',
  66. goog.crypt.byteArrayToHex(sha224.digest()));
  67. }