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 import 'dart:typed_data'; |
| 13 import 'dart:convert'; |
| 14 |
| 15 part 'src/crypto_utils.dart'; |
| 16 part 'src/hash_utils.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, |
| 91 bool addLineSeparator : false}) { |
| 92 return _CryptoUtils.bytesToBase64(bytes, |
| 93 urlSafe, |
| 94 addLineSeparator); |
| 95 } |
| 96 |
| 97 |
| 98 /** |
| 99 * Converts a Base 64 encoded String into list of bytes. |
| 100 * |
| 101 * Decoder ignores "\r\n" sequences from input. |
| 102 * |
| 103 * Accepts both URL safe and unsafe Base 64 encoded strings. |
| 104 * |
| 105 * Throws a FormatException exception if input contains invalid characters. |
| 106 * |
| 107 * Based on [RFC 4648](http://tools.ietf.org/html/rfc4648) |
| 108 */ |
| 109 static List<int> base64StringToBytes(String input) { |
| 110 return _CryptoUtils.base64StringToBytes(input); |
| 111 } |
| 112 } |
OLD | NEW |