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 /** | 13 /// An implementation of the [MD5][rfc] hash function. |
14 * MD5 hash function implementation. | 14 /// |
15 * | 15 /// [rfc]: https://tools.ietf.org/html/rfc1321 |
16 * WARNING: MD5 has known collisions and should only be used when | 16 /// |
17 * required for backwards compatibility. | 17 /// **Warning**: MD5 has known collisions and should only be used when required |
18 */ | 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 /// The concrete implementation of [MD5]. |
| 26 /// |
| 27 /// This is separate so that it can extend [HashBase] without leaking additional |
| 28 /// public memebers. |
25 class _MD5 extends HashBase implements MD5 { | 29 class _MD5 extends HashBase implements MD5 { |
26 _MD5() : super(16, 4, false) { | 30 _MD5() : super(16, 4, false) { |
27 h[0] = 0x67452301; | 31 h[0] = 0x67452301; |
28 h[1] = 0xefcdab89; | 32 h[1] = 0xefcdab89; |
29 h[2] = 0x98badcfe; | 33 h[2] = 0x98badcfe; |
30 h[3] = 0x10325476; | 34 h[3] = 0x10325476; |
31 } | 35 } |
32 | 36 |
33 // Returns a new instance of this Hash. | |
34 MD5 newInstance() { | 37 MD5 newInstance() { |
35 return new _MD5(); | 38 return new _MD5(); |
36 } | 39 } |
37 | 40 |
| 41 /// Data from a non-linear mathematical function that functions as |
| 42 /// reproducible noise. |
38 static const _k = const [ | 43 static const _k = const [ |
39 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, | 44 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, |
40 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, | 45 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, |
41 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, | 46 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, |
42 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, | 47 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, |
43 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, | 48 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, |
44 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, | 49 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, |
45 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, | 50 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, |
46 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, | 51 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, |
47 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, | 52 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, |
48 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, | 53 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, |
49 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 | 54 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 |
50 ]; | 55 ]; |
51 | 56 |
| 57 /// Per-round shift amounts. |
52 static const _r = const [ | 58 static const _r = const [ |
53 07, 12, 17, 22, 07, 12, 17, 22, 07, 12, 17, 22, 07, 12, 17, 22, 05, 09, 14, | 59 07, 12, 17, 22, 07, 12, 17, 22, 07, 12, 17, 22, 07, 12, 17, 22, 05, 09, 14, |
54 20, 05, 09, 14, 20, 05, 09, 14, 20, 05, 09, 14, 20, 04, 11, 16, 23, 04, 11, | 60 20, 05, 09, 14, 20, 05, 09, 14, 20, 05, 09, 14, 20, 04, 11, 16, 23, 04, 11, |
55 16, 23, 04, 11, 16, 23, 04, 11, 16, 23, 06, 10, 15, 21, 06, 10, 15, 21, 06, | 61 16, 23, 04, 11, 16, 23, 04, 11, 16, 23, 06, 10, 15, 21, 06, 10, 15, 21, 06, |
56 10, 15, 21, 06, 10, 15, 21 | 62 10, 15, 21, 06, 10, 15, 21 |
57 ]; | 63 ]; |
58 | 64 |
59 // Compute one iteration of the MD5 algorithm with a chunk of | |
60 // 16 32-bit pieces. | |
61 void updateHash(Uint32List m) { | 65 void updateHash(Uint32List m) { |
62 assert(m.length == 16); | 66 assert(m.length == 16); |
63 | 67 |
64 var a = h[0]; | 68 var a = h[0]; |
65 var b = h[1]; | 69 var b = h[1]; |
66 var c = h[2]; | 70 var c = h[2]; |
67 var d = h[3]; | 71 var d = h[3]; |
68 | 72 |
69 var t0; | 73 var t0; |
70 var t1; | 74 var t1; |
(...skipping 20 matching lines...) Expand all Loading... |
91 b, rotl32(add32(add32(a, t0), add32(_k[i], m[t1])), _r[i])); | 95 b, rotl32(add32(add32(a, t0), add32(_k[i], m[t1])), _r[i])); |
92 a = temp; | 96 a = temp; |
93 } | 97 } |
94 | 98 |
95 h[0] = add32(a, h[0]); | 99 h[0] = add32(a, h[0]); |
96 h[1] = add32(b, h[1]); | 100 h[1] = add32(b, h[1]); |
97 h[2] = add32(c, h[2]); | 101 h[2] = add32(c, h[2]); |
98 h[3] = add32(d, h[3]); | 102 h[3] = add32(d, h[3]); |
99 } | 103 } |
100 } | 104 } |
OLD | NEW |