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 /** | 5 /** |
6 * Cryptographic algorithms, with support for hash functions such as | 6 * Cryptographic algorithms, with support for hash functions such as |
7 * SHA-1, SHA-256, HMAC, and MD5. | 7 * SHA-1, SHA-256, HMAC, and MD5. |
8 */ | 8 */ |
9 library crypto; | 9 library crypto; |
10 | 10 |
11 import 'dart:math'; | 11 export 'src/crypto_utils.dart'; |
12 import 'dart:typed_data'; | 12 export 'src/hmac.dart'; |
13 import 'dart:convert'; | 13 export 'src/md5.dart'; |
14 | 14 export 'src/sha1.dart'; |
15 part 'src/crypto_utils.dart'; | 15 export 'src/sha256.dart'; |
16 part 'src/hash_utils.dart'; | 16 export 'src/base64.dart'; |
17 part 'src/hmac.dart'; | |
18 part 'src/md5.dart'; | |
19 part 'src/sha1.dart'; | |
20 part 'src/sha256.dart'; | |
21 part 'src/base64.dart'; | |
22 | |
23 /** | |
24 * Interface for cryptographic hash functions. | |
25 * | |
26 * The [add] method is used to add data to the hash. The [close] method | |
27 * is used to extract the message digest. | |
28 * | |
29 * Once the [close] method has been called no more data can be added using the | |
30 * [add] method. If [add] is called after the first call to [close] a | |
31 * HashException is thrown. | |
32 * | |
33 * If multiple instances of a given Hash is needed the [newInstance] | |
34 * method can provide a new instance. | |
35 */ | |
36 // TODO(floitsch): make Hash implement Sink, EventSink or similar. | |
37 abstract class Hash { | |
38 /** | |
39 * Add a list of bytes to the hash computation. | |
40 */ | |
41 void add(List<int> data); | |
42 | |
43 /** | |
44 * Finish the hash computation and extract the message digest as | |
45 * a list of bytes. | |
46 */ | |
47 List<int> close(); | |
48 | |
49 /** | |
50 * Returns a new instance of this hash function. | |
51 */ | |
52 Hash newInstance(); | |
53 | |
54 /** | |
55 * Internal block size of the hash in bytes. | |
56 * | |
57 * This is exposed for use by the HMAC class which needs to know the | |
58 * block size for the [Hash] it is using. | |
59 */ | |
60 int get blockSize; | |
61 } | |
62 | |
63 /** | |
64 * Utility methods for working with message digests. | |
65 */ | |
66 class CryptoUtils { | |
67 /** | |
68 * Convert a list of bytes (for example a message digest) into a hex | |
69 * string. | |
70 */ | |
71 static String bytesToHex(List<int> bytes) { | |
72 return _CryptoUtils.bytesToHex(bytes); | |
73 } | |
74 | |
75 /** | |
76 * Converts a list of bytes into a Base 64 encoded string. | |
77 * | |
78 * The list can be any list of integers in the range 0..255, | |
79 * for example a message digest. | |
80 * | |
81 * If [addLineSeparator] is true, the resulting string will be | |
82 * broken into lines of 76 characters, separated by "\r\n". | |
83 * | |
84 * If [urlSafe] is true, the result is URL and filename safe. | |
85 * | |
86 * Based on [RFC 4648](http://tools.ietf.org/html/rfc4648) | |
87 * | |
88 */ | |
89 static String bytesToBase64(List<int> bytes, | |
90 {bool urlSafe: false, bool addLineSeparator: false}) { | |
91 return _CryptoUtils.bytesToBase64(bytes, urlSafe, addLineSeparator); | |
92 } | |
93 | |
94 /** | |
95 * Converts a Base 64 encoded String into list of bytes. | |
96 * | |
97 * Decoder ignores "\r\n" sequences from input. | |
98 * | |
99 * Accepts both URL safe and unsafe Base 64 encoded strings. | |
100 * | |
101 * Throws a FormatException exception if input contains invalid characters. | |
102 * | |
103 * Based on [RFC 4648](http://tools.ietf.org/html/rfc4648) | |
104 */ | |
105 static List<int> base64StringToBytes(String input) { | |
106 return _CryptoUtils.base64StringToBytes(input); | |
107 } | |
108 } | |
OLD | NEW |