123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- define('CRYPT_RANDOM_IS_WINDOWS', strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
- function crypt_random_string($length)
- {
- if (CRYPT_RANDOM_IS_WINDOWS) {
-
-
- if (function_exists('mcrypt_create_iv') && function_exists('class_alias')) {
- return mcrypt_create_iv($length);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (function_exists('openssl_random_pseudo_bytes') && version_compare(PHP_VERSION, '5.3.4', '>=')) {
- return openssl_random_pseudo_bytes($length);
- }
- } else {
-
- if (function_exists('openssl_random_pseudo_bytes')) {
- return openssl_random_pseudo_bytes($length);
- }
-
- static $fp = true;
- if ($fp === true) {
-
-
- $fp = @fopen('/dev/urandom', 'rb');
- }
- if ($fp !== true && $fp !== false) {
- return fread($fp, $length);
- }
-
-
-
-
-
- if (function_exists('mcrypt_create_iv')) {
- return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- static $crypto = false, $v;
- if ($crypto === false) {
-
- $old_session_id = session_id();
- $old_use_cookies = ini_get('session.use_cookies');
- $old_session_cache_limiter = session_cache_limiter();
- if (isset($_SESSION)) {
- $_OLD_SESSION = $_SESSION;
- }
- if ($old_session_id != '') {
- session_write_close();
- }
- session_id(1);
- ini_set('session.use_cookies', 0);
- session_cache_limiter('');
- session_start();
- $v = $seed = $_SESSION['seed'] = pack('H*', sha1(
- serialize($_SERVER) .
- serialize($_POST) .
- serialize($_GET) .
- serialize($_COOKIE) .
- serialize($GLOBALS) .
- serialize($_SESSION) .
- serialize($_OLD_SESSION)
- ));
- if (!isset($_SESSION['count'])) {
- $_SESSION['count'] = 0;
- }
- $_SESSION['count']++;
- session_write_close();
-
- if ($old_session_id != '') {
- session_id($old_session_id);
- session_start();
- ini_set('session.use_cookies', $old_use_cookies);
- session_cache_limiter($old_session_cache_limiter);
- } else {
- if (isset($_OLD_SESSION)) {
- $_SESSION = $_OLD_SESSION;
- unset($_OLD_SESSION);
- } else {
- unset($_SESSION);
- }
- }
-
-
-
-
-
-
-
-
- $key = pack('H*', sha1($seed . 'A'));
- $iv = pack('H*', sha1($seed . 'C'));
-
-
-
- switch (true) {
- case class_exists('Crypt_AES'):
- $crypto = new Crypt_AES(CRYPT_AES_MODE_CTR);
- break;
- case class_exists('Crypt_TripleDES'):
- $crypto = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);
- break;
- case class_exists('Crypt_DES'):
- $crypto = new Crypt_DES(CRYPT_DES_MODE_CTR);
- break;
- case class_exists('Crypt_RC4'):
- $crypto = new Crypt_RC4();
- break;
- default:
- $crypto = $seed;
- return crypt_random_string($length);
- }
- $crypto->setKey($key);
- $crypto->setIV($iv);
- $crypto->enableContinuousBuffer();
- }
- if (is_string($crypto)) {
-
-
-
-
-
-
-
-
-
-
-
- $result = '';
- while (strlen($result) < $length) {
-
-
- $i = pack('H*', sha1(microtime()));
- $r = pack('H*', sha1($i ^ $v));
- $v = pack('H*', sha1($r ^ $i));
- $result.= $r;
- }
- return substr($result, 0, $length);
- }
-
- $result = '';
- while (strlen($result) < $length) {
- $i = $crypto->encrypt(microtime());
- $r = $crypto->encrypt($i ^ $v);
- $v = $crypto->encrypt($r ^ $i);
- $result.= $r;
- }
- return substr($result, 0, $length);
- }
|