3 * A hash utility data mapper class
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
12 * @category PHPCryptLib
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@@
19 namespace CryptLib\Hash
;
22 * A hash utility data mapper class
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
31 * @category PHPCryptLib
33 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
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
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
48 * @var array An array of information about each supported hash function
50 protected static $hashInfo = array(
101 'ripemd256' => array(
106 'ripemd320' => array(
111 'whirlpool' => array(
116 'tiger128,3' => array(
121 'tiger160,3' => array(
126 'tiger192,3' => array(
131 'tiger128,4' => array(
136 'tiger160,4' => array(
141 'tiger192,4' => array(
151 'snefru256' => array(
186 'haval128,3' => array(
191 'haval160,3' => array(
196 'haval192,3' => array(
201 'haval224,3' => array(
206 'haval256,3' => array(
211 'haval128,4' => array(
216 'haval160,4' => array(
221 'haval192,4' => array(
226 'haval224,4' => array(
231 'haval256,4' => array(
236 'haval128,5' => array(
241 'haval160,5' => array(
246 'haval192,5' => array(
251 'haval224,5' => array(
256 'haval256,5' => array(
279 * Get the block size of the specified function in bytes
281 * @param string $hash The hash function to look up
283 * @return int The number of bytes in the block function
285 public static function getBlockSize($hash) {
286 return static::getBlockSizeInBits($hash) / 8;
290 * Get the block size of the specified function in bits
292 * @param string $hash The hash function to look up
294 * @return int The number of bits in the block function
296 public static function getBlockSizeInBits($hash) {
297 if (isset(static::$hashInfo[$hash]['BlockSize'])) {
298 return static::$hashInfo[$hash]['BlockSize'];
304 * Get the output size of the specified function in bytes
306 * @param string $hash The hash function to look up
308 * @return int The number of bytes outputted by the hash function
310 public static function getHashSize($hash) {
311 return static::getHashSizeInBits($hash) / 8;
315 * Get the output size of the specified function in bits
317 * @param string $hash The hash function to look up
319 * @return int The number of bits outputted by the hash function
321 public static function getHashSizeInBits($hash) {
322 if (isset(static::$hashInfo[$hash]['HashSize'])) {
323 return static::$hashInfo[$hash]['HashSize'];
329 * Check to see if the hash function specified is available
331 * @param string $hash The hash function to look up
333 * @return boolean If the hash function is available in this version of PHP
335 public static function isAvailable($hash) {
336 return in_array($hash, hash_algos());
340 * Check to see if the specified hash function is secure enough for
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
349 * @param string $hash The hash function to look up
351 * @return bolean If the function is secure
353 public static function isSecure($hash) {
354 if (isset(static::$hashInfo[$hash])) {
355 return static::$hashInfo[$hash]['secure'];