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:convert'; |
7 import 'dart:typed_data'; | 8 import 'dart:typed_data'; |
8 | 9 |
| 10 import 'digest.dart'; |
9 import 'hash.dart'; | 11 import 'hash.dart'; |
10 import 'hash_base.dart'; | 12 import 'hash_sink.dart'; |
11 import 'utils.dart'; | 13 import 'utils.dart'; |
12 | 14 |
| 15 /// An instance of [MD5]. |
| 16 /// |
| 17 /// This instance provides convenient access to the [MD5][rfc] hash function. |
| 18 /// |
| 19 /// [rfc]: https://tools.ietf.org/html/rfc1321 |
| 20 /// |
| 21 /// **Warning**: MD5 has known collisions and should only be used when required |
| 22 /// for backwards compatibility. |
| 23 final md5 = new MD5._(); |
| 24 |
13 /// An implementation of the [MD5][rfc] hash function. | 25 /// An implementation of the [MD5][rfc] hash function. |
14 /// | 26 /// |
15 /// [rfc]: https://tools.ietf.org/html/rfc1321 | 27 /// [rfc]: https://tools.ietf.org/html/rfc1321 |
16 /// | 28 /// |
17 /// **Warning**: MD5 has known collisions and should only be used when required | 29 /// **Warning**: MD5 has known collisions and should only be used when required |
18 /// for backwards compatibility. | 30 /// for backwards compatibility. |
19 abstract class MD5 implements Hash { | 31 /// |
20 factory MD5() = _MD5; | 32 /// Note that it's almost always easier to use [md5] rather than creating a new |
| 33 /// instance. |
| 34 class MD5 extends Hash { |
| 35 final int blockSize = 16 * bytesPerWord; |
21 | 36 |
22 MD5 newInstance(); | 37 @Deprecated("Use the md5 field instead.") |
| 38 MD5(); |
| 39 |
| 40 MD5 newInstance() => new MD5._(); |
| 41 |
| 42 ByteConversionSink startChunkedConversion(Sink<Digest> sink) => |
| 43 new ByteConversionSink.from(new _MD5Sink(sink)); |
23 } | 44 } |
24 | 45 |
25 /// Data from a non-linear mathematical function that functions as | 46 /// Data from a non-linear mathematical function that functions as |
26 /// reproducible noise. | 47 /// reproducible noise. |
27 const _noise = const [ | 48 const _noise = const [ |
28 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, | 49 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, |
29 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, | 50 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, |
30 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, | 51 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, |
31 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, | 52 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, |
32 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, | 53 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, |
(...skipping 10 matching lines...) Expand all Loading... |
43 07, 12, 17, 22, 07, 12, 17, 22, 07, 12, 17, 22, 07, 12, 17, 22, 05, 09, 14, | 64 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, | 65 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, | 66 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 | 67 10, 15, 21, 06, 10, 15, 21 |
47 ]; | 68 ]; |
48 | 69 |
49 /// The concrete implementation of [MD5]. | 70 /// The concrete implementation of [MD5]. |
50 /// | 71 /// |
51 /// This is separate so that it can extend [HashBase] without leaking additional | 72 /// This is separate so that it can extend [HashBase] without leaking additional |
52 /// public memebers. | 73 /// public memebers. |
53 class _MD5 extends HashBase implements MD5 { | 74 class _MD5Sink extends HashSink { |
54 final digest = new Uint32List(4); | 75 final digest = new Uint32List(4); |
55 | 76 |
56 _MD5() : super(16, endian: Endianness.LITTLE_ENDIAN) { | 77 _MD5Sink(Sink<Digest> sink) |
| 78 : super(sink, 16, endian: Endianness.LITTLE_ENDIAN) { |
57 digest[0] = 0x67452301; | 79 digest[0] = 0x67452301; |
58 digest[1] = 0xefcdab89; | 80 digest[1] = 0xefcdab89; |
59 digest[2] = 0x98badcfe; | 81 digest[2] = 0x98badcfe; |
60 digest[3] = 0x10325476; | 82 digest[3] = 0x10325476; |
61 } | 83 } |
62 | 84 |
63 MD5 newInstance() => new _MD5(); | |
64 | |
65 void updateHash(Uint32List chunk) { | 85 void updateHash(Uint32List chunk) { |
66 assert(chunk.length == 16); | 86 assert(chunk.length == 16); |
67 | 87 |
68 var a = digest[0]; | 88 var a = digest[0]; |
69 var b = digest[1]; | 89 var b = digest[1]; |
70 var c = digest[2]; | 90 var c = digest[2]; |
71 var d = digest[3]; | 91 var d = digest[3]; |
72 | 92 |
73 var e; | 93 var e; |
74 var f; | 94 var f; |
(...skipping 25 matching lines...) Expand all Loading... |
100 _shiftAmounts[i])); | 120 _shiftAmounts[i])); |
101 a = temp; | 121 a = temp; |
102 } | 122 } |
103 | 123 |
104 digest[0] = add32(a, digest[0]); | 124 digest[0] = add32(a, digest[0]); |
105 digest[1] = add32(b, digest[1]); | 125 digest[1] = add32(b, digest[1]); |
106 digest[2] = add32(c, digest[2]); | 126 digest[2] = add32(c, digest[2]); |
107 digest[3] = add32(d, digest[3]); | 127 digest[3] = add32(d, digest[3]); |
108 } | 128 } |
109 } | 129 } |
OLD | NEW |