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