| 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 // TODO(ager, iposva): Get rid of this file when the VM snapshot | 5 // TODO(ager, iposva): Get rid of this file when the VM snapshot |
| 6 // generation can deal with normal library structure. | 6 // generation can deal with normal library structure. |
| 7 | 7 |
| 8 library crypto; | 8 library crypto; |
| 9 | 9 |
| 10 import 'dart:math'; | 10 import 'dart:math'; |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * Interface for cryptographic hash functions. | 13 * Interface for cryptographic hash functions. |
| 14 * | 14 * |
| 15 * The [update] method is used to add data to the hash. The [digest] method | 15 * The [update] method is used to add data to the hash. The [digest] method |
| 16 * is used to extract the message digest. | 16 * is used to extract the message digest. |
| 17 * | 17 * |
| 18 * Once the [digest] method has been called no more data can be added using the | 18 * Once the [digest] method has been called no more data can be added using the |
| 19 * [update] method. If [update] is called after the first call to [digest] a | 19 * [update] method. If [update] is called after the first call to [digest] a |
| 20 * HashException is thrown. | 20 * HashException is thrown. |
| 21 * | 21 * |
| 22 * If multiple instances of a given Hash is needed the [newInstance] | 22 * If multiple instances of a given Hash is needed the [newInstance] |
| 23 * method can provide a new instance. | 23 * method can provide a new instance. |
| 24 */ | 24 */ |
| 25 interface Hash { | 25 abstract class Hash { |
| 26 /** | 26 /** |
| 27 * Add a list of bytes to the hash computation. | 27 * Add a list of bytes to the hash computation. |
| 28 */ | 28 */ |
| 29 Hash update(List<int> data); | 29 Hash update(List<int> data); |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * Finish the hash computation and extract the message digest as | 32 * Finish the hash computation and extract the message digest as |
| 33 * a list of bytes. | 33 * a list of bytes. |
| 34 */ | 34 */ |
| 35 List<int> digest(); | 35 List<int> digest(); |
| 36 | 36 |
| 37 /** | 37 /** |
| 38 * Returns a new instance of this hash function. | 38 * Returns a new instance of this hash function. |
| 39 */ | 39 */ |
| 40 Hash newInstance(); | 40 Hash newInstance(); |
| 41 | 41 |
| 42 /** | 42 /** |
| 43 * Block size of the hash in bytes. | 43 * Block size of the hash in bytes. |
| 44 */ | 44 */ |
| 45 int get blockSize; | 45 int get blockSize; |
| 46 } | 46 } |
| 47 | 47 |
| 48 /** | 48 /** |
| 49 * SHA1 hash function implementation. | 49 * SHA1 hash function implementation. |
| 50 */ | 50 */ |
| 51 interface SHA1 extends Hash default _SHA1 { | 51 abstract class SHA1 implements Hash { |
| 52 SHA1(); | 52 factory SHA1() => new _SHA1(); |
| 53 } | 53 } |
| 54 | 54 |
| 55 /** | 55 /** |
| 56 * SHA256 hash function implementation. | 56 * SHA256 hash function implementation. |
| 57 */ | 57 */ |
| 58 interface SHA256 extends Hash default _SHA256 { | 58 abstract class SHA256 implements Hash { |
| 59 SHA256(); | 59 factory SHA256() => new _SHA256(); |
| 60 } | 60 } |
| 61 | 61 |
| 62 /** | 62 /** |
| 63 * MD5 hash function implementation. | 63 * MD5 hash function implementation. |
| 64 * | 64 * |
| 65 * WARNING: MD5 has known collisions and should only be used when | 65 * WARNING: MD5 has known collisions and should only be used when |
| 66 * required for backwards compatibility. | 66 * required for backwards compatibility. |
| 67 */ | 67 */ |
| 68 interface MD5 extends Hash default _MD5 { | 68 abstract class MD5 implements Hash { |
| 69 MD5(); | 69 factory MD5() => new _MD5(); |
| 70 } | 70 } |
| 71 | 71 |
| 72 /** | 72 /** |
| 73 * Hash-based Message Authentication Code support. | 73 * Hash-based Message Authentication Code support. |
| 74 * | 74 * |
| 75 * The [update] method is used to add data to the message. The [digest] method | 75 * The [update] method is used to add data to the message. The [digest] method |
| 76 * is used to extract the message authentication code. | 76 * is used to extract the message authentication code. |
| 77 */ | 77 */ |
| 78 interface HMAC default _HMAC { | 78 abstract class HMAC { |
| 79 /** | 79 /** |
| 80 * Create an [HMAC] object from a [Hash] and a key. | 80 * Create an [HMAC] object from a [Hash] and a key. |
| 81 */ | 81 */ |
| 82 HMAC(Hash hash, List<int> key); | 82 factory HMAC(Hash hash, List<int> key) => new _HMAC(hash, key); |
| 83 | 83 |
| 84 /** | 84 /** |
| 85 * Add a list of bytes to the message. | 85 * Add a list of bytes to the message. |
| 86 */ | 86 */ |
| 87 HMAC update(List<int> data); | 87 HMAC update(List<int> data); |
| 88 | 88 |
| 89 /** | 89 /** |
| 90 * Perform the actual computation and extract the message digest | 90 * Perform the actual computation and extract the message digest |
| 91 * as a list of bytes. | 91 * as a list of bytes. |
| 92 */ | 92 */ |
| 93 List<int> digest(); | 93 List<int> digest(); |
| 94 } | 94 } |
| 95 | 95 |
| 96 /** | 96 /** |
| 97 * Utility methods for working with message digests. | 97 * Utility methods for working with message digests. |
| 98 */ | 98 */ |
| 99 class CryptoUtils { | 99 abstract class CryptoUtils { |
| 100 /** | 100 /** |
| 101 * Convert a list of bytes (for example a message digest) into a hex | 101 * Convert a list of bytes (for example a message digest) into a hex |
| 102 * string. | 102 * string. |
| 103 */ | 103 */ |
| 104 static String bytesToHex(List<int> bytes) { | 104 static String bytesToHex(List<int> bytes) { |
| 105 return _CryptoUtils.bytesToHex(bytes); | 105 return _CryptoUtils.bytesToHex(bytes); |
| 106 } | 106 } |
| 107 | 107 |
| 108 /** | 108 /** |
| 109 * Converts a list of bytes (for example a message digest) into a | 109 * Converts a list of bytes (for example a message digest) into a |
| 110 * base64 encoded string optionally broken up in to lines of | 110 * base64 encoded string optionally broken up in to lines of |
| 111 * [lineLength] chars separated by '\r\n'. | 111 * [lineLength] chars separated by '\r\n'. |
| 112 */ | 112 */ |
| 113 static String bytesToBase64(List<int> bytes, [int lineLength]) { | 113 static String bytesToBase64(List<int> bytes, [int lineLength]) { |
| 114 return _CryptoUtils.bytesToBase64(bytes, lineLength); | 114 return _CryptoUtils.bytesToBase64(bytes, lineLength); |
| 115 } | 115 } |
| 116 } | 116 } |
| 117 | 117 |
| 118 /** | 118 /** |
| 119 * HashExceptions are thrown on invalid use of a Hash | 119 * HashExceptions are thrown on invalid use of a Hash |
| 120 * object. | 120 * object. |
| 121 */ | 121 */ |
| 122 class HashException { | 122 class HashException { |
| 123 HashException(String this.message); | 123 HashException(String this.message); |
| 124 toString() => "HashException: $message"; | 124 toString() => "HashException: $message"; |
| 125 String message; | 125 String message; |
| 126 } | 126 } |
| 127 | 127 |
| OLD | NEW |