sha1_test.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2010 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.Sha1Test');
  15. goog.setTestOnly('goog.crypt.Sha1Test');
  16. goog.require('goog.crypt');
  17. goog.require('goog.crypt.Sha1');
  18. goog.require('goog.crypt.hashTester');
  19. goog.require('goog.testing.jsunit');
  20. goog.require('goog.userAgent');
  21. function testBasicOperations() {
  22. var sha1 = new goog.crypt.Sha1();
  23. goog.crypt.hashTester.runBasicTests(sha1);
  24. }
  25. function testBlockOperations() {
  26. var sha1 = new goog.crypt.Sha1();
  27. goog.crypt.hashTester.runBlockTests(sha1, 64);
  28. }
  29. function testHashing() {
  30. // Test vectors from:
  31. // csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
  32. // Empty stream.
  33. var sha1 = new goog.crypt.Sha1();
  34. assertEquals(
  35. 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
  36. goog.crypt.byteArrayToHex(sha1.digest()));
  37. // Test one-block message.
  38. sha1.reset();
  39. sha1.update([0x61, 0x62, 0x63]);
  40. assertEquals(
  41. 'a9993e364706816aba3e25717850c26c9cd0d89d',
  42. goog.crypt.byteArrayToHex(sha1.digest()));
  43. // Test multi-block message.
  44. sha1.reset();
  45. sha1.update('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq');
  46. assertEquals(
  47. '84983e441c3bd26ebaae4aa1f95129e5e54670f1',
  48. goog.crypt.byteArrayToHex(sha1.digest()));
  49. // The following test might cause timeouts on IE7 and IE8. See b/22873770.
  50. if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher('9')) {
  51. // Test long message.
  52. var thousandAs = [];
  53. for (var i = 0; i < 1000; ++i) {
  54. thousandAs[i] = 0x61;
  55. }
  56. sha1.reset();
  57. for (var i = 0; i < 1000; ++i) {
  58. sha1.update(thousandAs);
  59. }
  60. assertEquals(
  61. '34aa973cd4c4daa4f61eeb2bdbad27316534016f',
  62. goog.crypt.byteArrayToHex(sha1.digest()));
  63. }
  64. // Test standard message.
  65. sha1.reset();
  66. sha1.update('The quick brown fox jumps over the lazy dog');
  67. assertEquals(
  68. '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12',
  69. goog.crypt.byteArrayToHex(sha1.digest()));
  70. }