Update code_sniffer build.xml file to be executable on our system
[phpbb.git] / phpBB / includes / libraries / sftp / hash.php
blobeb048f5d285e622d7ea4f84bdedaa8d3ea2ba797
1 <?php
2 /**
4 * @package sftp
5 * @version $Id$
6 * @copyright (c) 2006 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 /**
20 * Code from http://phpseclib.sourceforge.net/
22 * Modified by phpBB Group to meet our coding standards
23 * and being able to integrate into phpBB
25 * Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions.
27 * Copyright 2007-2009 TerraFrost <terrafrost@php.net>
28 * Copyright 2009+ phpBB
30 * @package sftp
31 * @author TerraFrost <terrafrost@php.net>
34 /**#@+
35 * @access private
36 * @see hash::__construct()
38 /**
39 * Toggles the internal implementation
41 define('CRYPT_HASH_MODE_INTERNAL', 1);
42 /**
43 * Toggles the mhash() implementation, which has been deprecated on PHP 5.3.0+.
45 define('CRYPT_HASH_MODE_MHASH', 2);
46 /**
47 * Toggles the hash() implementation, which works on PHP 5.1.2+.
49 define('CRYPT_HASH_MODE_HASH', 3);
50 /**#@-*/
52 /**
53 * Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions.
55 * @author Jim Wigginton <terrafrost@php.net>
56 * @version 0.1.0
57 * @access public
58 * @package hash
60 class hash
62 /**
63 * Byte-length of compression blocks / key (Internal HMAC)
65 * @see hash::set_algorithm()
66 * @var Integer
67 * @access private
69 var $b;
71 /**
72 * Byte-length of hash output (Internal HMAC)
74 * @see hash::set_hash()
75 * @var Integer
76 * @access private
78 var $l;
80 /**
81 * Hash Algorithm
83 * @see hash::set_hash()
84 * @var String
85 * @access private
87 var $hash;
89 /**
90 * Key
92 * @see hash::setKey()
93 * @var String
94 * @access private
96 var $key = '';
98 /**
99 * Outer XOR (Internal HMAC)
101 * @see hash::setKey()
102 * @var String
103 * @access private
105 var $opad;
108 * Inner XOR (Internal HMAC)
110 * @see hash::setKey()
111 * @var String
112 * @access private
114 var $ipad;
117 * Default Constructor.
119 * @param optional String $hash
120 * @return hash
121 * @access public
123 function __construct($hash = 'sha1')
125 if ( !defined('CRYPT_HASH_MODE') )
127 switch (true)
129 case extension_loaded('hash'):
130 define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_HASH);
131 break;
132 case extension_loaded('mhash'):
133 define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_MHASH);
134 break;
135 default:
136 define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_INTERNAL);
140 $this->set_hash($hash);
144 * Sets the key for HMACs
146 * Keys can be of any length.
148 * @access public
149 * @param String $key
151 function set_key($key)
153 $this->key = $key;
157 * Sets the hash function.
159 * @access public
160 * @param String $hash
162 function set_hash($hash)
164 switch ($hash)
166 case 'md5-96':
167 case 'sha1-96':
168 $this->l = 12; // 96 / 8 = 12
169 break;
170 case 'md5':
171 $this->l = 16;
172 break;
173 case 'sha1':
174 $this->l = 20;
177 switch (CRYPT_HASH_MODE)
179 case CRYPT_HASH_MODE_MHASH:
180 switch ($hash)
182 case 'md5':
183 case 'md5-96':
184 $this->hash = MHASH_MD5;
185 break;
186 case 'sha1':
187 case 'sha1-96':
188 default:
189 $this->hash = MHASH_SHA1;
191 return;
192 case CRYPT_HASH_MODE_HASH:
193 switch ($hash)
195 case 'md5':
196 case 'md5-96':
197 $this->hash = 'md5';
198 return;
199 case 'sha1':
200 case 'sha1-96':
201 default:
202 $this->hash = 'sha1';
204 return;
207 switch ($hash)
209 case 'md5':
210 case 'md5-96':
211 $this->b = 64;
212 $this->hash = 'md5';
213 break;
214 case 'sha1':
215 case 'sha1-96':
216 default:
217 $this->b = 64;
218 $this->hash = 'sha1';
221 $this->ipad = str_repeat(chr(0x36), $this->b);
222 $this->opad = str_repeat(chr(0x5C), $this->b);
226 * Compute the HMAC.
228 * @access public
229 * @param String $text
231 function create($text)
233 if (!empty($this->key))
235 switch (CRYPT_HASH_MODE)
237 case CRYPT_HASH_MODE_MHASH:
238 $output = mhash($this->hash, $text, $this->key);
239 break;
240 case CRYPT_HASH_MODE_HASH:
241 $output = hash_hmac($this->hash, $text, $this->key, true);
242 break;
243 case CRYPT_HASH_MODE_INTERNAL:
244 $hash = $this->hash;
245 /* "Applications that use keys longer than B bytes will first hash the key using H and then use the
246 resultant L byte string as the actual key to HMAC."
248 -- http://tools.ietf.org/html/rfc2104#section-2 */
249 $key = strlen($this->key) > $this->b ? $hash($this->key) : $this->key;
251 $key = str_pad($key, $this->b, chr(0));// step 1
252 $temp = $this->ipad ^ $key; // step 2
253 $temp .= $text; // step 3
254 $temp = pack('H*', $hash($temp)); // step 4
255 $output = $this->opad ^ $key; // step 5
256 $output.= $temp; // step 6
257 $output = pack('H*', $hash($output)); // step 7
260 else
262 switch (CRYPT_HASH_MODE)
264 case CRYPT_HASH_MODE_MHASH:
265 $output = mhash($this->hash, $text);
266 break;
267 case CRYPT_HASH_MODE_MHASH:
268 $output = hash($this->hash, $text, true);
269 break;
270 case CRYPT_HASH_MODE_INTERNAL:
271 $hash = $this->hash;
272 $output = pack('H*', $hash($output));
276 return substr($output, 0, $this->l);