[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Crypt.php
blobe5fdb201b8031fbb45be57cf5323ac19ab1324ca
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
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$
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
28 class Zend_Crypt
31 const TYPE_OPENSSL = 'openssl';
32 const TYPE_HASH = 'hash';
33 const TYPE_MHASH = 'mhash';
35 protected static $_type = null;
37 /**
38 * @var array
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'
53 /**
54 * @var array
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'
75 /**
76 * @param string $algorithm
77 * @param string $data
78 * @param bool $binaryOutput
79 * @return unknown
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);
92 /**
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())) {
101 return;
104 if (function_exists('mhash')) {
105 self::$_type = self::TYPE_MHASH;
106 if (in_array($algorithm, self::$_supportedAlgosMhash)) {
107 return;
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)) {
116 return;
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
130 * @return string
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
141 * @return string
143 protected static function _digestMhash($algorithm, $data, $binaryOutput)
145 $constant = constant('MHASH_' . strtoupper($algorithm));
146 $binary = mhash($constant, $data);
147 if ($binaryOutput) {
148 return $binary;
150 return bin2hex($binary);
154 * @param string $algorithm
155 * @param string $data
156 * @param bool $binaryOutput
157 * @return string
159 protected static function _digestOpenssl($algorithm, $data, $binaryOutput)
161 if ($algorithm == 'ripemd160') {
162 $algorithm = 'rmd160';
164 return openssl_digest($data, $algorithm, $binaryOutput);