2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-05-29 15:20:28 +00:00
|
|
|
* Core Security
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Utility
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v .0.10.0.1233
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2015-01-05 00:00:57 +00:00
|
|
|
App::uses('CakeText', 'Utility');
|
2010-12-03 23:07:21 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 20:37:55 +00:00
|
|
|
* Security Library contains utility methods related to security
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Utility
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-24 01:28:54 +00:00
|
|
|
class Security {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Default hash method
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-24 01:28:54 +00:00
|
|
|
public static $hashType = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-07-21 16:48:14 +00:00
|
|
|
/**
|
|
|
|
* Default cost
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public static $hashCost = '10';
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-07-26 10:46:07 +00:00
|
|
|
* Get allowed minutes of inactivity based on security level.
|
|
|
|
*
|
2014-09-02 15:03:22 +00:00
|
|
|
* @deprecated 3.0.0 Exists for backwards compatibility only, not used by the core
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return int Allowed inactivity in minutes
|
2009-07-26 10:46:07 +00:00
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function inactiveMins() {
|
2008-10-23 00:10:44 +00:00
|
|
|
switch (Configure::read('Security.level')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'high':
|
|
|
|
return 10;
|
|
|
|
case 'medium':
|
|
|
|
return 100;
|
|
|
|
case 'low':
|
|
|
|
default:
|
|
|
|
return 300;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-07-26 10:46:07 +00:00
|
|
|
* Generate authorization hash.
|
|
|
|
*
|
|
|
|
* @return string Hash
|
2016-02-22 05:16:15 +00:00
|
|
|
* @deprecated 2.8.1 This method was removed in 3.0.0
|
2009-07-26 10:46:07 +00:00
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function generateAuthKey() {
|
2015-01-05 00:00:57 +00:00
|
|
|
return Security::hash(CakeText::uuid());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Validate authorization hash.
|
|
|
|
*
|
|
|
|
* @param string $authKey Authorization hash
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2016-02-22 05:16:15 +00:00
|
|
|
* @deprecated 2.8.1 This method was removed in 3.0.0
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public static function validateAuthKey($authKey) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-11-11 21:25:08 +00:00
|
|
|
* Create a hash from string using given method or fallback on next available method.
|
|
|
|
*
|
|
|
|
* #### Using Blowfish
|
|
|
|
*
|
|
|
|
* - Creating Hashes: *Do not supply a salt*. Cake handles salt creation for
|
|
|
|
* you ensuring that each hashed password will have a *unique* salt.
|
|
|
|
* - Comparing Hashes: Simply pass the originally hashed password as the salt.
|
|
|
|
* The salt is prepended to the hash and php handles the parsing automagically.
|
2014-02-13 12:24:53 +00:00
|
|
|
* For convenience the `BlowfishPasswordHasher` class is available for use with
|
2012-11-11 21:25:08 +00:00
|
|
|
* the AuthComponent.
|
|
|
|
* - Do NOT use a constant salt for blowfish!
|
|
|
|
*
|
|
|
|
* Creating a blowfish/bcrypt hash:
|
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2016-02-22 05:16:15 +00:00
|
|
|
* $hash = Security::hash($password, 'blowfish');
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $string String to hash
|
2012-11-11 21:25:08 +00:00
|
|
|
* @param string $type Method to use (sha1/sha256/md5/blowfish)
|
2013-08-12 10:52:34 +00:00
|
|
|
* @param mixed $salt If true, automatically prepends the application's salt
|
2012-07-21 16:48:14 +00:00
|
|
|
* value to $string (Security.salt). If you are using blowfish the salt
|
|
|
|
* must be false or a previously generated salt.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Hash
|
2013-11-17 02:40:39 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/security.html#Security::hash
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function hash($string, $type = null, $salt = false) {
|
2012-07-21 16:48:14 +00:00
|
|
|
if (empty($type)) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$type = static::$hashType;
|
2012-07-21 16:48:14 +00:00
|
|
|
}
|
|
|
|
$type = strtolower($type);
|
|
|
|
|
|
|
|
if ($type === 'blowfish') {
|
2015-07-21 08:22:53 +00:00
|
|
|
return static::_crypt($string, $salt);
|
2012-07-21 16:48:14 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($salt) {
|
2012-11-01 13:15:52 +00:00
|
|
|
if (!is_string($salt)) {
|
|
|
|
$salt = Configure::read('Security.salt');
|
2008-10-02 00:03:52 +00:00
|
|
|
}
|
2012-11-01 13:15:52 +00:00
|
|
|
$string = $salt . $string;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-08-02 14:44:42 +00:00
|
|
|
|
2013-02-12 02:38:08 +00:00
|
|
|
if (!$type || $type === 'sha1') {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (function_exists('sha1')) {
|
2012-11-01 13:15:52 +00:00
|
|
|
return sha1($string);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-08-02 14:44:42 +00:00
|
|
|
$type = 'sha256';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($type === 'sha256' && function_exists('mhash')) {
|
2008-08-02 14:44:42 +00:00
|
|
|
return bin2hex(mhash(MHASH_SHA256, $string));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-07-07 17:13:09 +00:00
|
|
|
if (function_exists('hash')) {
|
2008-08-02 14:44:42 +00:00
|
|
|
return hash($type, $string);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-08-02 14:44:42 +00:00
|
|
|
return md5($string);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Sets the default hash method for the Security object. This affects all objects using
|
2008-05-30 11:40:08 +00:00
|
|
|
* Security::hash().
|
|
|
|
*
|
2012-07-21 16:48:14 +00:00
|
|
|
* @param string $hash Method to use (sha1/sha256/md5/blowfish)
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @see Security::hash()
|
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function setHash($hash) {
|
2015-07-21 08:22:53 +00:00
|
|
|
static::$hashType = $hash;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-07-21 16:48:14 +00:00
|
|
|
/**
|
|
|
|
* Sets the cost for they blowfish hash method.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $cost Valid values are 4-31
|
2012-07-21 16:48:14 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function setCost($cost) {
|
2012-11-01 13:15:52 +00:00
|
|
|
if ($cost < 4 || $cost > 31) {
|
|
|
|
trigger_error(__d(
|
|
|
|
'cake_dev',
|
|
|
|
'Invalid value, cost must be between %s and %s',
|
|
|
|
array(4, 31)
|
|
|
|
), E_USER_WARNING);
|
|
|
|
return null;
|
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
static::$hashCost = $cost;
|
2012-07-21 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 05:14:44 +00:00
|
|
|
/**
|
|
|
|
* Get random bytes from a secure source.
|
|
|
|
*
|
|
|
|
* This method will fall back to an insecure source an trigger a warning
|
|
|
|
* if it cannot find a secure source of random data.
|
|
|
|
*
|
|
|
|
* @param int $length The number of bytes you want.
|
|
|
|
* @return string Random bytes in binary.
|
|
|
|
*/
|
|
|
|
public static function randomBytes($length) {
|
|
|
|
if (function_exists('random_bytes')) {
|
|
|
|
return random_bytes($length);
|
|
|
|
}
|
|
|
|
if (function_exists('openssl_random_pseudo_bytes')) {
|
|
|
|
return openssl_random_pseudo_bytes($length);
|
|
|
|
}
|
|
|
|
trigger_error(
|
|
|
|
'You do not have a safe source of random data available. ' .
|
|
|
|
'Install either the openssl extension, or paragonie/random_compat. ' .
|
|
|
|
'Falling back to an insecure random source.',
|
|
|
|
E_USER_WARNING
|
|
|
|
);
|
|
|
|
$bytes = '';
|
|
|
|
while ($bytes < $length) {
|
|
|
|
$bytes .= static::hash(CakeText::uuid() . uniqid(mt_rand(), true), 'sha512', true);
|
|
|
|
}
|
|
|
|
return substr($bytes, 0, $length);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-05-07 03:03:20 +00:00
|
|
|
* Runs $text through a XOR cipher.
|
|
|
|
*
|
|
|
|
* *Note* This is not a cryptographically strong method and should not be used
|
|
|
|
* for sensitive data. Additionally this method does *not* work in environments
|
|
|
|
* where suhosin is enabled.
|
|
|
|
*
|
|
|
|
* Instead you should use Security::rijndael() when you need strong
|
|
|
|
* encryption.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $text Encrypted string to decrypt, normal string to encrypt
|
|
|
|
* @param string $key Key to use
|
|
|
|
* @return string Encrypted/Decrypted string
|
2014-09-02 15:03:22 +00:00
|
|
|
* @deprecated 3.0.0 Will be removed in 3.0.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function cipher($text, $key) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($key)) {
|
2013-08-16 09:39:38 +00:00
|
|
|
trigger_error(__d('cake_dev', 'You cannot use an empty key for %s', 'Security::cipher()'), E_USER_WARNING);
|
2008-05-30 11:40:08 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2015-12-13 20:18:59 +00:00
|
|
|
srand((int)Configure::read('Security.cipherSeed'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$out = '';
|
2010-01-11 20:51:04 +00:00
|
|
|
$keyLength = strlen($key);
|
2010-03-27 21:19:42 +00:00
|
|
|
for ($i = 0, $textLength = strlen($text); $i < $textLength; $i++) {
|
|
|
|
$j = ord(substr($key, $i % $keyLength, 1));
|
|
|
|
while ($j--) {
|
2010-01-11 20:51:04 +00:00
|
|
|
rand(0, 255);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-07-05 15:03:46 +00:00
|
|
|
$mask = rand(0, 255);
|
2010-03-27 21:19:42 +00:00
|
|
|
$out .= chr(ord(substr($text, $i, 1)) ^ $mask);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-01-19 19:53:20 +00:00
|
|
|
srand();
|
2008-05-30 11:40:08 +00:00
|
|
|
return $out;
|
|
|
|
}
|
2012-03-03 22:31:47 +00:00
|
|
|
|
2012-05-29 23:25:01 +00:00
|
|
|
/**
|
|
|
|
* Encrypts/Decrypts a text using the given key using rijndael method.
|
|
|
|
*
|
2013-02-09 19:55:13 +00:00
|
|
|
* Prior to 2.3.1, a fixed initialization vector was used. This was not
|
|
|
|
* secure. This method now uses a random iv, and will silently upgrade values when
|
|
|
|
* they are re-encrypted.
|
|
|
|
*
|
2012-05-29 23:25:01 +00:00
|
|
|
* @param string $text Encrypted string to decrypt, normal string to encrypt
|
2013-02-09 19:14:27 +00:00
|
|
|
* @param string $key Key to use as the encryption key for encrypted data.
|
2012-05-29 23:25:01 +00:00
|
|
|
* @param string $operation Operation to perform, encrypt or decrypt
|
2013-03-05 07:05:14 +00:00
|
|
|
* @return string Encrypted/Decrypted string
|
2012-05-29 23:25:01 +00:00
|
|
|
*/
|
|
|
|
public static function rijndael($text, $key, $operation) {
|
|
|
|
if (empty($key)) {
|
2013-08-16 09:39:38 +00:00
|
|
|
trigger_error(__d('cake_dev', 'You cannot use an empty key for %s', 'Security::rijndael()'), E_USER_WARNING);
|
2012-05-29 23:25:01 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
if (empty($operation) || !in_array($operation, array('encrypt', 'decrypt'))) {
|
|
|
|
trigger_error(__d('cake_dev', 'You must specify the operation for Security::rijndael(), either encrypt or decrypt'), E_USER_WARNING);
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
if (strlen($key) < 32) {
|
|
|
|
trigger_error(__d('cake_dev', 'You must use a key larger than 32 bytes for Security::rijndael()'), E_USER_WARNING);
|
|
|
|
return '';
|
|
|
|
}
|
2013-02-09 19:14:27 +00:00
|
|
|
$algorithm = MCRYPT_RIJNDAEL_256;
|
|
|
|
$mode = MCRYPT_MODE_CBC;
|
2013-02-09 19:55:13 +00:00
|
|
|
$ivSize = mcrypt_get_iv_size($algorithm, $mode);
|
|
|
|
|
2012-05-29 23:25:01 +00:00
|
|
|
$cryptKey = substr($key, 0, 32);
|
2012-11-01 13:15:52 +00:00
|
|
|
|
2012-05-29 23:25:01 +00:00
|
|
|
if ($operation === 'encrypt') {
|
2013-02-09 19:55:13 +00:00
|
|
|
$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
|
|
|
|
return $iv . '$$' . mcrypt_encrypt($algorithm, $cryptKey, $text, $mode, $iv);
|
|
|
|
}
|
|
|
|
// Backwards compatible decrypt with fixed iv
|
|
|
|
if (substr($text, $ivSize, 2) !== '$$') {
|
|
|
|
$iv = substr($key, strlen($key) - 32, 32);
|
|
|
|
return rtrim(mcrypt_decrypt($algorithm, $cryptKey, $text, $mode, $iv), "\0");
|
2012-05-29 23:25:01 +00:00
|
|
|
}
|
2013-02-09 19:55:13 +00:00
|
|
|
$iv = substr($text, 0, $ivSize);
|
|
|
|
$text = substr($text, $ivSize + 2);
|
2012-11-01 13:15:52 +00:00
|
|
|
return rtrim(mcrypt_decrypt($algorithm, $cryptKey, $text, $mode, $iv), "\0");
|
2012-05-29 23:25:01 +00:00
|
|
|
}
|
|
|
|
|
2012-07-21 16:48:14 +00:00
|
|
|
/**
|
|
|
|
* Generates a pseudo random salt suitable for use with php's crypt() function.
|
|
|
|
* The salt length should not exceed 27. The salt will be composed of
|
|
|
|
* [./0-9A-Za-z]{$length}.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $length The length of the returned salt
|
2012-07-21 16:48:14 +00:00
|
|
|
* @return string The generated salt
|
|
|
|
*/
|
2012-11-11 21:25:08 +00:00
|
|
|
protected static function _salt($length = 22) {
|
2012-08-24 13:35:33 +00:00
|
|
|
$salt = str_replace(
|
|
|
|
array('+', '='),
|
|
|
|
'.',
|
|
|
|
base64_encode(sha1(uniqid(Configure::read('Security.salt'), true), true))
|
|
|
|
);
|
|
|
|
return substr($salt, 0, $length);
|
2012-07-21 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-11 21:25:08 +00:00
|
|
|
* One way encryption using php's crypt() function. To use blowfish hashing see ``Security::hash()``
|
2012-07-21 16:48:14 +00:00
|
|
|
*
|
|
|
|
* @param string $password The string to be encrypted.
|
|
|
|
* @param mixed $salt false to generate a new salt or an existing salt.
|
2012-11-11 21:25:08 +00:00
|
|
|
* @return string The hashed string or an empty string on error.
|
2012-07-21 16:48:14 +00:00
|
|
|
*/
|
2012-11-01 13:15:52 +00:00
|
|
|
protected static function _crypt($password, $salt = false) {
|
2012-07-21 16:48:14 +00:00
|
|
|
if ($salt === false) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$salt = static::_salt(22);
|
|
|
|
$salt = vsprintf('$2a$%02d$%s', array(static::$hashCost, $salt));
|
2012-11-01 13:15:52 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 03:20:12 +00:00
|
|
|
$invalidCipher = (
|
|
|
|
strpos($salt, '$2y$') !== 0 &&
|
|
|
|
strpos($salt, '$2x$') !== 0 &&
|
|
|
|
strpos($salt, '$2a$') !== 0
|
|
|
|
);
|
|
|
|
if ($salt === true || $invalidCipher || strlen($salt) < 29) {
|
2012-07-21 16:48:14 +00:00
|
|
|
trigger_error(__d(
|
|
|
|
'cake_dev',
|
|
|
|
'Invalid salt: %s for %s Please visit http://www.php.net/crypt and read the appropriate section for building %s salts.',
|
2012-11-01 13:15:52 +00:00
|
|
|
array($salt, 'blowfish', 'blowfish')
|
2012-07-21 16:48:14 +00:00
|
|
|
), E_USER_WARNING);
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return crypt($password, $salt);
|
|
|
|
}
|
|
|
|
|
2013-08-28 01:20:22 +00:00
|
|
|
/**
|
|
|
|
* Encrypt a value using AES-256.
|
|
|
|
*
|
|
|
|
* *Caveat* You cannot properly encrypt/decrypt data with trailing null bytes.
|
|
|
|
* Any trailing null bytes will be removed on decryption due to how PHP pads messages
|
|
|
|
* with nulls prior to encryption.
|
|
|
|
*
|
|
|
|
* @param string $plain The value to encrypt.
|
|
|
|
* @param string $key The 256 bit/32 byte key to use as a cipher key.
|
2013-08-29 18:40:01 +00:00
|
|
|
* @param string $hmacSalt The salt to use for the HMAC process. Leave null to use Security.salt.
|
2013-08-28 01:20:22 +00:00
|
|
|
* @return string Encrypted data.
|
|
|
|
* @throws CakeException On invalid data or key.
|
|
|
|
*/
|
2013-08-29 18:40:01 +00:00
|
|
|
public static function encrypt($plain, $key, $hmacSalt = null) {
|
2015-07-21 08:22:53 +00:00
|
|
|
static::_checkKey($key, 'encrypt()');
|
2013-12-15 13:56:21 +00:00
|
|
|
|
2013-08-29 18:40:01 +00:00
|
|
|
if ($hmacSalt === null) {
|
|
|
|
$hmacSalt = Configure::read('Security.salt');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the encryption and hmac key.
|
|
|
|
$key = substr(hash('sha256', $key . $hmacSalt), 0, 32);
|
|
|
|
|
2013-08-28 01:20:22 +00:00
|
|
|
$algorithm = MCRYPT_RIJNDAEL_128;
|
|
|
|
$mode = MCRYPT_MODE_CBC;
|
|
|
|
|
|
|
|
$ivSize = mcrypt_get_iv_size($algorithm, $mode);
|
2013-08-29 18:40:01 +00:00
|
|
|
$iv = mcrypt_create_iv($ivSize, MCRYPT_DEV_URANDOM);
|
2013-09-02 01:44:45 +00:00
|
|
|
$ciphertext = $iv . mcrypt_encrypt($algorithm, $key, $plain, $mode, $iv);
|
2013-08-29 18:40:01 +00:00
|
|
|
$hmac = hash_hmac('sha256', $ciphertext, $key);
|
|
|
|
return $hmac . $ciphertext;
|
2013-08-28 01:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the encryption key for proper length.
|
|
|
|
*
|
2014-05-28 16:39:54 +00:00
|
|
|
* @param string $key Key to check.
|
2013-08-28 01:20:22 +00:00
|
|
|
* @param string $method The method the key is being checked for.
|
|
|
|
* @return void
|
|
|
|
* @throws CakeException When key length is not 256 bit/32 bytes
|
|
|
|
*/
|
|
|
|
protected static function _checkKey($key, $method) {
|
|
|
|
if (strlen($key) < 32) {
|
|
|
|
throw new CakeException(__d('cake_dev', 'Invalid key for %s, key must be at least 256 bits (32 bytes) long.', $method));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrypt a value using AES-256.
|
|
|
|
*
|
|
|
|
* @param string $cipher The ciphertext to decrypt.
|
|
|
|
* @param string $key The 256 bit/32 byte key to use as a cipher key.
|
2013-08-29 18:40:01 +00:00
|
|
|
* @param string $hmacSalt The salt to use for the HMAC process. Leave null to use Security.salt.
|
2013-08-28 01:20:22 +00:00
|
|
|
* @return string Decrypted data. Any trailing null bytes will be removed.
|
|
|
|
* @throws CakeException On invalid data or key.
|
|
|
|
*/
|
2013-08-29 18:40:01 +00:00
|
|
|
public static function decrypt($cipher, $key, $hmacSalt = null) {
|
2015-07-21 08:22:53 +00:00
|
|
|
static::_checkKey($key, 'decrypt()');
|
2013-08-28 01:20:22 +00:00
|
|
|
if (empty($cipher)) {
|
|
|
|
throw new CakeException(__d('cake_dev', 'The data to decrypt cannot be empty.'));
|
|
|
|
}
|
2013-08-29 18:40:01 +00:00
|
|
|
if ($hmacSalt === null) {
|
|
|
|
$hmacSalt = Configure::read('Security.salt');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the encryption and hmac key.
|
|
|
|
$key = substr(hash('sha256', $key . $hmacSalt), 0, 32);
|
|
|
|
|
|
|
|
// Split out hmac for comparison
|
|
|
|
$macSize = 64;
|
|
|
|
$hmac = substr($cipher, 0, $macSize);
|
|
|
|
$cipher = substr($cipher, $macSize);
|
|
|
|
|
|
|
|
$compareHmac = hash_hmac('sha256', $cipher, $key);
|
|
|
|
if ($hmac !== $compareHmac) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-28 01:20:22 +00:00
|
|
|
|
|
|
|
$algorithm = MCRYPT_RIJNDAEL_128;
|
|
|
|
$mode = MCRYPT_MODE_CBC;
|
|
|
|
$ivSize = mcrypt_get_iv_size($algorithm, $mode);
|
|
|
|
|
|
|
|
$iv = substr($cipher, 0, $ivSize);
|
|
|
|
$cipher = substr($cipher, $ivSize);
|
|
|
|
$plain = mcrypt_decrypt($algorithm, $key, $cipher, $mode, $iv);
|
|
|
|
return rtrim($plain, "\0");
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|