Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1117)

Unified Diff: lib/src/hmac.dart

Issue 1355223002: Change Hash subclasses to be Converters. (Closed) Base URL: git@github.com:dart-lang/crypto.git@master
Patch Set: Code review changes Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/src/hash_sink.dart ('k') | lib/src/md5.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/hmac.dart
diff --git a/lib/src/hmac.dart b/lib/src/hmac.dart
index 169e1fedfc69234357e6890c78e50fd80dc19b27..d9eae8caedb623d5d3004209de64128bb973353e 100644
--- a/lib/src/hmac.dart
+++ b/lib/src/hmac.dart
@@ -8,6 +8,7 @@ import 'dart:typed_data';
import 'package:typed_data/typed_data.dart';
+import 'digest_sink.dart';
import 'hash.dart';
/// An implementation of [keyed-hash method authentication codes][rfc].
@@ -29,7 +30,7 @@ class HMAC {
final _message = new Uint8Buffer();
/// The hash function used to compute the authentication digest.
- Hash _hash;
+ final Hash _hash;
/// The secret key shared by the sender and the receiver.
final Uint8List _key;
@@ -45,11 +46,7 @@ class HMAC {
: _hash = hash,
_key = new Uint8List(hash.blockSize) {
// Hash the key if it's longer than the block size of the hash.
- if (key.length > _hash.blockSize) {
- _hash = _hash.newInstance();
- _hash.add(key);
- key = _hash.close();
- }
+ if (key.length > _hash.blockSize) key = _hash.convert(key).bytes;
// If [key] is shorter than the block size, the rest of [_key] will be
// 0-padded.
@@ -73,10 +70,7 @@ class HMAC {
}
// Inner hash computation.
- _hash = _hash.newInstance();
- _hash.add(padding);
- _hash.add(_message);
- var innerDigest = _hash.close();
+ var innerDigest = _hashWithPadding(padding, _message);
// Compute outer padding.
for (var i = 0; i < padding.length; i++) {
@@ -84,10 +78,17 @@ class HMAC {
}
// Outer hash computation which is the result.
- _hash = _hash.newInstance();
- _hash.add(padding);
- _hash.add(innerDigest);
- return _hash.close();
+ return _hashWithPadding(padding, innerDigest);
+ }
+
+ /// Returns the digest of [padding] followed by [data].
+ List<int> _hashWithPadding(List<int> padding, List<int> data) {
+ var innerSink = new DigestSink();
+ _hash.startChunkedConversion(innerSink)
+ ..add(padding)
+ ..add(data)
+ ..close();
+ return innerSink.value.bytes;
}
/// Closes [this] and returns the digest of the message as a list of bytes.
« no previous file with comments | « lib/src/hash_sink.dart ('k') | lib/src/md5.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698