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 crypto; | 5 library crypto; |
6 | 6 |
7 import 'dart:math'; | 7 import 'dart:math'; |
8 | 8 |
9 part 'src/crypto_utils.dart'; | 9 part 'src/crypto_utils.dart'; |
10 part 'src/hash_utils.dart'; | 10 part 'src/hash_utils.dart'; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 /** | 47 /** |
48 * Internal block size of the hash in bytes. | 48 * Internal block size of the hash in bytes. |
49 * | 49 * |
50 * 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 |
51 * block size for the [Hash] it is using. | 51 * block size for the [Hash] it is using. |
52 */ | 52 */ |
53 int get blockSize; | 53 int get blockSize; |
54 } | 54 } |
55 | 55 |
56 /** | 56 /** |
57 * SHA1 hash function implementation. | |
58 */ | |
59 class SHA1 implements Hash { | |
60 factory SHA1() => new _SHA1(); | |
61 } | |
62 | |
63 /** | |
64 * SHA256 hash function implementation. | |
65 */ | |
66 class SHA256 implements Hash { | |
67 factory SHA256() => new _SHA256(); | |
68 } | |
69 | |
70 /** | |
71 * MD5 hash function implementation. | |
72 * | |
73 * WARNING: MD5 has known collisions and should only be used when | |
74 * required for backwards compatibility. | |
75 */ | |
76 class MD5 implements Hash { | |
77 factory MD5() => new _MD5(); | |
78 } | |
79 | |
80 /** | |
81 * Hash-based Message Authentication Code support. | |
82 * | |
83 * The [add] method is used to add data to the message. The [digest] and | |
84 * [close] methods are used to extract the message authentication code. | |
85 */ | |
86 // TODO(floitsch): make Hash implement Sink, EventSink or similar. | |
87 class HMAC { | |
88 /** | |
89 * Create an [HMAC] object from a [Hash] and a key. | |
90 */ | |
91 factory HMAC(Hash hash, List<int> key) => new _HMAC(hash, key); | |
92 | |
93 /** | |
94 * Add a list of bytes to the message. | |
95 */ | |
96 add(List<int> data); | |
97 | |
98 /** | |
99 * Perform the actual computation and extract the message digest | |
100 * as a list of bytes. | |
101 */ | |
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; | |
108 | |
109 /** | |
110 * Verify that the HMAC computed for the data so far matches the | |
111 * given message digest. | |
112 * | |
113 * This method should be used instead of memcmp-style comparisons | |
114 * to avoid leaking information via timing. | |
115 * | |
116 * Throws an exception if the given digest does not have the same | |
117 * size as the digest computed by this HMAC instance. | |
118 */ | |
119 bool verify(List<int> digest); | |
120 } | |
121 | |
122 /** | |
123 * Utility methods for working with message digests. | 57 * Utility methods for working with message digests. |
124 */ | 58 */ |
125 class CryptoUtils { | 59 class CryptoUtils { |
126 /** | 60 /** |
127 * Convert a list of bytes (for example a message digest) into a hex | 61 * Convert a list of bytes (for example a message digest) into a hex |
128 * string. | 62 * string. |
129 */ | 63 */ |
130 static String bytesToHex(List<int> bytes) { | 64 static String bytesToHex(List<int> bytes) { |
131 return _CryptoUtils.bytesToHex(bytes); | 65 return _CryptoUtils.bytesToHex(bytes); |
132 } | 66 } |
(...skipping 29 matching lines...) Expand all Loading... |
162 * | 96 * |
163 * Accepts both URL safe and unsafe Base 64 encoded strings. | 97 * Accepts both URL safe and unsafe Base 64 encoded strings. |
164 * | 98 * |
165 * Based on [RFC 4648](http://tools.ietf.org/html/rfc4648) | 99 * Based on [RFC 4648](http://tools.ietf.org/html/rfc4648) |
166 */ | 100 */ |
167 static List<int> base64StringToBytes(String input, | 101 static List<int> base64StringToBytes(String input, |
168 {bool ignoreInvalidCharacters : true}) { | 102 {bool ignoreInvalidCharacters : true}) { |
169 return _CryptoUtils.base64StringToBytes(input, ignoreInvalidCharacters); | 103 return _CryptoUtils.base64StringToBytes(input, ignoreInvalidCharacters); |
170 } | 104 } |
171 } | 105 } |
OLD | NEW |