本文整理汇总了PHP中COM::GetRandom方法的典型用法代码示例。如果您正苦于以下问题:PHP COM::GetRandom方法的具体用法?PHP COM::GetRandom怎么用?PHP COM::GetRandom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类COM
的用法示例。
在下文中一共展示了COM::GetRandom方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: filter
public function filter($value)
{
//source: http://www.php-security.org/2010/05/09/mops-submission-04-generating-unpredictable-session-ids-and-hashes/
$entropy = '';
// try ssl first
if (function_exists('openssl_random_pseudo_bytes')) {
$entropy = openssl_random_pseudo_bytes(64, $strong);
// skip ssl since it wasn't using the strong algo
if ($strong !== true) {
$entropy = '';
}
}
// add some basic mt_rand/uniqid combo
$entropy .= uniqid(mt_rand(), true);
// try to read from the windows RNG
if (class_exists('COM')) {
try {
$com = new COM('CAPICOM.Utilities.1');
$entropy .= base64_decode($com->GetRandom(64, 0));
} catch (Exception $ex) {
}
}
// try to read from the unix RNG
if (is_readable('/dev/urandom')) {
$h = fopen('/dev/urandom', 'rb');
$entropy .= fread($h, 64);
fclose($h);
}
$hash = hash('whirlpool', $entropy);
return substr($hash, 0, $this->_length);
}
示例2: generateUniqueId
function generateUniqueId($maxLength = null)
{
$entropy = '';
// On test ssl d'abord.
if (function_exists('openssl_random_pseudo_bytes')) {
$entropy = openssl_random_pseudo_bytes(64, $strong);
// skip ssl since it wasn't using the strong algo
if ($strong !== true) {
$entropy = '';
}
}
// On ajoute les basics mt_rand/uniqid combo
$entropy .= uniqid(mt_rand(), true);
// On test la lecture de la fenêtre RNG
if (class_exists('COM')) {
try {
$com = new COM('CAPICOM.Utilities.1');
$entropy .= base64_decode($com->GetRandom(64, 0));
} catch (Exception $ex) {
}
}
// on test la lecture de unix RNG
if (is_readable('/dev/urandom')) {
$h = fopen('/dev/urandom', 'rb');
$entropy .= fread($h, 64);
fclose($h);
}
$hash = hash('whirlpool', $entropy);
if ($maxLength) {
return substr($hash, 0, $maxLength);
}
return $hash;
}
示例3: generate
public static function generate($maxLength = null)
{
$entropy = '';
// try ssl first
if (function_exists('openssl_random_pseudo_bytes')) {
$entropy = openssl_random_pseudo_bytes(64, $strong);
// skip ssl since it wasn't using the strong algo
if ($strong !== true) {
$entropy = '';
}
}
// add some basic mt_rand/uniqid combo
$entropy .= uniqid(mt_rand(), true);
// try to read from the windows RNG
if (class_exists('COM')) {
try {
$com = new COM('CAPICOM.Utilities.1');
$entropy .= base64_decode($com->GetRandom(64, 0));
} catch (Exception $ex) {
}
}
// try to read from the unix RNG
if (is_readable('/dev/urandom')) {
$h = fopen('/dev/urandom', 'rb');
$entropy .= fread($h, 64);
fclose($h);
}
$hash = hash('whirlpool', $entropy);
if ($maxLength) {
return substr($hash, 0, $maxLength);
}
return $hash;
}
示例4: getRandomBytes
function getRandomBytes($count)
{
$output = '';
// we will try to obtain entropy from several sources, starting with OpenSSL
if (function_exists('openssl_random_pseudo_bytes')) {
$strong = FALSE;
$output = openssl_random_pseudo_bytes($count, $strong);
// if OpenSSL didn't use a strong cryptographic primitive, we'll find another source of entropy
if (FALSE == $strong) {
$output = '';
}
}
// if we've got a POSIX system, hopefully urandom is available
if ($fd = @fopen('/dev/urandom', 'rb')) {
$output = fread($fd, $count);
fclose($fd);
}
// if we're on Windows, hopefully we can use its PRNG
if (class_exists('COM')) {
@($com = new COM('CAPICOM.Utilities.1'));
@($output .= base64_decode($com->GetRandom($count, 0)));
}
// we fall back to a rather cryptographically insufficient but workable source of entropy
if (strlen($output) < $count) {
$output = '';
for ($i = 0; $i < $count; $i += 16) {
$this->randomState = md5(microtime() . $this->randomState);
$output .= md5($this->randomState, TRUE);
}
$output = substr($output, 0, $count);
}
return $output;
}
示例5: random_bytes
/**
* Windows with PHP < 5.3.0 will not have the function
* openssl_random_pseudo_bytes() available, so let's use
* CAPICOM to work around this deficiency.
*
* @param int $bytes
*
* @throws Exception
*
* @return string
*/
function random_bytes($bytes)
{
if (!is_int($bytes)) {
throw new TypeError('Length must be an integer');
}
if ($bytes < 1) {
throw new Error('Length must be greater than 0');
}
$buf = '';
$util = new COM('CAPICOM.Utilities.1');
$execCount = 0;
/**
* Let's not let it loop forever. If we run N times and fail to
* get N bytes of random data, then CAPICOM has failed us.
*/
do {
$buf .= base64_decode($util->GetRandom($bytes, 0));
if (RandomCompat_strlen($buf) >= $bytes) {
/**
* Return our random entropy buffer here:
*/
return RandomCompat_substr($buf, 0, $bytes);
}
++$execCount;
} while ($execCount < $bytes);
/**
* If we reach here, PHP has failed us.
*/
throw new Exception('PHP failed to generate random data.');
}
示例6: zen_get_entropy
function zen_get_entropy($seed)
{
$entropy = '';
$fp = @fopen('/dev/urandom', 'rb');
if ($fp !== FALSE) {
$entropy .= @fread($fp, 16);
// echo "USING /dev/random" . "<br>";
@fclose($fp);
}
// MS-Windows platform?
if (@class_exists('COM')) {
// http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx
try {
$CAPI_Util = new COM('CAPICOM.Utilities.1');
$entropy .= $CAPI_Util->GetRandom(16, 0);
if ($entropy) {
$entropy = md5($entropy, TRUE);
// echo "USING WINDOWS" . "<br>";
}
} catch (Exception $ex) {
// echo 'Exception: ' . $ex->getMessage();
}
}
if (strlen($entropy) < 16) {
$entropy = sha1_file('/includes/configure.php');
$entropy .= microtime() . mt_rand() . $seed;
// echo "USING FALLBACK" . "<br>";
}
return sha1($entropy);
}
示例7: generate
/**
* Generate a random string of the specified size
*
* @param int $size The size of the requested random string
*
* @return string A string of the requested size
*/
public function generate($size)
{
try {
$util = new \COM('CAPICOM.Utilities.1');
$data = base64_decode($util->GetRandom($size, 0));
return str_pad($data, $size, chr(0));
} catch (\Exception $e) {
unset($e);
return static::emptyValue($size);
}
}
示例8: RandomBytes
public static function RandomBytes($count, $printable = FALSE)
{
$bytes = '';
// supress warnings when open_basedir restricts access to /dev/urand
if (@is_readable('/dev/urandom') && ($hRand = @fopen('/dev/urandom', 'rb')) !== FALSE) {
$bytes = fread($hRand, $count);
fclose($hRand);
}
if (strlen($bytes) < $count && function_exists('mcrypt_create_iv')) {
// Use MCRYPT_RAND on Windows hosts with PHP < 5.3.7, otherwise use MCRYPT_DEV_URANDOM
// (http://bugs.php.net/55169).
if (version_compare(PHP_VERSION, '5.3.7', '<') && strncasecmp(PHP_OS, 'WIN', 3) == 0) {
$bytes = mcrypt_create_iv($count, MCRYPT_RAND);
} else {
$bytes = mcrypt_create_iv($count, MCRYPT_DEV_URANDOM);
}
}
if (strlen($bytes) < $count && function_exists('openssl_random_pseudo_bytes')) {
$bytes = openssl_random_pseudo_bytes($count);
}
if (strlen($bytes) < $count && @class_exists('COM')) {
// Officially deprecated in Windows 7
// http://msdn.microsoft.com/en-us/library/aa388182%28v=vs.85%29.aspx
try {
$CAPI_Util = new COM('CAPICOM.Utilities.1');
if (is_callable(array($CAPI_Util, 'GetRandom'))) {
$bytes = $CAPI_Util->GetRandom(16, 0);
$bytes = base64_decode($bytes);
}
} catch (Exception $ex) {
}
}
if (strlen($bytes) < $count) {
// This fallback here based on phpass code
$bytes = '';
$random_state = microtime();
if (function_exists('getmypid')) {
$random_state .= getmypid();
}
for ($i = 0; $i < $count; $i += 16) {
$random_state = md5(microtime() . $random_state);
$bytes .= pack('H*', md5($random_state));
}
$bytes = substr($bytes, 0, $count);
}
if ($printable) {
return base64_encode($bytes);
} else {
return $bytes;
}
}
示例9: generate
/**
* Generate a random string of the specified size
*
* @param int $size The size of the requested random string
*
* @return string A string of the requested size
*/
public function generate($size)
{
if (!class_exists('\\COM', false)) {
return str_repeat(chr(0), $size);
}
try {
$util = new \COM('CAPICOM.Utilities.1');
$data = base64_decode($util->GetRandom($size, 0));
return str_pad($data, $size, chr(0));
} catch (\Exception $e) {
unset($e);
return str_repeat(chr(0), $size);
}
}
示例10: randomBytes
public function randomBytes($byteLength)
{
if (function_exists('openssl_random_pseudo_bytes')) {
$data = openssl_random_pseudo_bytes($byteLength);
} elseif (is_readable('/dev/urandom')) {
$fp = @fopen('/dev/urandom', 'rb');
if ($fp !== false) {
$data = fread($fp, $byteLength);
fclose($fp);
}
} elseif (class_exists('COM')) {
// @TODO: Someone care to test on Windows? Not it!
try {
$capi = new COM('CAPICOM.Utilities.1');
$data = $capi->GetRandom($btyeLength, 0);
} catch (Exception $ex) {
// Fail silently
}
}
return $data;
}
示例11: generateEntropy
/**
* Note: Returned values are not guaranteed to be crypto-safe,
* depending on the used retrieval method.
*
* @return string Returns a random series of bytes
*/
public function generateEntropy()
{
$isWin = preg_match('/WIN/', PHP_OS);
// TODO Fails with "Could not gather sufficient random data" on IIS, temporarily disabled on windows
if (!$isWin) {
if (function_exists('mcrypt_create_iv')) {
$e = mcrypt_create_iv(64, MCRYPT_DEV_URANDOM);
if ($e !== false) {
return $e;
}
}
}
// Fall back to SSL methods - may slow down execution by a few ms
if (function_exists('openssl_random_pseudo_bytes')) {
$e = openssl_random_pseudo_bytes(64, $strong);
// Only return if strong algorithm was used
if ($strong) {
return $e;
}
}
// Read from the unix random number generator
if (!$isWin && !ini_get('open_basedir') && is_readable('/dev/urandom') && ($h = fopen('/dev/urandom', 'rb'))) {
$e = fread($h, 64);
fclose($h);
return $e;
}
// Warning: Both methods below are considered weak
// try to read from the windows RNG
if ($isWin && class_exists('COM')) {
try {
$comObj = new COM('CAPICOM.Utilities.1');
if (is_callable(array($comObj, 'GetRandom'))) {
return base64_decode($comObj->GetRandom(64, 0));
}
} catch (Exception $ex) {
}
}
// Fallback to good old mt_rand()
return uniqid(mt_rand(), true);
}
示例12: zen_get_entropy
/**
* Returns entropy using a hash of various available methods for obtaining
* random data.
* The default hash method is "sha1" and the default size is 32.
*
* @param string $hash
* the hash method to use while generating the hash.
* @param int $size
* the size of random data to use while generating the hash.
* @return string the randomized salt
*/
function zen_get_entropy($hash = 'sha1', $size = 32)
{
$data = null;
if (!in_array($hash, hash_algos())) {
$hash = 'sha1';
}
if (!is_int($size)) {
$size = (int) $size;
}
// Use openssl if available
if (function_exists('openssl_random_pseudo_bytes')) {
// echo('Attempting to create entropy using openssl');
$entropy = openssl_random_pseudo_bytes($size, $strong);
if ($strong) {
$data = $entropy;
}
unset($strong, $entropy);
}
// Use mcrypt with /dev/urandom if available
if ($data === null && function_exists('mcrypt_create_iv') && (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' || version_compare(PHP_VERSION, '5.3.7', '>='))) {
// echo('Attempting to create entropy using mcrypt');
$entropy = mcrypt_create_iv($size, MCRYPT_DEV_URANDOM);
if ($entropy !== FALSE) {
$data = $entropy;
}
unset($entropy);
}
if ($data === null) {
// Fall back to using /dev/urandom if available
$fp = @fopen('/dev/urandom', 'rb');
if ($fp !== FALSE) {
// echo('Attempting to create entropy using /dev/urandom');
$entropy = @fread($fp, $size);
@fclose($fp);
if (strlen($entropy) == $size) {
$data = $entropy;
}
unset($fp, $entropy);
}
}
// Final fallback (mixture of various methods)
if ($data === null) {
// echo('Attempting to create entropy using FINAL FALLBACK');
if (!defined('DIR_FS_ROOT')) {
define('DIR_FS_ROOT', DIR_FS_CATALOG);
}
$filename = DIR_FS_ROOT . 'includes/configure.php';
$stat = @stat($filename);
if ($stat === FALSE) {
$stat = array('microtime' => microtime());
}
$stat['mt_rand'] = mt_rand();
$stat['file_hash'] = hash_file($hash, $filename, TRUE);
// Attempt to get a random value on windows
// http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx
if (@class_exists('COM')) {
try {
$CAPI_Util = new COM('CAPICOM.Utilities.1');
$entropy = $CAPI_Util->GetRandom($size, 0);
if ($entropy) {
// echo('Adding random data to entropy using CAPICOM.Utilities');
$stat['CAPICOM_Utilities_random'] = md5($entropy, TRUE);
}
unset($CAPI_Util, $entropy);
} catch (Exception $ex) {
}
}
// echo('Adding random data to entropy using file information and contents');
@shuffle($stat);
foreach ($stat as $value) {
$data .= $value;
}
unset($filename, $value, $stat);
}
return hash($hash, $data);
}
示例13: COM
/**
* Windows with PHP < 5.3.0 will not have the function
* openssl_random_pseudo_bytes() available, so let's use
* CAPICOM to work around this deficiency.
*
* @param int $bytes
* @return string
*/
function random_bytes($bytes)
{
try {
$buf = '';
$util = new COM('CAPICOM.Utilities.1');
$execs = 0;
/**
* Let's not let it loop forever. If we run N times and fail to
* get N bytes of random data, then CAPICOM has failed us.
*/
do {
$buf .= base64_decode($util->GetRandom($bytes, 0));
if (RandomCompat_strlen($buf) >= $bytes) {
return RandomCompat_substr($buf, 0, $bytes);
}
++$execs;
} while ($execs < $bytes);
} catch (Exception $e) {
unset($e);
// Let's not let CAPICOM errors kill our app
}
throw new Exception('PHP failed to generate random data.');
}
示例14: generate_random_string
function generate_random_string($chars = 16)
{
if (defined('USE_POOR_RANDOMS')) {
$options = array_merge(range('a', 'b'), range('A', 'Z'), range(0, 9));
$res = '';
for ($i = 0; $i < $chars; $i++) {
$res .= $options[rand(0, count($options) - 1)];
}
return $res;
}
$pr_bits = '';
if (function_exists('openssl_random_pseudo_bytes')) {
$pr_bits = openssl_random_pseudo_bytes($chars);
} else {
// Unix/Linux platform?
$fp = @fopen('/dev/urandom', 'rb');
if ($fp !== FALSE) {
$pr_bits .= @fread($fp, $chars);
@fclose($fp);
}
// MS-Windows platform?
if (@class_exists('COM')) {
// http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx
try {
$CAPI_Util = new COM('CAPICOM.Utilities.1');
$pr_bits .= $CAPI_Util->GetRandom($chars, 0);
// if we ask for binary data PHP munges it, so we
// request base64 return value. We squeeze out the
// redundancy and useless ==CRLF by hashing...
if ($pr_bits) {
$pr_bits = md5($pr_bits, TRUE);
}
} catch (Exception $ex) {
// echo 'Exception: ' . $ex->getMessage();
}
}
}
if (empty($pr_bits)) {
trigger_error("Could not generate random string", E_USER_ERROR);
}
if (strlen($pr_bits) < $chars) {
trigger_error("Generated random string not long enough (only " . strlen($pr_bits));
}
$validChars = array_merge(range(0, 9), range('A', 'Z'), range('a', 'z'));
for ($i = 0; $i < strlen($pr_bits); $i++) {
if (!preg_match('/[A-Za-z0-9]/', $pr_bits[$i])) {
$pr_bits[$i] = $validChars[ord($pr_bits[$i]) % count($validChars)];
}
}
return $pr_bits;
}
示例15: auth_randombytes
/**
* Return truly (pseudo) random bytes if available, otherwise fall back to mt_rand
*
* @author Mark Seecof
* @author Michael Hamann <michael@content-space.de>
* @link http://www.php.net/manual/de/function.mt-rand.php#83655
* @param int $length number of bytes to get
* @return string binary random strings
*/
function auth_randombytes($length)
{
$strong = false;
$rbytes = false;
if (function_exists('openssl_random_pseudo_bytes') && (version_compare(PHP_VERSION, '5.3.4') >= 0 || strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) {
$rbytes = openssl_random_pseudo_bytes($length, $strong);
}
if (!$strong && function_exists('mcrypt_create_iv') && (version_compare(PHP_VERSION, '5.3.7') >= 0 || strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) {
$rbytes = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
if ($rbytes !== false && strlen($rbytes) === $length) {
$strong = true;
}
}
// If no strong randoms available, try OS the specific ways
if (!$strong) {
// Unix/Linux platform
$fp = @fopen('/dev/urandom', 'rb');
if ($fp !== false) {
$rbytes = fread($fp, $length);
fclose($fp);
}
// MS-Windows platform
if (class_exists('COM')) {
// http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx
try {
$CAPI_Util = new COM('CAPICOM.Utilities.1');
$rbytes = $CAPI_Util->GetRandom($length, 0);
// if we ask for binary data PHP munges it, so we
// request base64 return value.
if ($rbytes) {
$rbytes = base64_decode($rbytes);
}
} catch (Exception $ex) {
// fail
}
}
}
if (strlen($rbytes) < $length) {
$rbytes = false;
}
// still no random bytes available - fall back to mt_rand()
if ($rbytes === false) {
$rbytes = '';
for ($i = 0; $i < $length; ++$i) {
$rbytes .= chr(mt_rand(0, 255));
}
}
return $rbytes;
}