| 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 dart.crypto; | 5 library dart.crypto; |
| 6 | 6 |
| 7 import 'dart:math'; | 7 import 'dart:math'; |
| 8 | 8 |
| 9 part 'crypto_base.dart'; |
| 9 part 'crypto_utils.dart'; | 10 part 'crypto_utils.dart'; |
| 10 part 'hash_utils.dart'; | 11 part 'hash_utils.dart'; |
| 11 part 'hmac.dart'; | 12 part 'hmac.dart'; |
| 12 part 'md5.dart'; | 13 part 'md5.dart'; |
| 13 part 'sha1.dart'; | 14 part 'sha1.dart'; |
| 14 part 'sha256.dart'; | 15 part 'sha256.dart'; |
| 15 | |
| 16 /** | |
| 17 * Interface for cryptographic hash functions. | |
| 18 * | |
| 19 * The [add] method is used to add data to the hash. The [close] method | |
| 20 * is used to extract the message digest. | |
| 21 * | |
| 22 * Once the [close] method has been called no more data can be added using the | |
| 23 * [add] method. If [add] is called after the first call to [close] a | |
| 24 * HashException is thrown. | |
| 25 * | |
| 26 * If multiple instances of a given Hash is needed the [newInstance] | |
| 27 * method can provide a new instance. | |
| 28 */ | |
| 29 // TODO(floitsch): make Hash implement Sink, StreamSink or similar. | |
| 30 abstract class Hash { | |
| 31 /** | |
| 32 * Add a list of bytes to the hash computation. | |
| 33 */ | |
| 34 add(List<int> data); | |
| 35 | |
| 36 /** | |
| 37 * Finish the hash computation and extract the message digest as | |
| 38 * a list of bytes. | |
| 39 */ | |
| 40 List<int> close(); | |
| 41 | |
| 42 /** | |
| 43 * Returns a new instance of this hash function. | |
| 44 */ | |
| 45 Hash newInstance(); | |
| 46 | |
| 47 /** | |
| 48 * Internal block size of the hash in bytes. | |
| 49 * | |
| 50 * This is exposed for use by the HMAC class which needs to know the | |
| 51 * block size for the [Hash] it is using. | |
| 52 */ | |
| 53 int get blockSize; | |
| 54 } | |
| 55 | |
| 56 /** | |
| 57 * SHA1 hash function implementation. | |
| 58 */ | |
| 59 abstract class SHA1 implements Hash { | |
| 60 factory SHA1() => new _SHA1(); | |
| 61 } | |
| 62 | |
| 63 /** | |
| 64 * SHA256 hash function implementation. | |
| 65 */ | |
| 66 abstract class SHA256 implements Hash { | |
| 67 factory SHA256() => new _SHA256(); | |
| 68 } | |
| 69 | |
| 70 /** | |
| 71 * MD5 hash function implementation. | |
| 72 * | |
| 73 * WARNING: MD5 has known collisions and should only be used when | |
| 74 * required for backwards compatibility. | |
| 75 */ | |
| 76 abstract class MD5 implements Hash { | |
| 77 factory MD5() => new _MD5(); | |
| 78 } | |
| 79 | |
| 80 /** | |
| 81 * Hash-based Message Authentication Code support. | |
| 82 * | |
| 83 * The [add] method is used to add data to the message. The [digest] and | |
| 84 * [close] methods are used to extract the message authentication code. | |
| 85 */ | |
| 86 // TODO(floitsch): make Hash implement Sink, StreamSink or similar. | |
| 87 abstract class HMAC { | |
| 88 /** | |
| 89 * Create an [HMAC] object from a [Hash] and a key. | |
| 90 */ | |
| 91 factory HMAC(Hash hash, List<int> key) => new _HMAC(hash, key); | |
| 92 | |
| 93 /** | |
| 94 * Add a list of bytes to the message. | |
| 95 */ | |
| 96 add(List<int> data); | |
| 97 | |
| 98 /** | |
| 99 * Perform the actual computation and extract the message digest | |
| 100 * as a list of bytes. | |
| 101 */ | |
| 102 List<int> close(); | |
| 103 | |
| 104 /** | |
| 105 * Extract the message digest as a list of bytes without closing [this]. | |
| 106 */ | |
| 107 List<int> get digest; | |
| 108 | |
| 109 /** | |
| 110 * Verify that the HMAC computed for the data so far matches the | |
| 111 * given message digest. | |
| 112 * | |
| 113 * This method should be used instead of memcmp-style comparisons | |
| 114 * to avoid leaking information via timing. | |
| 115 * | |
| 116 * Throws an exception if the given digest does not have the same | |
| 117 * size as the digest computed by this HMAC instance. | |
| 118 */ | |
| 119 bool verify(List<int> digest); | |
| 120 } | |
| 121 | |
| 122 /** | |
| 123 * Utility methods for working with message digests. | |
| 124 */ | |
| 125 abstract class CryptoUtils { | |
| 126 /** | |
| 127 * Convert a list of bytes (for example a message digest) into a hex | |
| 128 * string. | |
| 129 */ | |
| 130 static String bytesToHex(List<int> bytes) { | |
| 131 return _CryptoUtils.bytesToHex(bytes); | |
| 132 } | |
| 133 | |
| 134 /** | |
| 135 * Converts a list of bytes (for example a message digest) into a | |
| 136 * base64 encoded string optionally broken up in to lines of | |
| 137 * [lineLength] chars separated by '\r\n'. | |
| 138 */ | |
| 139 static String bytesToBase64(List<int> bytes, [int lineLength]) { | |
| 140 return _CryptoUtils.bytesToBase64(bytes, lineLength); | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 /** | |
| 145 * HashExceptions are thrown on invalid use of a Hash | |
| 146 * object. | |
| 147 */ | |
| 148 class HashException { | |
| 149 HashException(String this.message); | |
| 150 toString() => "HashException: $message"; | |
| 151 String message; | |
| 152 } | |
| 153 | |
| OLD | NEW |