Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/flow-flo.../libs/cakephp/utility
File: Security.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
[2] Fix | Delete
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
[3] Fix | Delete
*
[4] Fix | Delete
* Licensed under The MIT License
[5] Fix | Delete
* For full copyright and license information, please see the LICENSE.txt
[6] Fix | Delete
* Redistributions of files must retain the above copyright notice.
[7] Fix | Delete
*
[8] Fix | Delete
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
[9] Fix | Delete
* @link https://cakephp.org CakePHP(tm) Project
[10] Fix | Delete
* @since 0.10.0
[11] Fix | Delete
* @license https://opensource.org/licenses/mit-license.php MIT License
[12] Fix | Delete
*/
[13] Fix | Delete
namespace Cake\Utility;
[14] Fix | Delete
[15] Fix | Delete
use Cake\Utility\Crypto\Mcrypt;
[16] Fix | Delete
use Cake\Utility\Crypto\OpenSsl;
[17] Fix | Delete
use InvalidArgumentException;
[18] Fix | Delete
use RuntimeException;
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Security Library contains utility methods related to security
[22] Fix | Delete
*/
[23] Fix | Delete
class Security
[24] Fix | Delete
{
[25] Fix | Delete
/**
[26] Fix | Delete
* Default hash method. If `$type` param for `Security::hash()` is not specified
[27] Fix | Delete
* this value is used. Defaults to 'sha1'.
[28] Fix | Delete
*
[29] Fix | Delete
* @var string
[30] Fix | Delete
*/
[31] Fix | Delete
public static $hashType = 'sha1';
[32] Fix | Delete
[33] Fix | Delete
/**
[34] Fix | Delete
* The HMAC salt to use for encryption and decryption routines
[35] Fix | Delete
*
[36] Fix | Delete
* @var string|null
[37] Fix | Delete
*/
[38] Fix | Delete
protected static $_salt;
[39] Fix | Delete
[40] Fix | Delete
/**
[41] Fix | Delete
* The crypto implementation to use.
[42] Fix | Delete
*
[43] Fix | Delete
* @var object
[44] Fix | Delete
*/
[45] Fix | Delete
protected static $_instance;
[46] Fix | Delete
[47] Fix | Delete
/**
[48] Fix | Delete
* Create a hash from string using given method.
[49] Fix | Delete
*
[50] Fix | Delete
* @param string $string String to hash
[51] Fix | Delete
* @param string|null $algorithm Hashing algo to use (i.e. sha1, sha256 etc.).
[52] Fix | Delete
* Can be any valid algo included in list returned by hash_algos().
[53] Fix | Delete
* If no value is passed the type specified by `Security::$hashType` is used.
[54] Fix | Delete
* @param mixed $salt If true, automatically prepends the application's salt
[55] Fix | Delete
* value to $string (Security.salt).
[56] Fix | Delete
* @return string Hash
[57] Fix | Delete
* @throws \RuntimeException
[58] Fix | Delete
* @link https://book.cakephp.org/3/en/core-libraries/security.html#hashing-data
[59] Fix | Delete
*/
[60] Fix | Delete
public static function hash($string, $algorithm = null, $salt = false)
[61] Fix | Delete
{
[62] Fix | Delete
if (empty($algorithm)) {
[63] Fix | Delete
$algorithm = static::$hashType;
[64] Fix | Delete
}
[65] Fix | Delete
$algorithm = strtolower($algorithm);
[66] Fix | Delete
[67] Fix | Delete
$availableAlgorithms = hash_algos();
[68] Fix | Delete
if (!in_array($algorithm, $availableAlgorithms, true)) {
[69] Fix | Delete
throw new RuntimeException(sprintf(
[70] Fix | Delete
'The hash type `%s` was not found. Available algorithms are: %s',
[71] Fix | Delete
$algorithm,
[72] Fix | Delete
implode(', ', $availableAlgorithms)
[73] Fix | Delete
));
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
if ($salt) {
[77] Fix | Delete
if (!is_string($salt)) {
[78] Fix | Delete
$salt = static::$_salt;
[79] Fix | Delete
}
[80] Fix | Delete
$string = $salt . $string;
[81] Fix | Delete
}
[82] Fix | Delete
[83] Fix | Delete
return hash($algorithm, $string);
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
/**
[87] Fix | Delete
* Sets the default hash method for the Security object. This affects all objects
[88] Fix | Delete
* using Security::hash().
[89] Fix | Delete
*
[90] Fix | Delete
* @param string $hash Method to use (sha1/sha256/md5 etc.)
[91] Fix | Delete
* @return void
[92] Fix | Delete
* @see \Cake\Utility\Security::hash()
[93] Fix | Delete
*/
[94] Fix | Delete
public static function setHash($hash)
[95] Fix | Delete
{
[96] Fix | Delete
static::$hashType = $hash;
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
/**
[100] Fix | Delete
* Get random bytes from a secure source.
[101] Fix | Delete
*
[102] Fix | Delete
* This method will fall back to an insecure source an trigger a warning
[103] Fix | Delete
* if it cannot find a secure source of random data.
[104] Fix | Delete
*
[105] Fix | Delete
* @param int $length The number of bytes you want.
[106] Fix | Delete
* @return string Random bytes in binary.
[107] Fix | Delete
*/
[108] Fix | Delete
public static function randomBytes($length)
[109] Fix | Delete
{
[110] Fix | Delete
if (function_exists('random_bytes')) {
[111] Fix | Delete
return random_bytes($length);
[112] Fix | Delete
}
[113] Fix | Delete
if (!function_exists('openssl_random_pseudo_bytes')) {
[114] Fix | Delete
throw new RuntimeException(
[115] Fix | Delete
'You do not have a safe source of random data available. ' .
[116] Fix | Delete
'Install either the openssl extension, or paragonie/random_compat. ' .
[117] Fix | Delete
'Or use Security::insecureRandomBytes() alternatively.'
[118] Fix | Delete
);
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
$bytes = openssl_random_pseudo_bytes($length, $strongSource);
[122] Fix | Delete
if (!$strongSource) {
[123] Fix | Delete
trigger_error(
[124] Fix | Delete
'openssl was unable to use a strong source of entropy. ' .
[125] Fix | Delete
'Consider updating your system libraries, or ensuring ' .
[126] Fix | Delete
'you have more available entropy.',
[127] Fix | Delete
E_USER_WARNING
[128] Fix | Delete
);
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
return $bytes;
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
/**
[135] Fix | Delete
* Creates a secure random string.
[136] Fix | Delete
*
[137] Fix | Delete
* @param int $length String length. Default 64.
[138] Fix | Delete
* @return string
[139] Fix | Delete
* @since 3.6.0
[140] Fix | Delete
*/
[141] Fix | Delete
public static function randomString($length = 64)
[142] Fix | Delete
{
[143] Fix | Delete
return substr(
[144] Fix | Delete
bin2hex(Security::randomBytes(ceil($length / 2))),
[145] Fix | Delete
0,
[146] Fix | Delete
$length
[147] Fix | Delete
);
[148] Fix | Delete
}
[149] Fix | Delete
[150] Fix | Delete
/**
[151] Fix | Delete
* Like randomBytes() above, but not cryptographically secure.
[152] Fix | Delete
*
[153] Fix | Delete
* @param int $length The number of bytes you want.
[154] Fix | Delete
* @return string Random bytes in binary.
[155] Fix | Delete
* @see \Cake\Utility\Security::randomBytes()
[156] Fix | Delete
*/
[157] Fix | Delete
public static function insecureRandomBytes($length)
[158] Fix | Delete
{
[159] Fix | Delete
$length *= 2;
[160] Fix | Delete
[161] Fix | Delete
$bytes = '';
[162] Fix | Delete
$byteLength = 0;
[163] Fix | Delete
while ($byteLength < $length) {
[164] Fix | Delete
$bytes .= static::hash(Text::uuid() . uniqid(mt_rand(), true), 'sha512', true);
[165] Fix | Delete
$byteLength = strlen($bytes);
[166] Fix | Delete
}
[167] Fix | Delete
$bytes = substr($bytes, 0, $length);
[168] Fix | Delete
[169] Fix | Delete
return pack('H*', $bytes);
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
/**
[173] Fix | Delete
* Get the crypto implementation based on the loaded extensions.
[174] Fix | Delete
*
[175] Fix | Delete
* You can use this method to forcibly decide between mcrypt/openssl/custom implementations.
[176] Fix | Delete
*
[177] Fix | Delete
* @param \Cake\Utility\Crypto\OpenSsl|\Cake\Utility\Crypto\Mcrypt|null $instance The crypto instance to use.
[178] Fix | Delete
* @return \Cake\Utility\Crypto\OpenSsl|\Cake\Utility\Crypto\Mcrypt Crypto instance.
[179] Fix | Delete
* @throws \InvalidArgumentException When no compatible crypto extension is available.
[180] Fix | Delete
*/
[181] Fix | Delete
public static function engine($instance = null)
[182] Fix | Delete
{
[183] Fix | Delete
if ($instance === null && static::$_instance === null) {
[184] Fix | Delete
if (extension_loaded('openssl')) {
[185] Fix | Delete
$instance = new OpenSsl();
[186] Fix | Delete
} elseif (extension_loaded('mcrypt')) {
[187] Fix | Delete
$instance = new Mcrypt();
[188] Fix | Delete
}
[189] Fix | Delete
}
[190] Fix | Delete
if ($instance) {
[191] Fix | Delete
static::$_instance = $instance;
[192] Fix | Delete
}
[193] Fix | Delete
if (isset(static::$_instance)) {
[194] Fix | Delete
return static::$_instance;
[195] Fix | Delete
}
[196] Fix | Delete
throw new InvalidArgumentException(
[197] Fix | Delete
'No compatible crypto engine available. ' .
[198] Fix | Delete
'Load either the openssl or mcrypt extensions'
[199] Fix | Delete
);
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
/**
[203] Fix | Delete
* Encrypts/Decrypts a text using the given key using rijndael method.
[204] Fix | Delete
*
[205] Fix | Delete
* @param string $text Encrypted string to decrypt, normal string to encrypt
[206] Fix | Delete
* @param string $key Key to use as the encryption key for encrypted data.
[207] Fix | Delete
* @param string $operation Operation to perform, encrypt or decrypt
[208] Fix | Delete
* @throws \InvalidArgumentException When there are errors.
[209] Fix | Delete
* @return string Encrypted/Decrypted string.
[210] Fix | Delete
* @deprecated 3.6.3 This method relies on functions provided by mcrypt
[211] Fix | Delete
* extension which has been deprecated in PHP 7.1 and removed in PHP 7.2.
[212] Fix | Delete
* There's no 1:1 replacement for this method.
[213] Fix | Delete
* Upgrade your code to use Security::encrypt()/Security::decrypt() with
[214] Fix | Delete
* OpenSsl engine instead.
[215] Fix | Delete
*/
[216] Fix | Delete
public static function rijndael($text, $key, $operation)
[217] Fix | Delete
{
[218] Fix | Delete
if (empty($key)) {
[219] Fix | Delete
throw new InvalidArgumentException('You cannot use an empty key for Security::rijndael()');
[220] Fix | Delete
}
[221] Fix | Delete
if (empty($operation) || !in_array($operation, ['encrypt', 'decrypt'])) {
[222] Fix | Delete
throw new InvalidArgumentException('You must specify the operation for Security::rijndael(), either encrypt or decrypt');
[223] Fix | Delete
}
[224] Fix | Delete
if (mb_strlen($key, '8bit') < 32) {
[225] Fix | Delete
throw new InvalidArgumentException('You must use a key larger than 32 bytes for Security::rijndael()');
[226] Fix | Delete
}
[227] Fix | Delete
$crypto = static::engine();
[228] Fix | Delete
[229] Fix | Delete
return $crypto->rijndael($text, $key, $operation);
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
/**
[233] Fix | Delete
* Encrypt a value using AES-256.
[234] Fix | Delete
*
[235] Fix | Delete
* *Caveat* You cannot properly encrypt/decrypt data with trailing null bytes.
[236] Fix | Delete
* Any trailing null bytes will be removed on decryption due to how PHP pads messages
[237] Fix | Delete
* with nulls prior to encryption.
[238] Fix | Delete
*
[239] Fix | Delete
* @param string $plain The value to encrypt.
[240] Fix | Delete
* @param string $key The 256 bit/32 byte key to use as a cipher key.
[241] Fix | Delete
* @param string|null $hmacSalt The salt to use for the HMAC process. Leave null to use Security.salt.
[242] Fix | Delete
* @return string Encrypted data.
[243] Fix | Delete
* @throws \InvalidArgumentException On invalid data or key.
[244] Fix | Delete
*/
[245] Fix | Delete
public static function encrypt($plain, $key, $hmacSalt = null)
[246] Fix | Delete
{
[247] Fix | Delete
self::_checkKey($key, 'encrypt()');
[248] Fix | Delete
[249] Fix | Delete
if ($hmacSalt === null) {
[250] Fix | Delete
$hmacSalt = static::$_salt;
[251] Fix | Delete
}
[252] Fix | Delete
// Generate the encryption and hmac key.
[253] Fix | Delete
$key = mb_substr(hash('sha256', $key . $hmacSalt), 0, 32, '8bit');
[254] Fix | Delete
[255] Fix | Delete
$crypto = static::engine();
[256] Fix | Delete
$ciphertext = $crypto->encrypt($plain, $key);
[257] Fix | Delete
$hmac = hash_hmac('sha256', $ciphertext, $key);
[258] Fix | Delete
[259] Fix | Delete
return $hmac . $ciphertext;
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
/**
[263] Fix | Delete
* Check the encryption key for proper length.
[264] Fix | Delete
*
[265] Fix | Delete
* @param string $key Key to check.
[266] Fix | Delete
* @param string $method The method the key is being checked for.
[267] Fix | Delete
* @return void
[268] Fix | Delete
* @throws \InvalidArgumentException When key length is not 256 bit/32 bytes
[269] Fix | Delete
*/
[270] Fix | Delete
protected static function _checkKey($key, $method)
[271] Fix | Delete
{
[272] Fix | Delete
if (mb_strlen($key, '8bit') < 32) {
[273] Fix | Delete
throw new InvalidArgumentException(
[274] Fix | Delete
sprintf('Invalid key for %s, key must be at least 256 bits (32 bytes) long.', $method)
[275] Fix | Delete
);
[276] Fix | Delete
}
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
/**
[280] Fix | Delete
* Decrypt a value using AES-256.
[281] Fix | Delete
*
[282] Fix | Delete
* @param string $cipher The ciphertext to decrypt.
[283] Fix | Delete
* @param string $key The 256 bit/32 byte key to use as a cipher key.
[284] Fix | Delete
* @param string|null $hmacSalt The salt to use for the HMAC process. Leave null to use Security.salt.
[285] Fix | Delete
* @return string|false Decrypted data. Any trailing null bytes will be removed.
[286] Fix | Delete
* @throws \InvalidArgumentException On invalid data or key.
[287] Fix | Delete
*/
[288] Fix | Delete
public static function decrypt($cipher, $key, $hmacSalt = null)
[289] Fix | Delete
{
[290] Fix | Delete
self::_checkKey($key, 'decrypt()');
[291] Fix | Delete
if (empty($cipher)) {
[292] Fix | Delete
throw new InvalidArgumentException('The data to decrypt cannot be empty.');
[293] Fix | Delete
}
[294] Fix | Delete
if ($hmacSalt === null) {
[295] Fix | Delete
$hmacSalt = static::$_salt;
[296] Fix | Delete
}
[297] Fix | Delete
[298] Fix | Delete
// Generate the encryption and hmac key.
[299] Fix | Delete
$key = mb_substr(hash('sha256', $key . $hmacSalt), 0, 32, '8bit');
[300] Fix | Delete
[301] Fix | Delete
// Split out hmac for comparison
[302] Fix | Delete
$macSize = 64;
[303] Fix | Delete
$hmac = mb_substr($cipher, 0, $macSize, '8bit');
[304] Fix | Delete
$cipher = mb_substr($cipher, $macSize, null, '8bit');
[305] Fix | Delete
[306] Fix | Delete
$compareHmac = hash_hmac('sha256', $cipher, $key);
[307] Fix | Delete
if (!static::constantEquals($hmac, $compareHmac)) {
[308] Fix | Delete
return false;
[309] Fix | Delete
}
[310] Fix | Delete
[311] Fix | Delete
$crypto = static::engine();
[312] Fix | Delete
[313] Fix | Delete
return $crypto->decrypt($cipher, $key);
[314] Fix | Delete
}
[315] Fix | Delete
[316] Fix | Delete
/**
[317] Fix | Delete
* A timing attack resistant comparison that prefers native PHP implementations.
[318] Fix | Delete
*
[319] Fix | Delete
* @param string $original The original value.
[320] Fix | Delete
* @param string $compare The comparison value.
[321] Fix | Delete
* @return bool
[322] Fix | Delete
* @see https://github.com/resonantcore/php-future/
[323] Fix | Delete
* @since 3.6.2
[324] Fix | Delete
*/
[325] Fix | Delete
public static function constantEquals($original, $compare)
[326] Fix | Delete
{
[327] Fix | Delete
if (!is_string($original) || !is_string($compare)) {
[328] Fix | Delete
return false;
[329] Fix | Delete
}
[330] Fix | Delete
if (function_exists('hash_equals')) {
[331] Fix | Delete
return hash_equals($original, $compare);
[332] Fix | Delete
}
[333] Fix | Delete
$originalLength = mb_strlen($original, '8bit');
[334] Fix | Delete
$compareLength = mb_strlen($compare, '8bit');
[335] Fix | Delete
if ($originalLength !== $compareLength) {
[336] Fix | Delete
return false;
[337] Fix | Delete
}
[338] Fix | Delete
$result = 0;
[339] Fix | Delete
for ($i = 0; $i < $originalLength; $i++) {
[340] Fix | Delete
$result |= (ord($original[$i]) ^ ord($compare[$i]));
[341] Fix | Delete
}
[342] Fix | Delete
[343] Fix | Delete
return $result === 0;
[344] Fix | Delete
}
[345] Fix | Delete
[346] Fix | Delete
/**
[347] Fix | Delete
* Gets the HMAC salt to be used for encryption/decryption
[348] Fix | Delete
* routines.
[349] Fix | Delete
*
[350] Fix | Delete
* @return string The currently configured salt
[351] Fix | Delete
*/
[352] Fix | Delete
public static function getSalt()
[353] Fix | Delete
{
[354] Fix | Delete
if (static::$_salt === null) {
[355] Fix | Delete
throw new RuntimeException(
[356] Fix | Delete
'Salt not set. Use Security::setSalt() to set one, ideally in `config/bootstrap.php`.'
[357] Fix | Delete
);
[358] Fix | Delete
}
[359] Fix | Delete
[360] Fix | Delete
return static::$_salt;
[361] Fix | Delete
}
[362] Fix | Delete
[363] Fix | Delete
/**
[364] Fix | Delete
* Sets the HMAC salt to be used for encryption/decryption
[365] Fix | Delete
* routines.
[366] Fix | Delete
*
[367] Fix | Delete
* @param string $salt The salt to use for encryption routines.
[368] Fix | Delete
* @return void
[369] Fix | Delete
*/
[370] Fix | Delete
public static function setSalt($salt)
[371] Fix | Delete
{
[372] Fix | Delete
static::$_salt = (string)$salt;
[373] Fix | Delete
}
[374] Fix | Delete
[375] Fix | Delete
/**
[376] Fix | Delete
* Gets or sets the HMAC salt to be used for encryption/decryption
[377] Fix | Delete
* routines.
[378] Fix | Delete
*
[379] Fix | Delete
* @deprecated 3.5.0 Use getSalt()/setSalt() instead.
[380] Fix | Delete
* @param string|null $salt The salt to use for encryption routines. If null returns current salt.
[381] Fix | Delete
* @return string The currently configured salt
[382] Fix | Delete
*/
[383] Fix | Delete
public static function salt($salt = null)
[384] Fix | Delete
{
[385] Fix | Delete
deprecationWarning(
[386] Fix | Delete
'Security::salt() is deprecated. ' .
[387] Fix | Delete
'Use Security::getSalt()/setSalt() instead.'
[388] Fix | Delete
);
[389] Fix | Delete
if ($salt === null) {
[390] Fix | Delete
return static::$_salt;
[391] Fix | Delete
}
[392] Fix | Delete
[393] Fix | Delete
return static::$_salt = (string)$salt;
[394] Fix | Delete
}
[395] Fix | Delete
}
[396] Fix | Delete
[397] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function