| 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 MD5 newInstance() => new MD5(); |
| 38 |
| 39 ByteConversionSink startChunkedConversion(Sink<Digest> sink) => |
| 40 new ByteConversionSink.from(new _MD5Sink(sink)); |
| 23 } | 41 } |
| 24 | 42 |
| 25 /// Data from a non-linear mathematical function that functions as | 43 /// Data from a non-linear mathematical function that functions as |
| 26 /// reproducible noise. | 44 /// reproducible noise. |
| 27 const _noise = const [ | 45 const _noise = const [ |
| 28 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, | 46 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, |
| 29 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, | 47 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, |
| 30 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, | 48 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, |
| 31 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, | 49 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, |
| 32 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, | 50 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, | 61 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, | 62 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, | 63 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 | 64 10, 15, 21, 06, 10, 15, 21 |
| 47 ]; | 65 ]; |
| 48 | 66 |
| 49 /// The concrete implementation of [MD5]. | 67 /// The concrete implementation of [MD5]. |
| 50 /// | 68 /// |
| 51 /// This is separate so that it can extend [HashBase] without leaking additional | 69 /// This is separate so that it can extend [HashBase] without leaking additional |
| 52 /// public memebers. | 70 /// public memebers. |
| 53 class _MD5 extends HashBase implements MD5 { | 71 class _MD5Sink extends HashSink { |
| 54 final digest = new Uint32List(4); | 72 final digest = new Uint32List(4); |
| 55 | 73 |
| 56 _MD5() : super(16, endian: Endianness.LITTLE_ENDIAN) { | 74 _MD5Sink(Sink<Digest> sink) |
| 75 : super(sink, 16, endian: Endianness.LITTLE_ENDIAN) { |
| 57 digest[0] = 0x67452301; | 76 digest[0] = 0x67452301; |
| 58 digest[1] = 0xefcdab89; | 77 digest[1] = 0xefcdab89; |
| 59 digest[2] = 0x98badcfe; | 78 digest[2] = 0x98badcfe; |
| 60 digest[3] = 0x10325476; | 79 digest[3] = 0x10325476; |
| 61 } | 80 } |
| 62 | 81 |
| 63 MD5 newInstance() => new _MD5(); | |
| 64 | |
| 65 void updateHash(Uint32List chunk) { | 82 void updateHash(Uint32List chunk) { |
| 66 assert(chunk.length == 16); | 83 assert(chunk.length == 16); |
| 67 | 84 |
| 68 var a = digest[0]; | 85 var a = digest[0]; |
| 69 var b = digest[1]; | 86 var b = digest[1]; |
| 70 var c = digest[2]; | 87 var c = digest[2]; |
| 71 var d = digest[3]; | 88 var d = digest[3]; |
| 72 | 89 |
| 73 var e; | 90 var e; |
| 74 var f; | 91 var f; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 100 _shiftAmounts[i])); | 117 _shiftAmounts[i])); |
| 101 a = temp; | 118 a = temp; |
| 102 } | 119 } |
| 103 | 120 |
| 104 digest[0] = add32(a, digest[0]); | 121 digest[0] = add32(a, digest[0]); |
| 105 digest[1] = add32(b, digest[1]); | 122 digest[1] = add32(b, digest[1]); |
| 106 digest[2] = add32(c, digest[2]); | 123 digest[2] = add32(c, digest[2]); |
| 107 digest[3] = add32(d, digest[3]); | 124 digest[3] = add32(d, digest[3]); |
| 108 } | 125 } |
| 109 } | 126 } |
| OLD | NEW |