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

Side by Side Diff: sdk/lib/crypto/crypto.dart

Issue 15820008: Remove library dart:crypto (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments Created 7 years, 6 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/_internal/pub/lib/src/utils.dart ('k') | sdk/lib/crypto/crypto_utils.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library dart.crypto;
6
7 import 'dart:math';
8
9 part 'crypto_utils.dart';
10 part 'hash_utils.dart';
11 part 'hmac.dart';
12 part 'md5.dart';
13 part 'sha1.dart';
14 part 'sha256.dart';
15
16 /**
17 * Interface for cryptographic hash functions.
18 *
19 * The [add] method is used to add data to the hash. The [close] method
20 * is used to extract the message digest.
21 *
22 * Once the [close] method has been called no more data can be added using the
23 * [add] method. If [add] is called after the first call to [close] a
24 * HashException is thrown.
25 *
26 * If multiple instances of a given Hash is needed the [newInstance]
27 * method can provide a new instance.
28 */
29 // TODO(floitsch): make Hash implement Sink, EventSink or similar.
30 abstract class Hash {
31 /**
32 * Add a list of bytes to the hash computation.
33 */
34 add(List<int> data);
35
36 /**
37 * Finish the hash computation and extract the message digest as
38 * a list of bytes.
39 */
40 List<int> close();
41
42 /**
43 * Returns a new instance of this hash function.
44 */
45 Hash newInstance();
46
47 /**
48 * Internal block size of the hash in bytes.
49 *
50 * This is exposed for use by the HMAC class which needs to know the
51 * block size for the [Hash] it is using.
52 */
53 int get blockSize;
54 }
55
56 /**
57 * SHA1 hash function implementation.
58 */
59 abstract class SHA1 implements Hash {
60 factory SHA1() => new _SHA1();
61 }
62
63 /**
64 * SHA256 hash function implementation.
65 */
66 abstract 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 abstract 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 abstract 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.
124 */
125 abstract class CryptoUtils {
126 /**
127 * Convert a list of bytes (for example a message digest) into a hex
128 * string.
129 */
130 static String bytesToHex(List<int> bytes) {
131 return _CryptoUtils.bytesToHex(bytes);
132 }
133
134 /**
135 * Converts a list of bytes into a Base 64 encoded string.
136 *
137 * The list can be any list of integers in the range 0..255,
138 * for example a message digest.
139 *
140 * If [addLineSeparator] is true, the resulting string will be
141 * broken into lines of 76 characters, separated by "\r\n".
142 *
143 * If [urlSafe] is true, the result is URL and filename safe.
144 *
145 * Based on [RFC 4648](http://tools.ietf.org/html/rfc4648)
146 *
147 */
148 static String bytesToBase64(List<int> bytes,
149 {bool urlSafe : false,
150 bool addLineSeparator : false}) {
151 return _CryptoUtils.bytesToBase64(bytes,
152 urlSafe,
153 addLineSeparator);
154 }
155
156
157 /**
158 * Converts a Base 64 encoded String into list of bytes.
159 *
160 * Decoder ignores "\r\n" sequences from input. By default it also ignores
161 * all illegal characters unless [ignoreInvalidCharacters] is false.
162 *
163 * Accepts both URL safe and unsafe Base 64 encoded strings.
164 *
165 * Based on [RFC 4648](http://tools.ietf.org/html/rfc4648)
166 */
167 static List<int> base64StringToBytes(String input,
168 {bool ignoreInvalidCharacters : true}) {
169 return _CryptoUtils.base64StringToBytes(input, ignoreInvalidCharacters);
170 }
171 }
172
173 /**
174 * HashExceptions are thrown on invalid use of a Hash
175 * object.
176 */
177 class HashException {
178 HashException(String this.message);
179 toString() => "HashException: $message";
180 String message;
181 }
182
OLDNEW
« no previous file with comments | « sdk/lib/_internal/pub/lib/src/utils.dart ('k') | sdk/lib/crypto/crypto_utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698