Debian Codegraph @

closure-library/closure/goog/crypt/md5_test.js

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