3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
14 const OUTPUT_STRING
= false;
15 const OUTPUT_BINARY
= true;
18 * Last algorithm supported
22 protected static $lastAlgorithmSupported;
28 * @throws Exception\InvalidArgumentException
31 public static function compute($hash, $data, $output = self
::OUTPUT_STRING
)
33 if (!$hash ||
($hash !== static::$lastAlgorithmSupported && !static::isSupported($hash))) {
34 throw new Exception\
InvalidArgumentException(
35 'Hash algorithm provided is not supported on this PHP installation'
39 return hash($hash, $data, $output);
43 * Get the output size according to the hash algorithm and the output format
49 public static function getOutputSize($hash, $output = self
::OUTPUT_STRING
)
51 return strlen(static::compute($hash, 'data', $output));
55 * Get the supported algorithm
59 public static function getSupportedAlgorithms()
65 * Is the hash algorithm supported?
67 * @param string $algorithm
70 public static function isSupported($algorithm)
72 if ($algorithm === static::$lastAlgorithmSupported) {
76 if (in_array(strtolower($algorithm), hash_algos(), true)) {
77 static::$lastAlgorithmSupported = $algorithm;
85 * Clear the cache of last algorithm supported
87 public static function clearLastAlgorithmCache()
89 static::$lastAlgorithmSupported = null;