add methods to decrypt return data from router
[GitHub/Stricted/speedport-hybrid-php-api.git] / CryptLib / Hash / Hash.php
CommitLineData
14d4f286
S
1<?php
2/**
3 * A hash utility data mapper class
4 *
5 * This class's purpose is to store information about hash algorithms that is
6 * otherwise unavailable during runtime. Some information is available (such
7 * as the output size), but is included anyway for performance and completeness
8 * reasons.
9 *
10 * PHP version 5.3
11 *
12 * @category PHPCryptLib
13 * @package Hash
14 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
15 * @copyright 2011 The Authors
16 * @license http://www.opensource.org/licenses/mit-license.html MIT License
17 * @version Build @@version@@
18 */
19namespace CryptLib\Hash;
20
21/**
22 * A hash utility data mapper class
23 *
24 * This class's purpose is to store information about hash algorithms that is
25 * otherwise unavailable during runtime. Some information is available (such
26 * as the output size), but is included anyway for performance and completeness
27 * reasons.
28 *
29 * PHP version 5.3
30 *
31 * @category PHPCryptLib
32 * @package Hash
33 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
34 */
35class Hash {
36
37 /**
38 * This array contains information about each hash function available to PHP
39 * at the present time. Block sizes are not available from functions, so they
40 * must be hard coded.
41 *
42 * The "secure" indicates the strength of the hash and whether or not any known
43 * cryptographic attacks exist for the hash function. This will only apply when
44 * using the hash functions for situations that require cryptographic strength
45 * such as message signing. For other uses the insecure ones can have valid
46 * uses.
47 *
48 * @var array An array of information about each supported hash function
49 */
50 protected static $hashInfo = array(
51 'md2' => array(
52 'HashSize' => 128,
53 'BlockSize' => 128,
54 'secure' => false,
55 ),
56 'md4' => array(
57 'HashSize' => 128,
58 'BlockSize' => 512,
59 'secure' => false,
60 ),
61 'md5' => array(
62 'HashSize' => 128,
63 'BlockSize' => 512,
64 'secure' => false,
65 ),
66 'sha1' => array(
67 'HashSize' => 160,
68 'BlockSize' => 512,
69 'secure' => false,
70 ),
71 'sha224' => array(
72 'HashSize' => 224,
73 'BlockSize' => 512,
74 'secure' => true,
75 ),
76 'sha256' => array(
77 'HashSize' => 256,
78 'BlockSize' => 512,
79 'secure' => true,
80 ),
81 'sha384' => array(
82 'HashSize' => 384,
83 'BlockSize' => 1024,
84 'secure' => true,
85 ),
86 'sha512' => array(
87 'HashSize' => 512,
88 'BlockSize' => 1024,
89 'secure' => true,
90 ),
91 'ripemd128' => array(
92 'HashSize' => 128,
93 'BlockSize' => 512,
94 'secure' => true,
95 ),
96 'ripemd160' => array(
97 'HashSize' => 160,
98 'BlockSize' => 512,
99 'secure' => true,
100 ),
101 'ripemd256' => array(
102 'HashSize' => 256,
103 'BlockSize' => 512,
104 'secure' => true,
105 ),
106 'ripemd320' => array(
107 'HashSize' => 320,
108 'BlockSize' => 512,
109 'secure' => true,
110 ),
111 'whirlpool' => array(
112 'HashSize' => 512,
113 'BlockSize' => 512,
114 'secure' => true,
115 ),
116 'tiger128,3' => array(
117 'HashSize' => 128,
118 'BlockSize' => 512,
119 'secure' => true,
120 ),
121 'tiger160,3' => array(
122 'HashSize' => 160,
123 'BlockSize' => 512,
124 'secure' => true,
125 ),
126 'tiger192,3' => array(
127 'HashSize' => 192,
128 'BlockSize' => 512,
129 'secure' => true,
130 ),
131 'tiger128,4' => array(
132 'HashSize' => 128,
133 'BlockSize' => 512,
134 'secure' => true,
135 ),
136 'tiger160,4' => array(
137 'HashSize' => 160,
138 'BlockSize' => 512,
139 'secure' => true,
140 ),
141 'tiger192,4' => array(
142 'HashSize' => 192,
143 'BlockSize' => 512,
144 'secure' => true,
145 ),
146 'snefru' => array(
147 'HashSize' => 256,
148 'BlockSize' => 512,
149 'secure' => false,
150 ),
151 'snefru256' => array(
152 'HashSize' => 256,
153 'BlockSize' => 512,
154 'secure' => false,
155 ),
156 'gost' => array(
157 'HashSize' => 256,
158 'BlockSize' => 256,
159 'secure' => false,
160 ),
161 'adler32' => array(
162 'HashSize' => 32,
163 'BlockSize' => 16,
164 'secure' => false,
165 ),
166 'crc32' => array(
167 'HashSize' => 32,
168 'BlockSize' => 32,
169 'secure' => false,
170 ),
171 'crc32b' => array(
172 'HashSize' => 32,
173 'BlockSize' => 32,
174 'secure' => false,
175 ),
176 'salsa10' => array(
177 'HashSize' => 512,
178 'BlockSize' => 512,
179 'secure' => true,
180 ),
181 'salsa20' => array(
182 'HashSize' => 512,
183 'BlockSize' => 512,
184 'secure' => true,
185 ),
186 'haval128,3' => array(
187 'HashSize' => 128,
188 'BlockSize' => 1024,
189 'secure' => false,
190 ),
191 'haval160,3' => array(
192 'HashSize' => 160,
193 'BlockSize' => 1024,
194 'secure' => false,
195 ),
196 'haval192,3' => array(
197 'HashSize' => 192,
198 'BlockSize' => 1024,
199 'secure' => false,
200 ),
201 'haval224,3' => array(
202 'HashSize' => 224,
203 'BlockSize' => 1024,
204 'secure' => false,
205 ),
206 'haval256,3' => array(
207 'HashSize' => 256,
208 'BlockSize' => 1024,
209 'secure' => false,
210 ),
211 'haval128,4' => array(
212 'HashSize' => 128,
213 'BlockSize' => 1024,
214 'secure' => false,
215 ),
216 'haval160,4' => array(
217 'HashSize' => 160,
218 'BlockSize' => 1024,
219 'secure' => false,
220 ),
221 'haval192,4' => array(
222 'HashSize' => 192,
223 'BlockSize' => 1024,
224 'secure' => false,
225 ),
226 'haval224,4' => array(
227 'HashSize' => 224,
228 'BlockSize' => 1024,
229 'secure' => false,
230 ),
231 'haval256,4' => array(
232 'HashSize' => 256,
233 'BlockSize' => 1024,
234 'secure' => false,
235 ),
236 'haval128,5' => array(
237 'HashSize' => 128,
238 'BlockSize' => 1024,
239 'secure' => false,
240 ),
241 'haval160,5' => array(
242 'HashSize' => 160,
243 'BlockSize' => 1024,
244 'secure' => false,
245 ),
246 'haval192,5' => array(
247 'HashSize' => 192,
248 'BlockSize' => 1024,
249 'secure' => false,
250 ),
251 'haval224,5' => array(
252 'HashSize' => 224,
253 'BlockSize' => 1024,
254 'secure' => false,
255 ),
256 'haval256,5' => array(
257 'HashSize' => 256,
258 'BlockSize' => 1024,
259 'secure' => false,
260 ),
261 'joaat' => array(
262 'HashSize' => 32,
263 'BlockSize' => 64,
264 'secure' => false,
265 ),
266 'fnv132' => array(
267 'HashSize' => 32,
268 'BlockSize' => 32,
269 'secure' => false,
270 ),
271 'fnv164' => array(
272 'HashSize' => 64,
273 'BlockSize' => 64,
274 'secure' => false,
275 ),
276 );
277
278 /**
279 * Get the block size of the specified function in bytes
280 *
281 * @param string $hash The hash function to look up
282 *
283 * @return int The number of bytes in the block function
284 */
285 public static function getBlockSize($hash) {
286 return static::getBlockSizeInBits($hash) / 8;
287 }
288
289 /**
290 * Get the block size of the specified function in bits
291 *
292 * @param string $hash The hash function to look up
293 *
294 * @return int The number of bits in the block function
295 */
296 public static function getBlockSizeInBits($hash) {
297 if (isset(static::$hashInfo[$hash]['BlockSize'])) {
298 return static::$hashInfo[$hash]['BlockSize'];
299 }
300 return 0;
301 }
302
303 /**
304 * Get the output size of the specified function in bytes
305 *
306 * @param string $hash The hash function to look up
307 *
308 * @return int The number of bytes outputted by the hash function
309 */
310 public static function getHashSize($hash) {
311 return static::getHashSizeInBits($hash) / 8;
312 }
313
314 /**
315 * Get the output size of the specified function in bits
316 *
317 * @param string $hash The hash function to look up
318 *
319 * @return int The number of bits outputted by the hash function
320 */
321 public static function getHashSizeInBits($hash) {
322 if (isset(static::$hashInfo[$hash]['HashSize'])) {
323 return static::$hashInfo[$hash]['HashSize'];
324 }
325 return 0;
326 }
327
328 /**
329 * Check to see if the hash function specified is available
330 *
331 * @param string $hash The hash function to look up
332 *
333 * @return boolean If the hash function is available in this version of PHP
334 */
335 public static function isAvailable($hash) {
336 return in_array($hash, hash_algos());
337 }
338
339 /**
340 * Check to see if the specified hash function is secure enough for
341 * cryptographic uses
342 *
343 * The "secure" indicates the strength of the hash and whether or not any known
344 * cryptographic attacks exist for the hash function. This will only apply when
345 * using the hash functions for situations that require cryptographic strength
346 * such as message signing. For other uses the insecure ones can have valid
347 * uses.
348 *
349 * @param string $hash The hash function to look up
350 *
351 * @return bolean If the function is secure
352 */
353 public static function isSecure($hash) {
354 if (isset(static::$hashInfo[$hash])) {
355 return static::$hashInfo[$hash]['secure'];
356 }
357 return false;
358 }
359
360}