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