OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 dart.crypto; | 5 library dart.crypto; |
6 | 6 |
7 import 'dart:math'; | 7 import 'dart:math'; |
8 | 8 |
9 part 'crypto_utils.dart'; | 9 part 'crypto_utils.dart'; |
10 part 'hash_utils.dart'; | 10 part 'hash_utils.dart'; |
11 part 'hmac.dart'; | 11 part 'hmac.dart'; |
12 part 'md5.dart'; | 12 part 'md5.dart'; |
13 part 'sha1.dart'; | 13 part 'sha1.dart'; |
14 part 'sha256.dart'; | 14 part 'sha256.dart'; |
15 | 15 |
16 /** | 16 /** |
17 * Interface for cryptographic hash functions. | 17 * Interface for cryptographic hash functions. |
18 * | 18 * |
19 * The [update] method is used to add data to the hash. The [digest] method | 19 * The [add] method is used to add data to the hash. The [close] method |
20 * is used to extract the message digest. | 20 * is used to extract the message digest. |
21 * | 21 * |
22 * Once the [digest] method has been called no more data can be added using the | 22 * Once the [close] method has been called no more data can be added using the |
23 * [update] method. If [update] is called after the first call to [digest] a | 23 * [add] method. If [add] is called after the first call to [close] a |
24 * HashException is thrown. | 24 * HashException is thrown. |
25 * | 25 * |
26 * If multiple instances of a given Hash is needed the [newInstance] | 26 * If multiple instances of a given Hash is needed the [newInstance] |
27 * method can provide a new instance. | 27 * method can provide a new instance. |
28 */ | 28 */ |
| 29 // TODO(floitsch): make Hash implement Sink, StreamSink or similar. |
29 abstract class Hash { | 30 abstract class Hash { |
30 /** | 31 /** |
31 * Add a list of bytes to the hash computation. | 32 * Add a list of bytes to the hash computation. |
32 */ | 33 */ |
33 Hash update(List<int> data); | 34 add(List<int> data); |
34 | 35 |
35 /** | 36 /** |
36 * Finish the hash computation and extract the message digest as | 37 * Finish the hash computation and extract the message digest as |
37 * a list of bytes. | 38 * a list of bytes. |
38 */ | 39 */ |
39 List<int> digest(); | 40 List<int> close(); |
40 | 41 |
41 /** | 42 /** |
42 * Returns a new instance of this hash function. | 43 * Returns a new instance of this hash function. |
43 */ | 44 */ |
44 Hash newInstance(); | 45 Hash newInstance(); |
45 | 46 |
46 /** | 47 /** |
47 * Internal block size of the hash in bytes. | 48 * Internal block size of the hash in bytes. |
48 * | 49 * |
49 * This is exposed for use by the HMAC class which needs to know the | 50 * This is exposed for use by the HMAC class which needs to know the |
(...skipping 22 matching lines...) Expand all Loading... |
72 * WARNING: MD5 has known collisions and should only be used when | 73 * WARNING: MD5 has known collisions and should only be used when |
73 * required for backwards compatibility. | 74 * required for backwards compatibility. |
74 */ | 75 */ |
75 abstract class MD5 implements Hash { | 76 abstract class MD5 implements Hash { |
76 factory MD5() => new _MD5(); | 77 factory MD5() => new _MD5(); |
77 } | 78 } |
78 | 79 |
79 /** | 80 /** |
80 * Hash-based Message Authentication Code support. | 81 * Hash-based Message Authentication Code support. |
81 * | 82 * |
82 * The [update] method is used to add data to the message. The [digest] method | 83 * The [add] method is used to add data to the message. The [digest] and |
83 * is used to extract the message authentication code. | 84 * [close] methods are used to extract the message authentication code. |
84 */ | 85 */ |
| 86 // TODO(floitsch): make Hash implement Sink, StreamSink or similar. |
85 abstract class HMAC { | 87 abstract class HMAC { |
86 /** | 88 /** |
87 * Create an [HMAC] object from a [Hash] and a key. | 89 * Create an [HMAC] object from a [Hash] and a key. |
88 */ | 90 */ |
89 factory HMAC(Hash hash, List<int> key) => new _HMAC(hash, key); | 91 factory HMAC(Hash hash, List<int> key) => new _HMAC(hash, key); |
90 | 92 |
91 /** | 93 /** |
92 * Add a list of bytes to the message. | 94 * Add a list of bytes to the message. |
93 */ | 95 */ |
94 HMAC update(List<int> data); | 96 add(List<int> data); |
95 | 97 |
96 /** | 98 /** |
97 * Perform the actual computation and extract the message digest | 99 * Perform the actual computation and extract the message digest |
98 * as a list of bytes. | 100 * as a list of bytes. |
99 */ | 101 */ |
100 List<int> digest(); | 102 List<int> close(); |
| 103 |
| 104 /** |
| 105 * Extract the message digest as a list of bytes without closing [this]. |
| 106 */ |
| 107 List<int> get digest; |
101 | 108 |
102 /** | 109 /** |
103 * Verify that the HMAC computed for the data so far matches the | 110 * Verify that the HMAC computed for the data so far matches the |
104 * given message digest. | 111 * given message digest. |
105 * | 112 * |
106 * This method should be used instead of memcmp-style comparisons | 113 * This method should be used instead of memcmp-style comparisons |
107 * to avoid leaking information via timing. | 114 * to avoid leaking information via timing. |
108 * | 115 * |
109 * Throws an exception if the given digest does not have the same | 116 * Throws an exception if the given digest does not have the same |
110 * size as the digest computed by this HMAC instance. | 117 * size as the digest computed by this HMAC instance. |
(...skipping 26 matching lines...) Expand all Loading... |
137 /** | 144 /** |
138 * HashExceptions are thrown on invalid use of a Hash | 145 * HashExceptions are thrown on invalid use of a Hash |
139 * object. | 146 * object. |
140 */ | 147 */ |
141 class HashException { | 148 class HashException { |
142 HashException(String this.message); | 149 HashException(String this.message); |
143 toString() => "HashException: $message"; | 150 toString() => "HashException: $message"; |
144 String message; | 151 String message; |
145 } | 152 } |
146 | 153 |
OLD | NEW |