Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * Core Security
4
 *
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://cakephp.org CakePHP(tm) Project
14
 * @package       Cake.Utility
15
 * @since         CakePHP(tm) v .0.10.0.1233
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('String', 'Utility');
20
 
21
/**
22
 * Security Library contains utility methods related to security
23
 *
24
 * @package       Cake.Utility
25
 */
26
class Security {
27
 
28
/**
29
 * Default hash method
30
 *
31
 * @var string
32
 */
33
	public static $hashType = null;
34
 
35
/**
36
 * Default cost
37
 *
38
 * @var string
39
 */
40
	public static $hashCost = '10';
41
 
42
/**
43
 * Get allowed minutes of inactivity based on security level.
44
 *
45
 * @deprecated Exists for backwards compatibility only, not used by the core
46
 * @return integer Allowed inactivity in minutes
47
 */
48
	public static function inactiveMins() {
49
		switch (Configure::read('Security.level')) {
50
			case 'high':
51
				return 10;
52
			case 'medium':
53
				return 100;
54
			case 'low':
55
			default:
56
				return 300;
57
		}
58
	}
59
 
60
/**
61
 * Generate authorization hash.
62
 *
63
 * @return string Hash
64
 */
65
	public static function generateAuthKey() {
66
		return Security::hash(String::uuid());
67
	}
68
 
69
/**
70
 * Validate authorization hash.
71
 *
72
 * @param string $authKey Authorization hash
73
 * @return boolean Success
74
 */
75
	public static function validateAuthKey($authKey) {
76
		return true;
77
	}
78
 
79
/**
80
 * Create a hash from string using given method or fallback on next available method.
81
 *
82
 * #### Using Blowfish
83
 *
84
 * - Creating Hashes: *Do not supply a salt*. Cake handles salt creation for
85
 * you ensuring that each hashed password will have a *unique* salt.
86
 * - Comparing Hashes: Simply pass the originally hashed password as the salt.
87
 * The salt is prepended to the hash and php handles the parsing automagically.
88
 * For convenience the BlowfishAuthenticate adapter is available for use with
89
 * the AuthComponent.
90
 * - Do NOT use a constant salt for blowfish!
91
 *
92
 * Creating a blowfish/bcrypt hash:
93
 *
94
 * {{{
95
 * 	$hash = Security::hash($password, 'blowfish');
96
 * }}}
97
 *
98
 * @param string $string String to hash
99
 * @param string $type Method to use (sha1/sha256/md5/blowfish)
100
 * @param mixed $salt If true, automatically prepends the application's salt
101
 *     value to $string (Security.salt). If you are using blowfish the salt
102
 *     must be false or a previously generated salt.
103
 * @return string Hash
104
 * @link http://book.cakephp.org/2.0/en/core-utility-libraries/security.html#Security::hash
105
 */
106
	public static function hash($string, $type = null, $salt = false) {
107
		if (empty($type)) {
108
			$type = self::$hashType;
109
		}
110
		$type = strtolower($type);
111
 
112
		if ($type === 'blowfish') {
113
			return self::_crypt($string, $salt);
114
		}
115
		if ($salt) {
116
			if (!is_string($salt)) {
117
				$salt = Configure::read('Security.salt');
118
			}
119
			$string = $salt . $string;
120
		}
121
 
122
		if (!$type || $type === 'sha1') {
123
			if (function_exists('sha1')) {
124
				return sha1($string);
125
			}
126
			$type = 'sha256';
127
		}
128
 
129
		if ($type === 'sha256' && function_exists('mhash')) {
130
			return bin2hex(mhash(MHASH_SHA256, $string));
131
		}
132
 
133
		if (function_exists('hash')) {
134
			return hash($type, $string);
135
		}
136
		return md5($string);
137
	}
138
 
139
/**
140
 * Sets the default hash method for the Security object. This affects all objects using
141
 * Security::hash().
142
 *
143
 * @param string $hash Method to use (sha1/sha256/md5/blowfish)
144
 * @return void
145
 * @see Security::hash()
146
 */
147
	public static function setHash($hash) {
148
		self::$hashType = $hash;
149
	}
150
 
151
/**
152
 * Sets the cost for they blowfish hash method.
153
 *
154
 * @param integer $cost Valid values are 4-31
155
 * @return void
156
 */
157
	public static function setCost($cost) {
158
		if ($cost < 4 || $cost > 31) {
159
			trigger_error(__d(
160
				'cake_dev',
161
				'Invalid value, cost must be between %s and %s',
162
				array(4, 31)
163
			), E_USER_WARNING);
164
			return null;
165
		}
166
		self::$hashCost = $cost;
167
	}
168
 
169
/**
170
 * Runs $text through a XOR cipher.
171
 *
172
 * *Note* This is not a cryptographically strong method and should not be used
173
 * for sensitive data. Additionally this method does *not* work in environments
174
 * where suhosin is enabled.
175
 *
176
 * Instead you should use Security::rijndael() when you need strong
177
 * encryption.
178
 *
179
 * @param string $text Encrypted string to decrypt, normal string to encrypt
180
 * @param string $key Key to use
181
 * @return string Encrypted/Decrypted string
182
 * @deprecated Will be removed in 3.0.
183
 */
184
	public static function cipher($text, $key) {
185
		if (empty($key)) {
186
			trigger_error(__d('cake_dev', 'You cannot use an empty key for %s', 'Security::cipher()'), E_USER_WARNING);
187
			return '';
188
		}
189
 
190
		srand(Configure::read('Security.cipherSeed'));
191
		$out = '';
192
		$keyLength = strlen($key);
193
		for ($i = 0, $textLength = strlen($text); $i < $textLength; $i++) {
194
			$j = ord(substr($key, $i % $keyLength, 1));
195
			while ($j--) {
196
				rand(0, 255);
197
			}
198
			$mask = rand(0, 255);
199
			$out .= chr(ord(substr($text, $i, 1)) ^ $mask);
200
		}
201
		srand();
202
		return $out;
203
	}
204
 
205
/**
206
 * Encrypts/Decrypts a text using the given key using rijndael method.
207
 *
208
 * Prior to 2.3.1, a fixed initialization vector was used. This was not
209
 * secure. This method now uses a random iv, and will silently upgrade values when
210
 * they are re-encrypted.
211
 *
212
 * @param string $text Encrypted string to decrypt, normal string to encrypt
213
 * @param string $key Key to use as the encryption key for encrypted data.
214
 * @param string $operation Operation to perform, encrypt or decrypt
215
 * @return string Encrypted/Decrypted string
216
 */
217
	public static function rijndael($text, $key, $operation) {
218
		if (empty($key)) {
219
			trigger_error(__d('cake_dev', 'You cannot use an empty key for %s', 'Security::rijndael()'), E_USER_WARNING);
220
			return '';
221
		}
222
		if (empty($operation) || !in_array($operation, array('encrypt', 'decrypt'))) {
223
			trigger_error(__d('cake_dev', 'You must specify the operation for Security::rijndael(), either encrypt or decrypt'), E_USER_WARNING);
224
			return '';
225
		}
226
		if (strlen($key) < 32) {
227
			trigger_error(__d('cake_dev', 'You must use a key larger than 32 bytes for Security::rijndael()'), E_USER_WARNING);
228
			return '';
229
		}
230
		$algorithm = MCRYPT_RIJNDAEL_256;
231
		$mode = MCRYPT_MODE_CBC;
232
		$ivSize = mcrypt_get_iv_size($algorithm, $mode);
233
 
234
		$cryptKey = substr($key, 0, 32);
235
 
236
		if ($operation === 'encrypt') {
237
			$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
238
			return $iv . '$$' . mcrypt_encrypt($algorithm, $cryptKey, $text, $mode, $iv);
239
		}
240
		// Backwards compatible decrypt with fixed iv
241
		if (substr($text, $ivSize, 2) !== '$$') {
242
			$iv = substr($key, strlen($key) - 32, 32);
243
			return rtrim(mcrypt_decrypt($algorithm, $cryptKey, $text, $mode, $iv), "\0");
244
		}
245
		$iv = substr($text, 0, $ivSize);
246
		$text = substr($text, $ivSize + 2);
247
		return rtrim(mcrypt_decrypt($algorithm, $cryptKey, $text, $mode, $iv), "\0");
248
	}
249
 
250
/**
251
 * Generates a pseudo random salt suitable for use with php's crypt() function.
252
 * The salt length should not exceed 27. The salt will be composed of
253
 * [./0-9A-Za-z]{$length}.
254
 *
255
 * @param integer $length The length of the returned salt
256
 * @return string The generated salt
257
 */
258
	protected static function _salt($length = 22) {
259
		$salt = str_replace(
260
			array('+', '='),
261
			'.',
262
			base64_encode(sha1(uniqid(Configure::read('Security.salt'), true), true))
263
		);
264
		return substr($salt, 0, $length);
265
	}
266
 
267
/**
268
 * One way encryption using php's crypt() function. To use blowfish hashing see ``Security::hash()``
269
 *
270
 * @param string $password The string to be encrypted.
271
 * @param mixed $salt false to generate a new salt or an existing salt.
272
 * @return string The hashed string or an empty string on error.
273
 */
274
	protected static function _crypt($password, $salt = false) {
275
		if ($salt === false) {
276
			$salt = self::_salt(22);
277
			$salt = vsprintf('$2a$%02d$%s', array(self::$hashCost, $salt));
278
		}
279
 
280
		if ($salt === true || strpos($salt, '$2a$') !== 0 || strlen($salt) < 29) {
281
			trigger_error(__d(
282
				'cake_dev',
283
				'Invalid salt: %s for %s Please visit http://www.php.net/crypt and read the appropriate section for building %s salts.',
284
				array($salt, 'blowfish', 'blowfish')
285
			), E_USER_WARNING);
286
			return '';
287
		}
288
		return crypt($password, $salt);
289
	}
290
 
291
}