Skip to content

Commit dec4eb1

Browse files
Ladiceknex3
authored andcommitted
Use a rich object to represent a message digest.
Closes dart-lang/crypto#2
1 parent 28bedeb commit dec4eb1

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

pkgs/crypto/lib/crypto.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export 'src/base64.dart';
88
export 'src/base64/decoder.dart';
99
export 'src/base64/encoder.dart';
1010
export 'src/crypto_utils.dart';
11+
export 'src/digest.dart';
1112
export 'src/hash.dart';
1213
export 'src/hmac.dart';
1314
export 'src/md5.dart';

pkgs/crypto/lib/src/digest.dart

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

0 commit comments

Comments
 (0)