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