File indexing completed on 2024-05-12 10:07:30
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Crypt 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** 0023 * @category Zend 0024 * @package Zend_Crypt 0025 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0026 * @license http://framework.zend.com/license/new-bsd New BSD License 0027 */ 0028 class Zend_Crypt 0029 { 0030 0031 const TYPE_OPENSSL = 'openssl'; 0032 const TYPE_HASH = 'hash'; 0033 const TYPE_MHASH = 'mhash'; 0034 0035 protected static $_type = null; 0036 0037 /** 0038 * @var array 0039 */ 0040 protected static $_supportedAlgosOpenssl = array( 0041 'md2', 0042 'md4', 0043 'mdc2', 0044 'rmd160', 0045 'sha', 0046 'sha1', 0047 'sha224', 0048 'sha256', 0049 'sha384', 0050 'sha512' 0051 ); 0052 0053 /** 0054 * @var array 0055 */ 0056 protected static $_supportedAlgosMhash = array( 0057 'adler32', 0058 'crc32', 0059 'crc32b', 0060 'gost', 0061 'haval128', 0062 'haval160', 0063 'haval192', 0064 'haval256', 0065 'md4', 0066 'md5', 0067 'ripemd160', 0068 'sha1', 0069 'sha256', 0070 'tiger', 0071 'tiger128', 0072 'tiger160' 0073 ); 0074 0075 /** 0076 * @param string $algorithm 0077 * @param string $data 0078 * @param bool $binaryOutput 0079 * @return unknown 0080 */ 0081 public static function hash($algorithm, $data, $binaryOutput = false) 0082 { 0083 $algorithm = strtolower($algorithm); 0084 if (function_exists($algorithm)) { 0085 return $algorithm($data, $binaryOutput); 0086 } 0087 self::_detectHashSupport($algorithm); 0088 $supportedMethod = '_digest' . ucfirst(self::$_type); 0089 $result = self::$supportedMethod($algorithm, $data, $binaryOutput); 0090 return $result; 0091 } 0092 0093 /** 0094 * @param string $algorithm 0095 * @throws Zend_Crypt_Exception 0096 */ 0097 protected static function _detectHashSupport($algorithm) 0098 { 0099 if (function_exists('hash')) { 0100 self::$_type = self::TYPE_HASH; 0101 if (in_array($algorithm, hash_algos())) { 0102 return; 0103 } 0104 } 0105 if (function_exists('mhash')) { 0106 self::$_type = self::TYPE_MHASH; 0107 if (in_array($algorithm, self::$_supportedAlgosMhash)) { 0108 return; 0109 } 0110 } 0111 if (function_exists('openssl_digest')) { 0112 if ($algorithm == 'ripemd160') { 0113 $algorithm = 'rmd160'; 0114 } 0115 self::$_type = self::TYPE_OPENSSL; 0116 if (in_array($algorithm, self::$_supportedAlgosOpenssl)) { 0117 return; 0118 } 0119 } 0120 /** 0121 * @see Zend_Crypt_Exception 0122 */ 0123 // require_once 'Zend/Crypt/Exception.php'; 0124 throw new Zend_Crypt_Exception('\'' . $algorithm . '\' is not supported by any available extension or native function'); 0125 } 0126 0127 /** 0128 * @param string $algorithm 0129 * @param string $data 0130 * @param bool $binaryOutput 0131 * @return string 0132 */ 0133 protected static function _digestHash($algorithm, $data, $binaryOutput) 0134 { 0135 return hash($algorithm, $data, $binaryOutput); 0136 } 0137 0138 /** 0139 * @param string $algorithm 0140 * @param string $data 0141 * @param bool $binaryOutput 0142 * @return string 0143 */ 0144 protected static function _digestMhash($algorithm, $data, $binaryOutput) 0145 { 0146 $constant = constant('MHASH_' . strtoupper($algorithm)); 0147 $binary = mhash($constant, $data); 0148 if ($binaryOutput) { 0149 return $binary; 0150 } 0151 return bin2hex($binary); 0152 } 0153 0154 /** 0155 * @param string $algorithm 0156 * @param string $data 0157 * @param bool $binaryOutput 0158 * @return string 0159 */ 0160 protected static function _digestOpenssl($algorithm, $data, $binaryOutput) 0161 { 0162 if ($algorithm == 'ripemd160') { 0163 $algorithm = 'rmd160'; 0164 } 0165 return openssl_digest($data, $algorithm, $binaryOutput); 0166 } 0167 0168 }