|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
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. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Crypt |
|
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 |
|
19 * @version $Id: Crypt.php 23089 2010-10-12 17:05:31Z padraic $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * @category Zend |
|
24 * @package Zend_Crypt |
|
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 |
|
27 */ |
|
28 class Zend_Crypt |
|
29 { |
|
30 |
|
31 const TYPE_OPENSSL = 'openssl'; |
|
32 const TYPE_HASH = 'hash'; |
|
33 const TYPE_MHASH = 'mhash'; |
|
34 |
|
35 protected static $_type = null; |
|
36 |
|
37 /** |
|
38 * @var array |
|
39 */ |
|
40 protected static $_supportedAlgosOpenssl = array( |
|
41 'md2', |
|
42 'md4', |
|
43 'mdc2', |
|
44 'rmd160', |
|
45 'sha', |
|
46 'sha1', |
|
47 'sha224', |
|
48 'sha256', |
|
49 'sha384', |
|
50 'sha512' |
|
51 ); |
|
52 |
|
53 /** |
|
54 * @var array |
|
55 */ |
|
56 protected static $_supportedAlgosMhash = array( |
|
57 'adler32', |
|
58 'crc32', |
|
59 'crc32b', |
|
60 'gost', |
|
61 'haval128', |
|
62 'haval160', |
|
63 'haval192', |
|
64 'haval256', |
|
65 'md4', |
|
66 'md5', |
|
67 'ripemd160', |
|
68 'sha1', |
|
69 'sha256', |
|
70 'tiger', |
|
71 'tiger128', |
|
72 'tiger160' |
|
73 ); |
|
74 |
|
75 /** |
|
76 * @param string $algorithm |
|
77 * @param string $data |
|
78 * @param bool $binaryOutput |
|
79 * @return unknown |
|
80 */ |
|
81 public static function hash($algorithm, $data, $binaryOutput = false) |
|
82 { |
|
83 $algorithm = strtolower($algorithm); |
|
84 if (function_exists($algorithm)) { |
|
85 return $algorithm($data, $binaryOutput); |
|
86 } |
|
87 self::_detectHashSupport($algorithm); |
|
88 $supportedMethod = '_digest' . ucfirst(self::$_type); |
|
89 $result = self::$supportedMethod($algorithm, $data, $binaryOutput); |
|
90 return $result; |
|
91 } |
|
92 |
|
93 /** |
|
94 * @param string $algorithm |
|
95 * @throws Zend_Crypt_Exception |
|
96 */ |
|
97 protected static function _detectHashSupport($algorithm) |
|
98 { |
|
99 if (function_exists('hash')) { |
|
100 self::$_type = self::TYPE_HASH; |
|
101 if (in_array($algorithm, hash_algos())) { |
|
102 return; |
|
103 } |
|
104 } |
|
105 if (function_exists('mhash')) { |
|
106 self::$_type = self::TYPE_MHASH; |
|
107 if (in_array($algorithm, self::$_supportedAlgosMhash)) { |
|
108 return; |
|
109 } |
|
110 } |
|
111 if (function_exists('openssl_digest')) { |
|
112 if ($algorithm == 'ripemd160') { |
|
113 $algorithm = 'rmd160'; |
|
114 } |
|
115 self::$_type = self::TYPE_OPENSSL; |
|
116 if (in_array($algorithm, self::$_supportedAlgosOpenssl)) { |
|
117 return; |
|
118 } |
|
119 } |
|
120 /** |
|
121 * @see Zend_Crypt_Exception |
|
122 */ |
|
123 require_once 'Zend/Crypt/Exception.php'; |
|
124 throw new Zend_Crypt_Exception('\'' . $algorithm . '\' is not supported by any available extension or native function'); |
|
125 } |
|
126 |
|
127 /** |
|
128 * @param string $algorithm |
|
129 * @param string $data |
|
130 * @param bool $binaryOutput |
|
131 * @return string |
|
132 */ |
|
133 protected static function _digestHash($algorithm, $data, $binaryOutput) |
|
134 { |
|
135 return hash($algorithm, $data, $binaryOutput); |
|
136 } |
|
137 |
|
138 /** |
|
139 * @param string $algorithm |
|
140 * @param string $data |
|
141 * @param bool $binaryOutput |
|
142 * @return string |
|
143 */ |
|
144 protected static function _digestMhash($algorithm, $data, $binaryOutput) |
|
145 { |
|
146 $constant = constant('MHASH_' . strtoupper($algorithm)); |
|
147 $binary = mhash($constant, $data); |
|
148 if ($binaryOutput) { |
|
149 return $binary; |
|
150 } |
|
151 return bin2hex($binary); |
|
152 } |
|
153 |
|
154 /** |
|
155 * @param string $algorithm |
|
156 * @param string $data |
|
157 * @param bool $binaryOutput |
|
158 * @return string |
|
159 */ |
|
160 protected static function _digestOpenssl($algorithm, $data, $binaryOutput) |
|
161 { |
|
162 if ($algorithm == 'ripemd160') { |
|
163 $algorithm = 'rmd160'; |
|
164 } |
|
165 return openssl_digest($data, $algorithm, $binaryOutput); |
|
166 } |
|
167 |
|
168 } |