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 library crypto.digest; | |
6 | |
7 import 'crypto_utils.dart'; | 5 import 'crypto_utils.dart'; |
8 | 6 |
9 /// A message digest as computed by a [Hash] or [HMAC] function. | 7 /// A message digest as computed by a [Hash] or [HMAC] function. |
10 class Digest { | 8 class Digest { |
11 /// The message digest as an array of bytes. | 9 /// The message digest as an array of bytes. |
12 final List<int> bytes; | 10 final List<int> bytes; |
13 | 11 |
14 Digest(this.bytes); | 12 Digest(this.bytes); |
15 | 13 |
16 /// Returns whether this is equal to another digest. | 14 /// Returns whether this is equal to another digest. |
17 /// | 15 /// |
18 /// This should be used instead of manual comparisons to avoid leaking | 16 /// This should be used instead of manual comparisons to avoid leaking |
19 /// information via timing. | 17 /// information via timing. |
20 bool operator ==(Object other) { | 18 bool operator ==(Object other) { |
21 if (other is! Digest) return false; | 19 if (other is! Digest) return false; |
22 | 20 |
23 var digest = other as Digest; | 21 var digest = other as Digest; |
24 if (digest.bytes.length != bytes.length) return false; | 22 if (digest.bytes.length != bytes.length) return false; |
25 | 23 |
26 var result = 0; | 24 var result = 0; |
27 for (var i = 0; i < bytes.length; i++) { | 25 for (var i = 0; i < bytes.length; i++) { |
28 result |= bytes[i] ^ digest.bytes[i]; | 26 result |= bytes[i] ^ digest.bytes[i]; |
29 } | 27 } |
30 return result == 0; | 28 return result == 0; |
31 } | 29 } |
32 | 30 |
33 /// The message digest as a string of hexadecimal digits. | 31 /// The message digest as a string of hexadecimal digits. |
34 String toString() => CryptoUtils.bytesToHex(bytes); | 32 String toString() => CryptoUtils.bytesToHex(bytes); |
35 } | 33 } |
OLD | NEW |