00001 <?php 00028 class Zend_Crypt 00029 { 00030 00031 const TYPE_OPENSSL = 'openssl'; 00032 const TYPE_HASH = 'hash'; 00033 const TYPE_MHASH = 'mhash'; 00034 00035 protected static $_type = null; 00036 00040 protected static $_supportedAlgosOpenssl = array( 00041 'md2', 00042 'md4', 00043 'mdc2', 00044 'rmd160', 00045 'sha', 00046 'sha1', 00047 'sha224', 00048 'sha256', 00049 'sha384', 00050 'sha512' 00051 ); 00052 00056 protected static $_supportedAlgosMhash = array( 00057 'adler32', 00058 'crc32', 00059 'crc32b', 00060 'gost', 00061 'haval128', 00062 'haval160', 00063 'haval192', 00064 'haval256', 00065 'md4', 00066 'md5', 00067 'ripemd160', 00068 'sha1', 00069 'sha256', 00070 'tiger', 00071 'tiger128', 00072 'tiger160' 00073 ); 00074 00081 public static function hash($algorithm, $data, $binaryOutput = false) 00082 { 00083 $algorithm = strtolower($algorithm); 00084 if (function_exists($algorithm)) { 00085 return $algorithm($data, $binaryOutput); 00086 } 00087 self::_detectHashSupport($algorithm); 00088 $supportedMethod = '_digest' . ucfirst(self::$_type); 00089 $result = self::$supportedMethod($algorithm, $data, $binaryOutput); 00090 } 00091 00096 protected static function _detectHashSupport($algorithm) 00097 { 00098 if (function_exists('hash')) { 00099 self::$_type = self::TYPE_HASH; 00100 if (in_array($algorithm, hash_algos())) { 00101 return; 00102 } 00103 } 00104 if (function_exists('mhash')) { 00105 self::$_type = self::TYPE_MHASH; 00106 if (in_array($algorithm, self::$_supportedAlgosMhash)) { 00107 return; 00108 } 00109 } 00110 if (function_exists('openssl_digest')) { 00111 if ($algorithm == 'ripemd160') { 00112 $algorithm = 'rmd160'; 00113 } 00114 self::$_type = self::TYPE_OPENSSL; 00115 if (in_array($algorithm, self::$_supportedAlgosOpenssl)) { 00116 return; 00117 } 00118 } 00122 require_once 'Zend/Crypt/Exception.php'; 00123 throw new Zend_Crypt_Exception('\'' . $algorithm . '\' is not supported by any available extension or native function'); 00124 } 00125 00132 protected static function _digestHash($algorithm, $data, $binaryOutput) 00133 { 00134 return hash($algorithm, $data, $binaryOutput); 00135 } 00136 00143 protected static function _digestMhash($algorithm, $data, $binaryOutput) 00144 { 00145 $constant = constant('MHASH_' . strtoupper($algorithm)); 00146 $binary = mhash($constant, $data); 00147 if ($binaryOutput) { 00148 return $binary; 00149 } 00150 return bin2hex($binary); 00151 } 00152 00159 protected static function _digestOpenssl($algorithm, $data, $binaryOutput) 00160 { 00161 if ($algorithm == 'ripemd160') { 00162 $algorithm = 'rmd160'; 00163 } 00164 return openssl_digest($data, $algorithm, $binaryOutput); 00165 } 00166 00167 }