Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Use a rich object to represent a message digest.
Browse files Browse the repository at this point in the history
Closes #2
  • Loading branch information
Ladicek authored and nex3 committed Sep 21, 2015
1 parent 193d3dc commit 200f2e6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/crypto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export 'src/base64.dart';
export 'src/base64/decoder.dart';
export 'src/base64/encoder.dart';
export 'src/crypto_utils.dart';
export 'src/digest.dart';
export 'src/hash.dart';
export 'src/hmac.dart';
export 'src/md5.dart';
Expand Down
36 changes: 36 additions & 0 deletions lib/src/digest.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// 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.

library crypto.digest;

import 'dart:typed_data';

import 'crypto_utils.dart';

/// A message digest as computed by a [Hash] or [HMAC] function.
class Digest {
/// The message digest as an array of bytes.
final List<int> bytes;

Digest(List<int> bytes)
: bytes = new Uint8List.fromList(bytes);

/// Returns whether this is equal to another 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;
if (other.bytes.length != bytes.length) return false;

var result = 0;
for (var i = 0; i < bytes.length; i++) {
result |= bytes[i] ^ other.bytes[i];
}
return result == 0;
}

/// The message digest as a string of hexadecimal digits.
String toString() => CryptoUtils.bytesToHex(bytes);
}

0 comments on commit 200f2e6

Please sign in to comment.