OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library crypto.hash; | 5 library crypto.hash; |
6 | 6 |
| 7 import 'dart:convert'; |
| 8 |
| 9 import 'digest.dart'; |
| 10 import 'digest_sink.dart'; |
| 11 |
7 /// An interface for cryptographic hash functions. | 12 /// An interface for cryptographic hash functions. |
8 /// | 13 /// |
9 /// The [add] method adds data to the hash. The [close] method extracts the | 14 /// Every hash is a converter that takes a list of ints and returns a single |
10 /// message digest. | 15 /// digest. When used in chunked mode, it will only ever add one digest to the |
11 /// | 16 /// inner [Sink]. |
12 /// If multiple instances of a given Hash is needed, the [newInstance] method | 17 abstract class Hash extends Converter<List<int>, Digest> { |
13 /// can provide a new instance. | |
14 // TODO(floitsch): make Hash implement Sink, EventSink or similar. | |
15 abstract class Hash { | |
16 /// Add a list of bytes to the hash computation. | |
17 /// | |
18 /// If [this] has already been closed, throws a [StateError]. | |
19 void add(List<int> data); | |
20 | |
21 /// Finish the hash computation and extract the message digest as a list of | |
22 /// bytes. | |
23 List<int> close(); | |
24 | |
25 /// Returns a new instance of this hash function. | |
26 Hash newInstance(); | |
27 | |
28 /// The internal block size of the hash in bytes. | 18 /// The internal block size of the hash in bytes. |
29 /// | 19 /// |
30 /// This is exposed for use by the [HMAC] class, which needs to know the block | 20 /// This is exposed for use by the [HMAC] class, which needs to know the block |
31 /// size for the [Hash] it uses. | 21 /// size for the [Hash] it uses. |
32 int get blockSize; | 22 int get blockSize; |
| 23 |
| 24 /// The sink for implementing the deprecated APIs that involved adding data |
| 25 /// directly to the [Hash] instance. |
| 26 ByteConversionSink _sink; |
| 27 |
| 28 /// The sink that [_sink] sends the [Digest] to once it finishes hashing. |
| 29 final DigestSink _innerSink = new DigestSink(); |
| 30 |
| 31 Hash() { |
| 32 _sink = startChunkedConversion(_innerSink); |
| 33 } |
| 34 |
| 35 Digest convert(List<int> data) { |
| 36 var innerSink = new DigestSink(); |
| 37 var outerSink = startChunkedConversion(innerSink); |
| 38 outerSink.add(data); |
| 39 outerSink.close(); |
| 40 return innerSink.value; |
| 41 } |
| 42 |
| 43 ByteConversionSink startChunkedConversion(Sink<Digest> sink); |
| 44 |
| 45 /// Returns a new instance of this hash function. |
| 46 @Deprecated("Expires in 1.0.0. Use Hash.startChunkedConversion() instead.") |
| 47 Hash newInstance(); |
| 48 |
| 49 /// Add a list of bytes to the hash computation. |
| 50 /// |
| 51 /// If [this] has already been closed, throws a [StateError]. |
| 52 @Deprecated("Expires in 1.0.0. Use Hash.convert() or " |
| 53 "Hash.startChunkedConversion() instead.") |
| 54 void add(List<int> data) => _sink.add(data); |
| 55 |
| 56 /// Finish the hash computation and extract the message digest as a list of |
| 57 /// bytes. |
| 58 @Deprecated("Expires in 1.0.0. Use Hash.convert() or " |
| 59 "Hash.startChunkedConversion() instead.") |
| 60 List<int> close() { |
| 61 _sink.close(); |
| 62 return _innerSink.value.bytes; |
| 63 } |
33 } | 64 } |
OLD | NEW |