md5_test.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.Md5Test');
  15. goog.setTestOnly('goog.crypt.Md5Test');
  16. goog.require('goog.crypt');
  17. goog.require('goog.crypt.Md5');
  18. goog.require('goog.crypt.hashTester');
  19. goog.require('goog.testing.jsunit');
  20. var sixty = '123456789012345678901234567890123456789012345678901234567890';
  21. function testBasicOperations() {
  22. var md5 = new goog.crypt.Md5();
  23. goog.crypt.hashTester.runBasicTests(md5);
  24. }
  25. function testBlockOperations() {
  26. var md5 = new goog.crypt.Md5();
  27. goog.crypt.hashTester.runBlockTests(md5, 64);
  28. }
  29. function testHashing() {
  30. // Empty stream.
  31. var md5 = new goog.crypt.Md5();
  32. assertEquals(
  33. 'd41d8cd98f00b204e9800998ecf8427e',
  34. goog.crypt.byteArrayToHex(md5.digest()));
  35. // Simple stream.
  36. md5.reset();
  37. md5.update([97]);
  38. assertEquals(
  39. '0cc175b9c0f1b6a831c399e269772661',
  40. goog.crypt.byteArrayToHex(md5.digest()));
  41. // Simple stream with two updates.
  42. md5.reset();
  43. md5.update([97]);
  44. md5.update('bc');
  45. assertEquals(
  46. '900150983cd24fb0d6963f7d28e17f72',
  47. goog.crypt.byteArrayToHex(md5.digest()));
  48. // RFC 1321 standard test.
  49. md5.reset();
  50. md5.update('abcdefghijklmnopqrstuvwxyz');
  51. assertEquals(
  52. 'c3fcd3d76192e4007dfb496cca67e13b',
  53. goog.crypt.byteArrayToHex(md5.digest()));
  54. // RFC 1321 standard test with two updates.
  55. md5.reset();
  56. md5.update('message ');
  57. md5.update('digest');
  58. assertEquals(
  59. 'f96b697d7cb7938d525a2f31aaf161d0',
  60. goog.crypt.byteArrayToHex(md5.digest()));
  61. // RFC 1321 standard test with three updates.
  62. md5.reset();
  63. md5.update('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
  64. md5.update('abcdefghijklmnopqrstuvwxyz');
  65. md5.update('0123456789');
  66. assertEquals(
  67. 'd174ab98d277d9f5a5611c2c9f419d9f',
  68. goog.crypt.byteArrayToHex(md5.digest()));
  69. }
  70. function testPadding() {
  71. // Message + padding fits in two 64-byte blocks.
  72. var md5 = new goog.crypt.Md5();
  73. md5.update(sixty);
  74. md5.update(sixty.substr(0, 59));
  75. assertEquals(
  76. '6261005311809757906e04c0d670492d',
  77. goog.crypt.byteArrayToHex(md5.digest()));
  78. // Message + padding does not fit in two 64-byte blocks.
  79. md5.reset();
  80. md5.update(sixty);
  81. md5.update(sixty);
  82. assertEquals(
  83. '1d453b96d48d5e0cec4a20a71fecaa81',
  84. goog.crypt.byteArrayToHex(md5.digest()));
  85. }
  86. function testTwoAccumulators() {
  87. // Two accumulators in parallel.
  88. var md5_A = new goog.crypt.Md5();
  89. var md5_B = new goog.crypt.Md5();
  90. md5_A.update(sixty);
  91. md5_B.update(sixty);
  92. md5_A.update(sixty + '1');
  93. md5_B.update(sixty + '2');
  94. assertEquals(
  95. '0801d688cc107d4789ec8b9a4519f01f',
  96. goog.crypt.byteArrayToHex(md5_A.digest()));
  97. assertEquals(
  98. '6e1a35ffc185d1e684d6ed281c0d4bd2',
  99. goog.crypt.byteArrayToHex(md5_B.digest()));
  100. }
  101. function testCollision() {
  102. // Check a known collision.
  103. var A = [
  104. 0xd1, 0x31, 0xdd, 0x02, 0xc5, 0xe6, 0xee, 0xc4, 0x69, 0x3d, 0x9a, 0x06,
  105. 0x98, 0xaf, 0xf9, 0x5c, 0x2f, 0xca, 0xb5, 0x87, 0x12, 0x46, 0x7e, 0xab,
  106. 0x40, 0x04, 0x58, 0x3e, 0xb8, 0xfb, 0x7f, 0x89, 0x55, 0xad, 0x34, 0x06,
  107. 0x09, 0xf4, 0xb3, 0x02, 0x83, 0xe4, 0x88, 0x83, 0x25, 0x71, 0x41, 0x5a,
  108. 0x08, 0x51, 0x25, 0xe8, 0xf7, 0xcd, 0xc9, 0x9f, 0xd9, 0x1d, 0xbd, 0xf2,
  109. 0x80, 0x37, 0x3c, 0x5b, 0xd8, 0x82, 0x3e, 0x31, 0x56, 0x34, 0x8f, 0x5b,
  110. 0xae, 0x6d, 0xac, 0xd4, 0x36, 0xc9, 0x19, 0xc6, 0xdd, 0x53, 0xe2, 0xb4,
  111. 0x87, 0xda, 0x03, 0xfd, 0x02, 0x39, 0x63, 0x06, 0xd2, 0x48, 0xcd, 0xa0,
  112. 0xe9, 0x9f, 0x33, 0x42, 0x0f, 0x57, 0x7e, 0xe8, 0xce, 0x54, 0xb6, 0x70,
  113. 0x80, 0xa8, 0x0d, 0x1e, 0xc6, 0x98, 0x21, 0xbc, 0xb6, 0xa8, 0x83, 0x93,
  114. 0x96, 0xf9, 0x65, 0x2b, 0x6f, 0xf7, 0x2a, 0x70
  115. ];
  116. var B = [
  117. 0xd1, 0x31, 0xdd, 0x02, 0xc5, 0xe6, 0xee, 0xc4, 0x69, 0x3d, 0x9a, 0x06,
  118. 0x98, 0xaf, 0xf9, 0x5c, 0x2f, 0xca, 0xb5, 0x07, 0x12, 0x46, 0x7e, 0xab,
  119. 0x40, 0x04, 0x58, 0x3e, 0xb8, 0xfb, 0x7f, 0x89, 0x55, 0xad, 0x34, 0x06,
  120. 0x09, 0xf4, 0xb3, 0x02, 0x83, 0xe4, 0x88, 0x83, 0x25, 0xf1, 0x41, 0x5a,
  121. 0x08, 0x51, 0x25, 0xe8, 0xf7, 0xcd, 0xc9, 0x9f, 0xd9, 0x1d, 0xbd, 0x72,
  122. 0x80, 0x37, 0x3c, 0x5b, 0xd8, 0x82, 0x3e, 0x31, 0x56, 0x34, 0x8f, 0x5b,
  123. 0xae, 0x6d, 0xac, 0xd4, 0x36, 0xc9, 0x19, 0xc6, 0xdd, 0x53, 0xe2, 0x34,
  124. 0x87, 0xda, 0x03, 0xfd, 0x02, 0x39, 0x63, 0x06, 0xd2, 0x48, 0xcd, 0xa0,
  125. 0xe9, 0x9f, 0x33, 0x42, 0x0f, 0x57, 0x7e, 0xe8, 0xce, 0x54, 0xb6, 0x70,
  126. 0x80, 0x28, 0x0d, 0x1e, 0xc6, 0x98, 0x21, 0xbc, 0xb6, 0xa8, 0x83, 0x93,
  127. 0x96, 0xf9, 0x65, 0xab, 0x6f, 0xf7, 0x2a, 0x70
  128. ];
  129. var digest = '79054025255fb1a26e4bc422aef54eb4';
  130. var md5_A = new goog.crypt.Md5();
  131. var md5_B = new goog.crypt.Md5();
  132. md5_A.update(A);
  133. md5_B.update(B);
  134. assertEquals(digest, goog.crypt.byteArrayToHex(md5_A.digest()));
  135. assertEquals(digest, goog.crypt.byteArrayToHex(md5_B.digest()));
  136. }