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