]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - Zend/Crypt.php
Release 6.5.0
[Github/sugarcrm.git] / Zend / Crypt.php
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
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     }
91
92     /**
93      * @param string $algorithm
94      * @throws Zend_Crypt_Exception
95      */
96     protected static function _detectHashSupport($algorithm)
97     {
98         if (function_exists('hash')) {
99             self::$_type = self::TYPE_HASH;
100             if (in_array($algorithm, hash_algos())) {
101                return;
102             }
103         }
104         if (function_exists('mhash')) {
105             self::$_type = self::TYPE_MHASH;
106             if (in_array($algorithm, self::$_supportedAlgosMhash)) {
107                return;
108             }
109         }
110         if (function_exists('openssl_digest')) {
111             if ($algorithm == 'ripemd160') {
112                 $algorithm = 'rmd160';
113             }
114             self::$_type = self::TYPE_OPENSSL;
115             if (in_array($algorithm, self::$_supportedAlgosOpenssl)) {
116                return;
117             }
118         }
119         /**
120          * @see Zend_Crypt_Exception
121          */
122         require_once 'Zend/Crypt/Exception.php';
123         throw new Zend_Crypt_Exception('\'' . $algorithm . '\' is not supported by any available extension or native function');
124     }
125
126     /**
127      * @param string $algorithm
128      * @param string $data
129      * @param bool $binaryOutput
130      * @return string
131      */
132     protected static function _digestHash($algorithm, $data, $binaryOutput)
133     {
134         return hash($algorithm, $data, $binaryOutput);
135     }
136
137     /**
138      * @param string $algorithm
139      * @param string $data
140      * @param bool $binaryOutput
141      * @return string
142      */
143     protected static function _digestMhash($algorithm, $data, $binaryOutput)
144     {
145         $constant = constant('MHASH_' . strtoupper($algorithm));
146         $binary = mhash($constant, $data);
147         if ($binaryOutput) {
148             return $binary;
149         }
150         return bin2hex($binary);
151     }
152
153     /**
154      * @param string $algorithm
155      * @param string $data
156      * @param bool $binaryOutput
157      * @return string
158      */
159     protected static function _digestOpenssl($algorithm, $data, $binaryOutput)
160     {
161         if ($algorithm == 'ripemd160') {
162             $algorithm = 'rmd160';
163         }
164         return openssl_digest($data, $algorithm, $binaryOutput);
165     }
166
167 }