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

Unified Diff: lib/src/hash.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/digest_sink.dart ('k') | lib/src/hash_base.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/hash.dart
diff --git a/lib/src/hash.dart b/lib/src/hash.dart
index bb228fa55fed0fc14992eedfa6330b71312f19cd..6f9b33ba0f7fe7d41e5adcad93cb90cdd629fb74 100644
--- a/lib/src/hash.dart
+++ b/lib/src/hash.dart
@@ -4,30 +4,61 @@
library crypto.hash;
+import 'dart:convert';
+
+import 'digest.dart';
+import 'digest_sink.dart';
+
/// An interface for cryptographic hash functions.
///
-/// The [add] method adds data to the hash. The [close] method extracts the
-/// message digest.
-///
-/// If multiple instances of a given Hash is needed, the [newInstance] method
-/// can provide a new instance.
-// TODO(floitsch): make Hash implement Sink, EventSink or similar.
-abstract class Hash {
- /// Add a list of bytes to the hash computation.
+/// Every hash is a converter that takes a list of ints and returns a single
+/// digest. When used in chunked mode, it will only ever add one digest to the
+/// inner [Sink].
+abstract class Hash extends Converter<List<int>, Digest> {
+ /// The internal block size of the hash in bytes.
///
- /// If [this] has already been closed, throws a [StateError].
- void add(List<int> data);
+ /// This is exposed for use by the [HMAC] class, which needs to know the block
+ /// size for the [Hash] it uses.
+ int get blockSize;
- /// Finish the hash computation and extract the message digest as a list of
- /// bytes.
- List<int> close();
+ /// The sink for implementing the deprecated APIs that involved adding data
+ /// directly to the [Hash] instance.
+ ByteConversionSink _sink;
+
+ /// The sink that [_sink] sends the [Digest] to once it finishes hashing.
+ final DigestSink _innerSink = new DigestSink();
+
+ Hash() {
+ _sink = startChunkedConversion(_innerSink);
+ }
+
+ Digest convert(List<int> data) {
+ var innerSink = new DigestSink();
+ var outerSink = startChunkedConversion(innerSink);
+ outerSink.add(data);
+ outerSink.close();
+ return innerSink.value;
+ }
+
+ ByteConversionSink startChunkedConversion(Sink<Digest> sink);
/// Returns a new instance of this hash function.
+ @Deprecated("Expires in 1.0.0. Use Hash.startChunkedConversion() instead.")
Hash newInstance();
- /// The internal block size of the hash in bytes.
+ /// Add a list of bytes to the hash computation.
///
- /// This is exposed for use by the [HMAC] class, which needs to know the block
- /// size for the [Hash] it uses.
- int get blockSize;
+ /// If [this] has already been closed, throws a [StateError].
+ @Deprecated("Expires in 1.0.0. Use Hash.convert() or "
+ "Hash.startChunkedConversion() instead.")
+ void add(List<int> data) => _sink.add(data);
+
+ /// Finish the hash computation and extract the message digest as a list of
+ /// bytes.
+ @Deprecated("Expires in 1.0.0. Use Hash.convert() or "
+ "Hash.startChunkedConversion() instead.")
+ List<int> close() {
+ _sink.close();
+ return _innerSink.value.bytes;
+ }
}
« no previous file with comments | « lib/src/digest_sink.dart ('k') | lib/src/hash_base.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698