Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/worker/src/PHPSecLi.../Crypt
File: Random.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Random Number Generator
[3] Fix | Delete
*
[4] Fix | Delete
* The idea behind this function is that it can be easily replaced with your own crypt_random_string()
[5] Fix | Delete
* function. eg. maybe you have a better source of entropy for creating the initial states or whatever.
[6] Fix | Delete
*
[7] Fix | Delete
* PHP versions 4 and 5
[8] Fix | Delete
*
[9] Fix | Delete
* Here's a short example of how to use this library:
[10] Fix | Delete
* <code>
[11] Fix | Delete
* <?php
[12] Fix | Delete
* include 'Crypt/Random.php';
[13] Fix | Delete
*
[14] Fix | Delete
* echo bin2hex(crypt_random_string(8));
[15] Fix | Delete
* ?>
[16] Fix | Delete
* </code>
[17] Fix | Delete
*
[18] Fix | Delete
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
[19] Fix | Delete
* of this software and associated documentation files (the "Software"), to deal
[20] Fix | Delete
* in the Software without restriction, including without limitation the rights
[21] Fix | Delete
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
[22] Fix | Delete
* copies of the Software, and to permit persons to whom the Software is
[23] Fix | Delete
* furnished to do so, subject to the following conditions:
[24] Fix | Delete
*
[25] Fix | Delete
* The above copyright notice and this permission notice shall be included in
[26] Fix | Delete
* all copies or substantial portions of the Software.
[27] Fix | Delete
*
[28] Fix | Delete
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
[29] Fix | Delete
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
[30] Fix | Delete
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
[31] Fix | Delete
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
[32] Fix | Delete
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
[33] Fix | Delete
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
[34] Fix | Delete
* THE SOFTWARE.
[35] Fix | Delete
*
[36] Fix | Delete
* @category Crypt
[37] Fix | Delete
* @package Crypt_Random
[38] Fix | Delete
* @author Jim Wigginton <terrafrost@php.net>
[39] Fix | Delete
* @copyright MMVII Jim Wigginton
[40] Fix | Delete
* @license http://www.opensource.org/licenses/mit-license.html MIT License
[41] Fix | Delete
* @link http://phpseclib.sourceforge.net
[42] Fix | Delete
*/
[43] Fix | Delete
[44] Fix | Delete
// laravel is a PHP framework that utilizes phpseclib. laravel workbenches may, independently,
[45] Fix | Delete
// have phpseclib as a requirement as well. if you're developing such a program you may encounter
[46] Fix | Delete
// a "Cannot redeclare crypt_random_string()" error.
[47] Fix | Delete
if (!function_exists('crypt_random_string')) {
[48] Fix | Delete
/**
[49] Fix | Delete
* "Is Windows" test
[50] Fix | Delete
*
[51] Fix | Delete
* @access private
[52] Fix | Delete
*/
[53] Fix | Delete
define('CRYPT_RANDOM_IS_WINDOWS', strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
[54] Fix | Delete
[55] Fix | Delete
/**
[56] Fix | Delete
* Generate a random string.
[57] Fix | Delete
*
[58] Fix | Delete
* Although microoptimizations are generally discouraged as they impair readability this function is ripe with
[59] Fix | Delete
* microoptimizations because this function has the potential of being called a huge number of times.
[60] Fix | Delete
* eg. for RSA key generation.
[61] Fix | Delete
*
[62] Fix | Delete
* @param Integer $length
[63] Fix | Delete
*
[64] Fix | Delete
* @return String
[65] Fix | Delete
* @access public
[66] Fix | Delete
*/
[67] Fix | Delete
function crypt_random_string($length)
[68] Fix | Delete
{
[69] Fix | Delete
if (CRYPT_RANDOM_IS_WINDOWS) {
[70] Fix | Delete
// method 1. prior to PHP 5.3 this would call rand() on windows hence the function_exists('class_alias') call.
[71] Fix | Delete
// ie. class_alias is a function that was introduced in PHP 5.3
[72] Fix | Delete
if (version_compare(PHP_VERSION, '5.3.6', '!=') && function_exists('mcrypt_create_iv') && function_exists('class_alias')) {
[73] Fix | Delete
return mcrypt_create_iv($length);
[74] Fix | Delete
}
[75] Fix | Delete
// method 2. openssl_random_pseudo_bytes was introduced in PHP 5.3.0 but prior to PHP 5.3.4 there was,
[76] Fix | Delete
// to quote <http://php.net/ChangeLog-5.php#5.3.4>, "possible blocking behavior". as of 5.3.4
[77] Fix | Delete
// openssl_random_pseudo_bytes and mcrypt_create_iv do the exact same thing on Windows. ie. they both
[78] Fix | Delete
// call php_win32_get_random_bytes():
[79] Fix | Delete
//
[80] Fix | Delete
// https://github.com/php/php-src/blob/7014a0eb6d1611151a286c0ff4f2238f92c120d6/ext/openssl/openssl.c#L5008
[81] Fix | Delete
// https://github.com/php/php-src/blob/7014a0eb6d1611151a286c0ff4f2238f92c120d6/ext/mcrypt/mcrypt.c#L1392
[82] Fix | Delete
//
[83] Fix | Delete
// php_win32_get_random_bytes() is defined thusly:
[84] Fix | Delete
//
[85] Fix | Delete
// https://github.com/php/php-src/blob/7014a0eb6d1611151a286c0ff4f2238f92c120d6/win32/winutil.c#L80
[86] Fix | Delete
//
[87] Fix | Delete
// we're calling it, all the same, in the off chance that the mcrypt extension is not available
[88] Fix | Delete
if (function_exists('openssl_random_pseudo_bytes') && version_compare(PHP_VERSION, '5.3.4', '>=')) {
[89] Fix | Delete
return openssl_random_pseudo_bytes($length);
[90] Fix | Delete
}
[91] Fix | Delete
} else {
[92] Fix | Delete
// method 1. the fastest
[93] Fix | Delete
if (function_exists('openssl_random_pseudo_bytes')) {
[94] Fix | Delete
return openssl_random_pseudo_bytes($length);
[95] Fix | Delete
}
[96] Fix | Delete
// method 2
[97] Fix | Delete
static $fp = true;
[98] Fix | Delete
if ($fp === true) {
[99] Fix | Delete
// warning's will be output unles the error suppression operator is used. errors such as
[100] Fix | Delete
// "open_basedir restriction in effect", "Permission denied", "No such file or directory", etc.
[101] Fix | Delete
$fp = @fopen('/dev/urandom', 'rb');
[102] Fix | Delete
}
[103] Fix | Delete
if ($fp !== true && $fp !== false) { // surprisingly faster than !is_bool() or is_resource()
[104] Fix | Delete
return fread($fp, $length);
[105] Fix | Delete
}
[106] Fix | Delete
// method 3. pretty much does the same thing as method 2 per the following url:
[107] Fix | Delete
// https://github.com/php/php-src/blob/7014a0eb6d1611151a286c0ff4f2238f92c120d6/ext/mcrypt/mcrypt.c#L1391
[108] Fix | Delete
// surprisingly slower than method 2. maybe that's because mcrypt_create_iv does a bunch of error checking that we're
[109] Fix | Delete
// not doing. regardless, this'll only be called if this PHP script couldn't open /dev/urandom due to open_basedir
[110] Fix | Delete
// restrictions or some such
[111] Fix | Delete
if (function_exists('mcrypt_create_iv')) {
[112] Fix | Delete
return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
[113] Fix | Delete
}
[114] Fix | Delete
}
[115] Fix | Delete
// at this point we have no choice but to use a pure-PHP CSPRNG
[116] Fix | Delete
[117] Fix | Delete
// cascade entropy across multiple PHP instances by fixing the session and collecting all
[118] Fix | Delete
// environmental variables, including the previous session data and the current session
[119] Fix | Delete
// data.
[120] Fix | Delete
//
[121] Fix | Delete
// mt_rand seeds itself by looking at the PID and the time, both of which are (relatively)
[122] Fix | Delete
// easy to guess at. linux uses mouse clicks, keyboard timings, etc, as entropy sources, but
[123] Fix | Delete
// PHP isn't low level to be able to use those as sources and on a web server there's not likely
[124] Fix | Delete
// going to be a ton of keyboard or mouse action. web servers do have one thing that we can use
[125] Fix | Delete
// however, a ton of people visiting the website. obviously you don't want to base your seeding
[126] Fix | Delete
// soley on parameters a potential attacker sends but (1) not everything in $_SERVER is controlled
[127] Fix | Delete
// by the user and (2) this isn't just looking at the data sent by the current user - it's based
[128] Fix | Delete
// on the data sent by all users. one user requests the page and a hash of their info is saved.
[129] Fix | Delete
// another user visits the page and the serialization of their data is utilized along with the
[130] Fix | Delete
// server envirnment stuff and a hash of the previous http request data (which itself utilizes
[131] Fix | Delete
// a hash of the session data before that). certainly an attacker should be assumed to have
[132] Fix | Delete
// full control over his own http requests. he, however, is not going to have control over
[133] Fix | Delete
// everyone's http requests.
[134] Fix | Delete
static $crypto = false, $v;
[135] Fix | Delete
if ($crypto === false) {
[136] Fix | Delete
// save old session data
[137] Fix | Delete
$old_session_id = session_id();
[138] Fix | Delete
$old_use_cookies = ini_get('session.use_cookies');
[139] Fix | Delete
$old_session_cache_limiter = session_cache_limiter();
[140] Fix | Delete
$_OLD_SESSION = isset($_SESSION) ? $_SESSION : false;
[141] Fix | Delete
if ($old_session_id != '') {
[142] Fix | Delete
session_write_close();
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
session_id(1);
[146] Fix | Delete
ini_set('session.use_cookies', 0);
[147] Fix | Delete
session_cache_limiter('');
[148] Fix | Delete
session_start();
[149] Fix | Delete
[150] Fix | Delete
$v = $seed = $_SESSION['seed'] = pack('H*', sha1(
[151] Fix | Delete
serialize($_SERVER).
[152] Fix | Delete
serialize($_POST).
[153] Fix | Delete
serialize($_GET).
[154] Fix | Delete
serialize($_COOKIE).
[155] Fix | Delete
serialize($_SESSION).
[156] Fix | Delete
serialize($_OLD_SESSION)
[157] Fix | Delete
));
[158] Fix | Delete
if (!isset($_SESSION['count'])) {
[159] Fix | Delete
$_SESSION['count'] = 0;
[160] Fix | Delete
}
[161] Fix | Delete
$_SESSION['count']++;
[162] Fix | Delete
[163] Fix | Delete
session_write_close();
[164] Fix | Delete
[165] Fix | Delete
// restore old session data
[166] Fix | Delete
if ($old_session_id != '') {
[167] Fix | Delete
session_id($old_session_id);
[168] Fix | Delete
session_start();
[169] Fix | Delete
ini_set('session.use_cookies', $old_use_cookies);
[170] Fix | Delete
session_cache_limiter($old_session_cache_limiter);
[171] Fix | Delete
} else {
[172] Fix | Delete
if ($_OLD_SESSION !== false) {
[173] Fix | Delete
$_SESSION = $_OLD_SESSION;
[174] Fix | Delete
unset($_OLD_SESSION);
[175] Fix | Delete
} else {
[176] Fix | Delete
unset($_SESSION);
[177] Fix | Delete
}
[178] Fix | Delete
}
[179] Fix | Delete
[180] Fix | Delete
// in SSH2 a shared secret and an exchange hash are generated through the key exchange process.
[181] Fix | Delete
// the IV client to server is the hash of that "nonce" with the letter A and for the encryption key it's the letter C.
[182] Fix | Delete
// if the hash doesn't produce enough a key or an IV that's long enough concat successive hashes of the
[183] Fix | Delete
// original hash and the current hash. we'll be emulating that. for more info see the following URL:
[184] Fix | Delete
//
[185] Fix | Delete
// http://tools.ietf.org/html/rfc4253#section-7.2
[186] Fix | Delete
//
[187] Fix | Delete
// see the is_string($crypto) part for an example of how to expand the keys
[188] Fix | Delete
$key = pack('H*', sha1($seed.'A'));
[189] Fix | Delete
$iv = pack('H*', sha1($seed.'C'));
[190] Fix | Delete
[191] Fix | Delete
// ciphers are used as per the nist.gov link below. also, see this link:
[192] Fix | Delete
//
[193] Fix | Delete
// http://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator#Designs_based_on_cryptographic_primitives
[194] Fix | Delete
switch (true) {
[195] Fix | Delete
case mwp_phpseclib_resolve_include_path('Crypt/AES.php'):
[196] Fix | Delete
if (!class_exists('Crypt_AES')) {
[197] Fix | Delete
require_once dirname(__FILE__).'/AES.php';
[198] Fix | Delete
}
[199] Fix | Delete
$crypto = new Crypt_AES(CRYPT_AES_MODE_CTR);
[200] Fix | Delete
break;
[201] Fix | Delete
case mwp_phpseclib_resolve_include_path('Crypt/Twofish.php'):
[202] Fix | Delete
if (!class_exists('Crypt_Twofish')) {
[203] Fix | Delete
require_once dirname(__FILE__).'/Twofish.php';
[204] Fix | Delete
}
[205] Fix | Delete
$crypto = new Crypt_Twofish(CRYPT_TWOFISH_MODE_CTR);
[206] Fix | Delete
break;
[207] Fix | Delete
case mwp_phpseclib_resolve_include_path('Crypt/Blowfish.php'):
[208] Fix | Delete
if (!class_exists('Crypt_Blowfish')) {
[209] Fix | Delete
require_once dirname(__FILE__).'/Blowfish.php';
[210] Fix | Delete
}
[211] Fix | Delete
$crypto = new Crypt_Blowfish(CRYPT_BLOWFISH_MODE_CTR);
[212] Fix | Delete
break;
[213] Fix | Delete
case mwp_phpseclib_resolve_include_path('Crypt/TripleDES.php'):
[214] Fix | Delete
if (!class_exists('Crypt_TripleDES')) {
[215] Fix | Delete
require_once dirname(__FILE__).'/TripleDES.php';
[216] Fix | Delete
}
[217] Fix | Delete
$crypto = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);
[218] Fix | Delete
break;
[219] Fix | Delete
case mwp_phpseclib_resolve_include_path('Crypt/DES.php'):
[220] Fix | Delete
if (!class_exists('Crypt_DES')) {
[221] Fix | Delete
require_once dirname(__FILE__).'/DES.php';
[222] Fix | Delete
}
[223] Fix | Delete
$crypto = new Crypt_DES(CRYPT_DES_MODE_CTR);
[224] Fix | Delete
break;
[225] Fix | Delete
case mwp_phpseclib_resolve_include_path('Crypt/RC4.php'):
[226] Fix | Delete
if (!class_exists('Crypt_RC4')) {
[227] Fix | Delete
require_once dirname(__FILE__).'/RC4.php';
[228] Fix | Delete
}
[229] Fix | Delete
$crypto = new Crypt_RC4();
[230] Fix | Delete
break;
[231] Fix | Delete
default:
[232] Fix | Delete
user_error('crypt_random_string requires at least one symmetric cipher be loaded');
[233] Fix | Delete
[234] Fix | Delete
return false;
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
$crypto->setKey($key);
[238] Fix | Delete
$crypto->setIV($iv);
[239] Fix | Delete
$crypto->enableContinuousBuffer();
[240] Fix | Delete
}
[241] Fix | Delete
[242] Fix | Delete
//return $crypto->encrypt(str_repeat("\0", $length));
[243] Fix | Delete
[244] Fix | Delete
// the following is based off of ANSI X9.31:
[245] Fix | Delete
//
[246] Fix | Delete
// http://csrc.nist.gov/groups/STM/cavp/documents/rng/931rngext.pdf
[247] Fix | Delete
//
[248] Fix | Delete
// OpenSSL uses that same standard for it's random numbers:
[249] Fix | Delete
//
[250] Fix | Delete
// http://www.opensource.apple.com/source/OpenSSL/OpenSSL-38/openssl/fips-1.0/rand/fips_rand.c
[251] Fix | Delete
// (do a search for "ANS X9.31 A.2.4")
[252] Fix | Delete
$result = '';
[253] Fix | Delete
while (strlen($result) < $length) {
[254] Fix | Delete
$i = $crypto->encrypt(microtime()); // strlen(microtime()) == 21
[255] Fix | Delete
$r = $crypto->encrypt($i ^ $v); // strlen($v) == 20
[256] Fix | Delete
$v = $crypto->encrypt($r ^ $i); // strlen($r) == 20
[257] Fix | Delete
$result .= $r;
[258] Fix | Delete
}
[259] Fix | Delete
[260] Fix | Delete
return substr($result, 0, $length);
[261] Fix | Delete
}
[262] Fix | Delete
}
[263] Fix | Delete
[264] Fix | Delete
if (!function_exists('phpseclib_resolve_include_path')) {
[265] Fix | Delete
/**
[266] Fix | Delete
* Resolve filename against the include path.
[267] Fix | Delete
*
[268] Fix | Delete
* Wrapper around stream_resolve_include_path() (which was introduced in
[269] Fix | Delete
* PHP 5.3.2) with fallback implementation for earlier PHP versions.
[270] Fix | Delete
*
[271] Fix | Delete
* @param string $filename
[272] Fix | Delete
*
[273] Fix | Delete
* @return mixed Filename (string) on success, false otherwise.
[274] Fix | Delete
* @access public
[275] Fix | Delete
*/
[276] Fix | Delete
function phpseclib_resolve_include_path($filename)
[277] Fix | Delete
{
[278] Fix | Delete
if (function_exists('stream_resolve_include_path')) {
[279] Fix | Delete
return stream_resolve_include_path($filename);
[280] Fix | Delete
}
[281] Fix | Delete
[282] Fix | Delete
// handle non-relative paths
[283] Fix | Delete
if (file_exists($filename)) {
[284] Fix | Delete
return realpath($filename);
[285] Fix | Delete
}
[286] Fix | Delete
[287] Fix | Delete
$paths = PATH_SEPARATOR == ':' ?
[288] Fix | Delete
preg_split('#(?<!phar):#', get_include_path()) :
[289] Fix | Delete
explode(PATH_SEPARATOR, get_include_path());
[290] Fix | Delete
foreach ($paths as $prefix) {
[291] Fix | Delete
// path's specified in include_path don't always end in /
[292] Fix | Delete
$ds = substr($prefix, -1) == DIRECTORY_SEPARATOR ? '' : DIRECTORY_SEPARATOR;
[293] Fix | Delete
$file = $prefix.$ds.$filename;
[294] Fix | Delete
if (file_exists($file)) {
[295] Fix | Delete
return realpath($file);
[296] Fix | Delete
}
[297] Fix | Delete
}
[298] Fix | Delete
[299] Fix | Delete
return false;
[300] Fix | Delete
}
[301] Fix | Delete
}
[302] Fix | Delete
[303] Fix | Delete
if (!function_exists('mwp_phpseclib_resolve_include_path')) {
[304] Fix | Delete
/**
[305] Fix | Delete
* We don't rely on include_path or PHAR, so support only one option.
[306] Fix | Delete
*
[307] Fix | Delete
* @param string $filename
[308] Fix | Delete
*
[309] Fix | Delete
* @return mixed Filename (string) on success, false otherwise.
[310] Fix | Delete
* @access public
[311] Fix | Delete
*/
[312] Fix | Delete
function mwp_phpseclib_resolve_include_path($filename)
[313] Fix | Delete
{
[314] Fix | Delete
if (file_exists(dirname(__FILE__).'/../'.$filename)) {
[315] Fix | Delete
return realpath(dirname(__FILE__).'/../'.$filename);
[316] Fix | Delete
}
[317] Fix | Delete
[318] Fix | Delete
return false;
[319] Fix | Delete
}
[320] Fix | Delete
}
[321] Fix | Delete
[322] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function