7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
25 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
26 * @license http://framework.zend.com/license/new-bsd New BSD License
31 const TYPE_OPENSSL
= 'openssl';
32 const TYPE_HASH
= 'hash';
33 const TYPE_MHASH
= 'mhash';
35 protected static $_type = null;
40 protected static $_supportedAlgosOpenssl = array(
56 protected static $_supportedAlgosMhash = array(
76 * @param string $algorithm
78 * @param bool $binaryOutput
81 public static function hash($algorithm, $data, $binaryOutput = false)
83 $algorithm = strtolower($algorithm);
84 if (function_exists($algorithm)) {
85 return $algorithm($data, $binaryOutput);
87 self
::_detectHashSupport($algorithm);
88 $supportedMethod = '_digest' . ucfirst(self
::$_type);
89 $result = self
::$supportedMethod($algorithm, $data, $binaryOutput);
93 * @param string $algorithm
94 * @throws Zend_Crypt_Exception
96 protected static function _detectHashSupport($algorithm)
98 if (function_exists('hash')) {
99 self
::$_type = self
::TYPE_HASH
;
100 if (in_array($algorithm, hash_algos())) {
104 if (function_exists('mhash')) {
105 self
::$_type = self
::TYPE_MHASH
;
106 if (in_array($algorithm, self
::$_supportedAlgosMhash)) {
110 if (function_exists('openssl_digest')) {
111 if ($algorithm == 'ripemd160') {
112 $algorithm = 'rmd160';
114 self
::$_type = self
::TYPE_OPENSSL
;
115 if (in_array($algorithm, self
::$_supportedAlgosOpenssl)) {
120 * @see Zend_Crypt_Exception
122 require_once 'Zend/Crypt/Exception.php';
123 throw new Zend_Crypt_Exception('\'' . $algorithm . '\' is not supported by any available extension or native function');
127 * @param string $algorithm
128 * @param string $data
129 * @param bool $binaryOutput
132 protected static function _digestHash($algorithm, $data, $binaryOutput)
134 return hash($algorithm, $data, $binaryOutput);
138 * @param string $algorithm
139 * @param string $data
140 * @param bool $binaryOutput
143 protected static function _digestMhash($algorithm, $data, $binaryOutput)
145 $constant = constant('MHASH_' . strtoupper($algorithm));
146 $binary = mhash($constant, $data);
150 return bin2hex($binary);
154 * @param string $algorithm
155 * @param string $data
156 * @param bool $binaryOutput
159 protected static function _digestOpenssl($algorithm, $data, $binaryOutput)
161 if ($algorithm == 'ripemd160') {
162 $algorithm = 'rmd160';
164 return openssl_digest($data, $algorithm, $binaryOutput);