OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library crypto.md5; | 5 library crypto.md5; |
6 | 6 |
7 import 'dart:typed_data'; | 7 import 'dart:typed_data'; |
8 | 8 |
9 import 'hash.dart'; | 9 import 'hash.dart'; |
10 import 'hash_base.dart'; | 10 import 'hash_base.dart'; |
11 import 'utils.dart'; | 11 import 'utils.dart'; |
12 | 12 |
13 /// An implementation of the [MD5][rfc] hash function. | 13 /// An implementation of the [MD5][rfc] hash function. |
14 /// | 14 /// |
15 /// [rfc]: https://tools.ietf.org/html/rfc1321 | 15 /// [rfc]: https://tools.ietf.org/html/rfc1321 |
16 /// | 16 /// |
17 /// **Warning**: MD5 has known collisions and should only be used when required | 17 /// **Warning**: MD5 has known collisions and should only be used when required |
18 /// for backwards compatibility. | 18 /// for backwards compatibility. |
19 abstract class MD5 implements Hash { | 19 abstract class MD5 implements Hash { |
20 factory MD5() = _MD5; | 20 factory MD5() = _MD5; |
21 | 21 |
22 MD5 newInstance(); | 22 MD5 newInstance(); |
23 } | 23 } |
24 | 24 |
| 25 /// Data from a non-linear mathematical function that functions as |
| 26 /// reproducible noise. |
| 27 const _noise = const [ |
| 28 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, |
| 29 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, |
| 30 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, |
| 31 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, |
| 32 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, |
| 33 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, |
| 34 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, |
| 35 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, |
| 36 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, |
| 37 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, |
| 38 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 |
| 39 ]; |
| 40 |
| 41 /// Per-round shift amounts. |
| 42 const _shiftAmounts = const [ |
| 43 07, 12, 17, 22, 07, 12, 17, 22, 07, 12, 17, 22, 07, 12, 17, 22, 05, 09, 14, |
| 44 20, 05, 09, 14, 20, 05, 09, 14, 20, 05, 09, 14, 20, 04, 11, 16, 23, 04, 11, |
| 45 16, 23, 04, 11, 16, 23, 04, 11, 16, 23, 06, 10, 15, 21, 06, 10, 15, 21, 06, |
| 46 10, 15, 21, 06, 10, 15, 21 |
| 47 ]; |
| 48 |
25 /// The concrete implementation of [MD5]. | 49 /// The concrete implementation of [MD5]. |
26 /// | 50 /// |
27 /// This is separate so that it can extend [HashBase] without leaking additional | 51 /// This is separate so that it can extend [HashBase] without leaking additional |
28 /// public memebers. | 52 /// public memebers. |
29 class _MD5 extends HashBase implements MD5 { | 53 class _MD5 extends HashBase implements MD5 { |
30 _MD5() : super(16, 4, false) { | 54 final digest = new Uint32List(4); |
31 h[0] = 0x67452301; | 55 |
32 h[1] = 0xefcdab89; | 56 _MD5() : super(16, endian: Endianness.LITTLE_ENDIAN) { |
33 h[2] = 0x98badcfe; | 57 digest[0] = 0x67452301; |
34 h[3] = 0x10325476; | 58 digest[1] = 0xefcdab89; |
| 59 digest[2] = 0x98badcfe; |
| 60 digest[3] = 0x10325476; |
35 } | 61 } |
36 | 62 |
37 MD5 newInstance() { | 63 MD5 newInstance() => new _MD5(); |
38 return new _MD5(); | |
39 } | |
40 | 64 |
41 /// Data from a non-linear mathematical function that functions as | 65 void updateHash(Uint32List chunk) { |
42 /// reproducible noise. | 66 assert(chunk.length == 16); |
43 static const _k = const [ | |
44 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, | |
45 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, | |
46 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, | |
47 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, | |
48 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, | |
49 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, | |
50 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, | |
51 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, | |
52 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, | |
53 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, | |
54 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 | |
55 ]; | |
56 | 67 |
57 /// Per-round shift amounts. | 68 var a = digest[0]; |
58 static const _r = const [ | 69 var b = digest[1]; |
59 07, 12, 17, 22, 07, 12, 17, 22, 07, 12, 17, 22, 07, 12, 17, 22, 05, 09, 14, | 70 var c = digest[2]; |
60 20, 05, 09, 14, 20, 05, 09, 14, 20, 05, 09, 14, 20, 04, 11, 16, 23, 04, 11, | 71 var d = digest[3]; |
61 16, 23, 04, 11, 16, 23, 04, 11, 16, 23, 06, 10, 15, 21, 06, 10, 15, 21, 06, | |
62 10, 15, 21, 06, 10, 15, 21 | |
63 ]; | |
64 | 72 |
65 void updateHash(Uint32List m) { | 73 var e; |
66 assert(m.length == 16); | 74 var f; |
67 | |
68 var a = h[0]; | |
69 var b = h[1]; | |
70 var c = h[2]; | |
71 var d = h[3]; | |
72 | |
73 var t0; | |
74 var t1; | |
75 | 75 |
76 for (var i = 0; i < 64; i++) { | 76 for (var i = 0; i < 64; i++) { |
77 if (i < 16) { | 77 if (i < 16) { |
78 t0 = (b & c) | ((~b & MASK_32) & d); | 78 e = (b & c) | ((~b & mask32) & d); |
79 t1 = i; | 79 f = i; |
80 } else if (i < 32) { | 80 } else if (i < 32) { |
81 t0 = (d & b) | ((~d & MASK_32) & c); | 81 e = (d & b) | ((~d & mask32) & c); |
82 t1 = ((5 * i) + 1) % 16; | 82 f = ((5 * i) + 1) % 16; |
83 } else if (i < 48) { | 83 } else if (i < 48) { |
84 t0 = b ^ c ^ d; | 84 e = b ^ c ^ d; |
85 t1 = ((3 * i) + 5) % 16; | 85 f = ((3 * i) + 5) % 16; |
86 } else { | 86 } else { |
87 t0 = c ^ (b | (~d & MASK_32)); | 87 e = c ^ (b | (~d & mask32)); |
88 t1 = (7 * i) % 16; | 88 f = (7 * i) % 16; |
89 } | 89 } |
90 | 90 |
91 var temp = d; | 91 var temp = d; |
92 d = c; | 92 d = c; |
93 c = b; | 93 c = b; |
94 b = add32( | 94 b = add32( |
95 b, rotl32(add32(add32(a, t0), add32(_k[i], m[t1])), _r[i])); | 95 b, |
| 96 rotl32( |
| 97 add32( |
| 98 add32(a, e), |
| 99 add32(_noise[i], chunk[f])), |
| 100 _shiftAmounts[i])); |
96 a = temp; | 101 a = temp; |
97 } | 102 } |
98 | 103 |
99 h[0] = add32(a, h[0]); | 104 digest[0] = add32(a, digest[0]); |
100 h[1] = add32(b, h[1]); | 105 digest[1] = add32(b, digest[1]); |
101 h[2] = add32(c, h[2]); | 106 digest[2] = add32(c, digest[2]); |
102 h[3] = add32(d, h[3]); | 107 digest[3] = add32(d, digest[3]); |
103 } | 108 } |
104 } | 109 } |
OLD | NEW |