Skip to content

Commit

Permalink
Add Digest.hashCode.
Browse files Browse the repository at this point in the history
Closes #30

R=kevmoo@google.com

Review URL: https://codereview.chromium.org//1830773003 .
  • Loading branch information
nex3 committed Mar 23, 2016
1 parent 3bb8955 commit 493d76d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
15 changes: 4 additions & 11 deletions lib/src/digest.dart
Expand Up @@ -2,6 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:collection/collection.dart';
import 'package:convert/convert.dart';

/// A message digest as computed by a [Hash] or [HMAC] function.
Expand All @@ -15,18 +16,10 @@ class Digest {
///
/// This should be used instead of manual comparisons to avoid leaking
/// information via timing.
bool operator ==(Object other) {
if (other is! Digest) return false;
bool operator ==(Object other) => other is Digest &&
const ListEquality().equals(bytes, other.bytes);

var digest = other as Digest;
if (digest.bytes.length != bytes.length) return false;

var result = 0;
for (var i = 0; i < bytes.length; i++) {
result |= bytes[i] ^ digest.bytes[i];
}
return result == 0;
}
int get hashCode => const ListEquality().hash(bytes);

/// The message digest as a string of hexadecimal digits.
String toString() => hex.encode(bytes);
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Expand Up @@ -6,6 +6,7 @@ homepage: https://www.github.com/dart-lang/crypto
environment:
sdk: '>=1.8.0 <2.0.0'
dependencies:
collection: '^1.0.0'
convert: '^1.0.0'
dev_dependencies:
test: '>=0.12.0 <0.13.0'

0 comments on commit 493d76d

Please sign in to comment.