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