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.crypto_utils; | 5 library crypto.crypto_utils; |
6 | 6 |
7 import 'base64.dart'; | 7 import 'base64.dart'; |
8 | 8 |
9 /** | 9 /// Utility methods for working with message digests. |
10 * Utility methods for working with message digests. | |
11 */ | |
12 abstract class CryptoUtils { | 10 abstract class CryptoUtils { |
13 /** | 11 /// Convert a list of bytes (for example a message digest) into a hexadecimal |
14 * Convert a list of bytes (for example a message digest) into a hex | 12 /// string. |
15 * string. | |
16 */ | |
17 static String bytesToHex(List<int> bytes) { | 13 static String bytesToHex(List<int> bytes) { |
18 var result = new StringBuffer(); | 14 var result = new StringBuffer(); |
19 for (var part in bytes) { | 15 for (var part in bytes) { |
20 result.write('${part < 16 ? '0' : ''}${part.toRadixString(16)}'); | 16 result.write('${part < 16 ? '0' : ''}${part.toRadixString(16)}'); |
21 } | 17 } |
22 return result.toString(); | 18 return result.toString(); |
23 } | 19 } |
24 | 20 |
25 /** | 21 /// Converts a list of bytes into a [Base64-encoded][rfc] string. |
26 * Converts a list of bytes into a Base 64 encoded string. | 22 /// |
27 * | 23 /// [rfc]: https://tools.ietf.org/html/rfc4648 |
28 * The list can be any list of integers in the range 0..255, | 24 /// |
29 * for example a message digest. | 25 /// The list can be any list of integers from 0 to 255 inclusive, for example |
30 * | 26 /// a message digest. |
31 * If [addLineSeparator] is true, the resulting string will be | 27 /// |
32 * broken into lines of 76 characters, separated by "\r\n". | 28 /// If [addLineSeparator] is true, the resulting string will be |
33 * | 29 /// broken into lines of 76 characters, separated by "\r\n". |
34 * If [urlSafe] is true, the result is URL and filename safe. | 30 /// |
35 * | 31 /// If [urlSafe] is true, the resulting string will be URL- and filename- |
36 * Based on [RFC 4648](http://tools.ietf.org/html/rfc4648) | 32 /// safe. |
37 * | |
38 */ | |
39 static String bytesToBase64(List<int> bytes, | 33 static String bytesToBase64(List<int> bytes, |
40 [bool urlSafe = false, bool addLineSeparator = false]) { | 34 [bool urlSafe = false, bool addLineSeparator = false]) { |
41 return BASE64.encode(bytes, | 35 return BASE64.encode(bytes, |
42 urlSafe: urlSafe, addLineSeparator: addLineSeparator); | 36 urlSafe: urlSafe, addLineSeparator: addLineSeparator); |
43 } | 37 } |
44 | 38 |
45 /** | 39 /// Converts a [Base64-encoded][rfc] String into list of bytes. |
46 * Converts a Base 64 encoded String into list of bytes. | 40 /// |
47 * | 41 /// [rfc]: https://tools.ietf.org/html/rfc4648 |
48 * Decoder ignores "\r\n" sequences from input. | 42 /// |
49 * | 43 /// This ignores "\r\n" sequences in [input]. It accepts both URL-safe and |
50 * Accepts both URL safe and unsafe Base 64 encoded strings. | 44 /// -unsafe Base 64 encoded strings. |
51 * | 45 /// |
52 * Throws a FormatException exception if input contains invalid characters. | 46 /// Throws a [FormatException] if [input] contains invalid characters. |
53 * | |
54 * Based on [RFC 4648](http://tools.ietf.org/html/rfc4648) | |
55 */ | |
56 static List<int> base64StringToBytes(String input) { | 47 static List<int> base64StringToBytes(String input) { |
57 return BASE64.decode(input); | 48 return BASE64.decode(input); |
58 } | 49 } |
59 } | 50 } |
OLD | NEW |