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