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 import 'dart:convert'; | 5 import 'dart:convert'; |
6 | 6 |
7 import 'digest.dart'; | 7 import 'digest.dart'; |
8 import 'digest_sink.dart'; | 8 import 'digest_sink.dart'; |
9 | 9 |
10 /// An interface for cryptographic hash functions. | 10 /// An interface for cryptographic hash functions. |
11 /// | 11 /// |
12 /// Every hash is a converter that takes a list of ints and returns a single | 12 /// Every hash is a converter that takes a list of ints and returns a single |
13 /// digest. When used in chunked mode, it will only ever add one digest to the | 13 /// digest. When used in chunked mode, it will only ever add one digest to the |
14 /// inner [Sink]. | 14 /// inner [Sink]. |
15 abstract class Hash extends Converter<List<int>, Digest> { | 15 abstract class Hash extends Converter<List<int>, Digest> { |
16 /// The internal block size of the hash in bytes. | 16 /// The internal block size of the hash in bytes. |
17 /// | 17 /// |
18 /// This is exposed for use by the [HMAC] class, which needs to know the block | 18 /// This is exposed for use by the [Hmac] class, which needs to know the block |
19 /// size for the [Hash] it uses. | 19 /// size for the [Hash] it uses. |
20 int get blockSize; | 20 int get blockSize; |
21 | 21 |
22 /// The sink for implementing the deprecated APIs that involved adding data | 22 const Hash(); |
23 /// directly to the [Hash] instance. | |
24 ByteConversionSink _sink; | |
25 | |
26 /// The sink that [_sink] sends the [Digest] to once it finishes hashing. | |
27 final DigestSink _innerSink = new DigestSink(); | |
28 | |
29 Hash() { | |
30 _sink = startChunkedConversion(_innerSink); | |
31 } | |
32 | 23 |
33 Digest convert(List<int> data) { | 24 Digest convert(List<int> data) { |
34 var innerSink = new DigestSink(); | 25 var innerSink = new DigestSink(); |
35 var outerSink = startChunkedConversion(innerSink); | 26 var outerSink = startChunkedConversion(innerSink); |
36 outerSink.add(data); | 27 outerSink.add(data); |
37 outerSink.close(); | 28 outerSink.close(); |
38 return innerSink.value; | 29 return innerSink.value; |
39 } | 30 } |
40 | 31 |
41 ByteConversionSink startChunkedConversion(Sink<Digest> sink); | 32 ByteConversionSink startChunkedConversion(Sink<Digest> sink); |
42 | |
43 /// This is deprecated. | |
44 /// | |
45 /// Use [startChunkedConversion] instead. | |
46 @Deprecated("Will be removed in crypto 1.0.0.") | |
47 Hash newInstance(); | |
48 | |
49 /// This is deprecated. | |
50 /// | |
51 /// Use [convert] or [startChunkedConversion] instead. | |
52 @Deprecated("Will be removed in crypto 1.0.0.") | |
53 void add(List<int> data) => _sink.add(data); | |
54 | |
55 /// This is deprecated. | |
56 /// | |
57 /// Use [convert] or [startChunkedConversion] instead. | |
58 @Deprecated("Will be removed in crypto 1.0.0.") | |
59 List<int> close() { | |
60 _sink.close(); | |
61 return _innerSink.value.bytes; | |
62 } | |
63 } | 33 } |
OLD | NEW |