| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:convert'; |
| 6 import 'dart:typed_data'; |
| 7 |
| 8 import 'digest.dart'; |
| 9 import 'hash.dart'; |
| 10 import 'hash_sink.dart'; |
| 11 import 'utils.dart'; |
| 12 |
| 13 /// An instance of [MD5]. |
| 14 /// |
| 15 /// This instance provides convenient access to the [MD5][rfc] hash function. |
| 16 /// |
| 17 /// [rfc]: https://tools.ietf.org/html/rfc1321 |
| 18 /// |
| 19 /// **Warning**: MD5 has known collisions and should only be used when required |
| 20 /// for backwards compatibility. |
| 21 final md5 = new MD5._(); |
| 22 |
| 23 /// An implementation of the [MD5][rfc] hash function. |
| 24 /// |
| 25 /// [rfc]: https://tools.ietf.org/html/rfc1321 |
| 26 /// |
| 27 /// **Warning**: MD5 has known collisions and should only be used when required |
| 28 /// for backwards compatibility. |
| 29 /// |
| 30 /// Note that it's almost always easier to use [md5] rather than creating a new |
| 31 /// instance. |
| 32 class MD5 extends Hash { |
| 33 @override |
| 34 final int blockSize = 16 * bytesPerWord; |
| 35 |
| 36 MD5._(); |
| 37 |
| 38 @override |
| 39 ByteConversionSink startChunkedConversion(Sink<Digest> sink) => |
| 40 new ByteConversionSink.from(new _MD5Sink(sink)); |
| 41 } |
| 42 |
| 43 /// Data from a non-linear mathematical function that functions as |
| 44 /// reproducible noise. |
| 45 const _noise = const [ |
| 46 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, // |
| 47 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, |
| 48 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, |
| 49 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, |
| 50 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, |
| 51 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, |
| 52 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, |
| 53 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, |
| 54 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, |
| 55 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, |
| 56 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 |
| 57 ]; |
| 58 |
| 59 /// Per-round shift amounts. |
| 60 const _shiftAmounts = const [ |
| 61 07, 12, 17, 22, 07, 12, 17, 22, 07, 12, 17, 22, 07, 12, 17, 22, 05, 09, 14, // |
| 62 20, 05, 09, 14, 20, 05, 09, 14, 20, 05, 09, 14, 20, 04, 11, 16, 23, 04, 11, |
| 63 16, 23, 04, 11, 16, 23, 04, 11, 16, 23, 06, 10, 15, 21, 06, 10, 15, 21, 06, |
| 64 10, 15, 21, 06, 10, 15, 21 |
| 65 ]; |
| 66 |
| 67 /// The concrete implementation of [MD5]. |
| 68 /// |
| 69 /// This is separate so that it can extend [HashSink] without leaking additional |
| 70 /// public members. |
| 71 class _MD5Sink extends HashSink { |
| 72 @override |
| 73 final digest = new Uint32List(4); |
| 74 |
| 75 _MD5Sink(Sink<Digest> sink) |
| 76 : super(sink, 16, endian: Endianness.LITTLE_ENDIAN) { |
| 77 digest[0] = 0x67452301; |
| 78 digest[1] = 0xefcdab89; |
| 79 digest[2] = 0x98badcfe; |
| 80 digest[3] = 0x10325476; |
| 81 } |
| 82 |
| 83 @override |
| 84 void updateHash(Uint32List chunk) { |
| 85 assert(chunk.length == 16); |
| 86 |
| 87 var a = digest[0]; |
| 88 var b = digest[1]; |
| 89 var c = digest[2]; |
| 90 var d = digest[3]; |
| 91 |
| 92 var e; |
| 93 var f; |
| 94 |
| 95 for (var i = 0; i < 64; i++) { |
| 96 if (i < 16) { |
| 97 e = (b & c) | ((~b & mask32) & d); |
| 98 f = i; |
| 99 } else if (i < 32) { |
| 100 e = (d & b) | ((~d & mask32) & c); |
| 101 f = ((5 * i) + 1) % 16; |
| 102 } else if (i < 48) { |
| 103 e = b ^ c ^ d; |
| 104 f = ((3 * i) + 5) % 16; |
| 105 } else { |
| 106 e = c ^ (b | (~d & mask32)); |
| 107 f = (7 * i) % 16; |
| 108 } |
| 109 |
| 110 var temp = d; |
| 111 d = c; |
| 112 c = b; |
| 113 b = add32( |
| 114 b, |
| 115 rotl32(add32(add32(a, e), add32(_noise[i], chunk[f])), |
| 116 _shiftAmounts[i])); |
| 117 a = temp; |
| 118 } |
| 119 |
| 120 digest[0] = add32(a, digest[0]); |
| 121 digest[1] = add32(b, digest[1]); |
| 122 digest[2] = add32(c, digest[2]); |
| 123 digest[3] = add32(d, digest[3]); |
| 124 } |
| 125 } |
| OLD | NEW |