| 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 #source('crypto_utils.dart'); | 9 #source('crypto_utils.dart'); |
| 10 #source('hash_utils.dart'); | 10 #source('hash_utils.dart'); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 | 45 |
| 46 /** | 46 /** |
| 47 * Block size of the hash in bytes. | 47 * Block size of the hash in bytes. |
| 48 */ | 48 */ |
| 49 int get blockSize; | 49 int get blockSize; |
| 50 } | 50 } |
| 51 | 51 |
| 52 /** | 52 /** |
| 53 * SHA1 hash function implementation. | 53 * SHA1 hash function implementation. |
| 54 */ | 54 */ |
| 55 interface SHA1 extends Hash default _SHA1 { | 55 abstract class SHA1 implements Hash { |
| 56 SHA1(); | 56 factory SHA1() => new _SHA1(); |
| 57 } | 57 } |
| 58 | 58 |
| 59 /** | 59 /** |
| 60 * SHA256 hash function implementation. | 60 * SHA256 hash function implementation. |
| 61 */ | 61 */ |
| 62 interface SHA256 extends Hash default _SHA256 { | 62 abstract class SHA256 implements Hash { |
| 63 SHA256(); | 63 factory SHA256() => new _SHA256(); |
| 64 } | 64 } |
| 65 | 65 |
| 66 /** | 66 /** |
| 67 * MD5 hash function implementation. | 67 * MD5 hash function implementation. |
| 68 * | 68 * |
| 69 * WARNING: MD5 has known collisions and should only be used when | 69 * WARNING: MD5 has known collisions and should only be used when |
| 70 * required for backwards compatibility. | 70 * required for backwards compatibility. |
| 71 */ | 71 */ |
| 72 interface MD5 extends Hash default _MD5 { | 72 abstract class MD5 implements Hash { |
| 73 MD5(); | 73 factory MD5() => new _MD5(); |
| 74 } | 74 } |
| 75 | 75 |
| 76 /** | 76 /** |
| 77 * Hash-based Message Authentication Code support. | 77 * Hash-based Message Authentication Code support. |
| 78 * | 78 * |
| 79 * The [update] method is used to add data to the message. The [digest] method | 79 * The [update] method is used to add data to the message. The [digest] method |
| 80 * is used to extract the message authentication code. | 80 * is used to extract the message authentication code. |
| 81 */ | 81 */ |
| 82 interface HMAC default _HMAC { | 82 abstract class HMAC { |
| 83 /** | 83 /** |
| 84 * Create an [HMAC] object from a [Hash] and a key. | 84 * Create an [HMAC] object from a [Hash] and a key. |
| 85 */ | 85 */ |
| 86 HMAC(Hash hash, List<int> key); | 86 factory HMAC(Hash hash, List<int> key) => new _HMAC(); |
| 87 | 87 |
| 88 /** | 88 /** |
| 89 * Add a list of bytes to the message. | 89 * Add a list of bytes to the message. |
| 90 */ | 90 */ |
| 91 HMAC update(List<int> data); | 91 HMAC update(List<int> data); |
| 92 | 92 |
| 93 /** | 93 /** |
| 94 * Perform the actual computation and extract the message digest | 94 * Perform the actual computation and extract the message digest |
| 95 * as a list of bytes. | 95 * as a list of bytes. |
| 96 */ | 96 */ |
| 97 List<int> digest(); | 97 List<int> digest(); |
| 98 } | 98 } |
| 99 | 99 |
| 100 /** | 100 /** |
| 101 * Utility methods for working with message digests. | 101 * Utility methods for working with message digests. |
| 102 */ | 102 */ |
| 103 class CryptoUtils { | 103 abstract class CryptoUtils { |
| 104 /** | 104 /** |
| 105 * Convert a list of bytes (for example a message digest) into a hex | 105 * Convert a list of bytes (for example a message digest) into a hex |
| 106 * string. | 106 * string. |
| 107 */ | 107 */ |
| 108 static String bytesToHex(List<int> bytes) { | 108 static String bytesToHex(List<int> bytes) { |
| 109 return _CryptoUtils.bytesToHex(bytes); | 109 return _CryptoUtils.bytesToHex(bytes); |
| 110 } | 110 } |
| 111 | 111 |
| 112 /** | 112 /** |
| 113 * Converts a list of bytes (for example a message digest) into a | 113 * Converts a list of bytes (for example a message digest) into a |
| 114 * base64 encoded string optionally broken up in to lines of | 114 * base64 encoded string optionally broken up in to lines of |
| 115 * [lineLength] chars separated by '\r\n'. | 115 * [lineLength] chars separated by '\r\n'. |
| 116 */ | 116 */ |
| 117 static String bytesToBase64(List<int> bytes, [int lineLength]) { | 117 static String bytesToBase64(List<int> bytes, [int lineLength]) { |
| 118 return _CryptoUtils.bytesToBase64(bytes, lineLength); | 118 return _CryptoUtils.bytesToBase64(bytes, lineLength); |
| 119 } | 119 } |
| 120 } | 120 } |
| 121 | 121 |
| 122 /** | 122 /** |
| 123 * HashExceptions are thrown on invalid use of a Hash | 123 * HashExceptions are thrown on invalid use of a Hash |
| 124 * object. | 124 * object. |
| 125 */ | 125 */ |
| 126 class HashException { | 126 class HashException { |
| 127 HashException(String this.message); | 127 HashException(String this.message); |
| 128 toString() => "HashException: $message"; | 128 toString() => "HashException: $message"; |
| 129 String message; | 129 String message; |
| 130 } | 130 } |
| 131 | 131 |
| OLD | NEW |