OLD | NEW |
---|---|
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'package:collection/collection.dart'; | |
5 import 'package:convert/convert.dart'; | 6 import 'package:convert/convert.dart'; |
6 | 7 |
7 /// A message digest as computed by a [Hash] or [HMAC] function. | 8 /// A message digest as computed by a [Hash] or [HMAC] function. |
8 class Digest { | 9 class Digest { |
9 /// The message digest as an array of bytes. | 10 /// The message digest as an array of bytes. |
10 final List<int> bytes; | 11 final List<int> bytes; |
11 | 12 |
12 Digest(this.bytes); | 13 Digest(this.bytes); |
13 | 14 |
14 /// Returns whether this is equal to another digest. | 15 /// Returns whether this is equal to another digest. |
15 /// | 16 /// |
16 /// This should be used instead of manual comparisons to avoid leaking | 17 /// This should be used instead of manual comparisons to avoid leaking |
17 /// information via timing. | 18 /// information via timing. |
18 bool operator ==(Object other) { | 19 bool operator ==(Object other) { |
19 if (other is! Digest) return false; | 20 if (other is! Digest) return false; |
20 | 21 |
21 var digest = other as Digest; | 22 var digest = other as Digest; |
kevmoo
2016/03/23 22:03:00
might could use ListEquality.equals here?
nweiz
2016/03/23 22:05:22
I'm guessing the bitwise stuff is more efficient,
kevmoo
2016/03/23 22:07:17
Sure – then another thought: would it be good to s
nweiz
2016/03/23 22:11:29
On second thought, I will just switch to ListEqual
| |
22 if (digest.bytes.length != bytes.length) return false; | 23 if (digest.bytes.length != bytes.length) return false; |
23 | 24 |
24 var result = 0; | 25 var result = 0; |
25 for (var i = 0; i < bytes.length; i++) { | 26 for (var i = 0; i < bytes.length; i++) { |
26 result |= bytes[i] ^ digest.bytes[i]; | 27 result |= bytes[i] ^ digest.bytes[i]; |
27 } | 28 } |
28 return result == 0; | 29 return result == 0; |
29 } | 30 } |
30 | 31 |
32 int get hashCode => const IterableEquality().hash(bytes); | |
33 | |
31 /// The message digest as a string of hexadecimal digits. | 34 /// The message digest as a string of hexadecimal digits. |
32 String toString() => hex.encode(bytes); | 35 String toString() => hex.encode(bytes); |
33 } | 36 } |
OLD | NEW |