本文整理汇总了PHP中random_bytes_emulate函数的典型用法代码示例。如果您正苦于以下问题:PHP random_bytes_emulate函数的具体用法?PHP random_bytes_emulate怎么用?PHP random_bytes_emulate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了random_bytes_emulate函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: complex_random_string
/**
* Generate a complex random string (useful for md5 salts)
*
* This function is based on the above {@link random_string()} however it uses a
* larger pool of characters and generates a string between 24 and 32 characters
*
* @param int $length Optional if set generates a string to exactly this length
* @return string
*/
function complex_random_string($length = null)
{
$pool = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$pool .= '`~!@#%^&*()_+-=[];,./<>?:{} ';
$poollen = strlen($pool);
if ($length === null) {
$length = floor(rand(24, 32));
}
$randombytes = random_bytes_emulate($length);
$string = '';
for ($i = 0; $i < $length; $i++) {
$rand = ord($randombytes[$i]);
$string .= $pool[$rand % $poollen];
}
return $string;
}
示例2: test_random_bytes_emulate
public function test_random_bytes_emulate()
{
$result = random_bytes_emulate(10);
$this->assertSame(10, strlen($result));
$this->assertnotSame($result, random_bytes_emulate(10));
$result = random_bytes_emulate(21);
$this->assertSame(21, strlen($result));
$this->assertnotSame($result, random_bytes_emulate(21));
$result = random_bytes_emulate(666);
$this->assertSame(666, strlen($result));
$this->assertDebuggingNotCalled();
$result = random_bytes_emulate(0);
$this->assertSame('', $result);
$this->assertDebuggingCalled();
$result = random_bytes_emulate(-1);
$this->assertSame('', $result);
$this->assertDebuggingCalled();
}
示例3: random_bytes_emulate
/**
* Try to generates cryptographically secure pseudo-random bytes.
*
* Note this is achieved by fallbacking between:
* - PHP 7 random_bytes().
* - OpenSSL openssl_random_pseudo_bytes().
* - In house random generator getting its entropy from various, hard to guess, pseudo-random sources.
*
* @param int $length requested length in bytes
* @return string binary data
*/
function random_bytes_emulate($length)
{
global $CFG;
if ($length <= 0) {
debugging('Invalid random bytes length', DEBUG_DEVELOPER);
return '';
}
if (function_exists('random_bytes')) {
// Use PHP 7 goodness.
$hash = @random_bytes($length);
if ($hash !== false) {
return $hash;
}
}
if (function_exists('openssl_random_pseudo_bytes')) {
// For PHP 5.3 and later with openssl extension.
$hash = openssl_random_pseudo_bytes($length);
if ($hash !== false) {
return $hash;
}
}
// Bad luck, there is no reliable random generator, let's just hash some unique stuff that is hard to guess.
$hash = sha1(serialize($CFG) . serialize($_SERVER) . microtime(true) . uniqid('', true), true);
// NOTE: the last param in sha1() is true, this means we are getting 20 bytes, not 40 chars as usual.
if ($length <= 20) {
return substr($hash, 0, $length);
}
return $hash . random_bytes_emulate($length - 20);
}
示例4: create_big_files
/**
* Creates a number of resource activities with one big file each.
*/
private function create_big_files()
{
// Work out how many files and how many blocks to use (up to 64KB).
$count = self::$parambigfilecount[$this->size];
$filesize = $this->limit_filesize(self::$parambigfilesize[$this->size]);
$blocks = ceil($filesize / 65536);
$blocksize = floor($filesize / $blocks);
$this->log('createbigfiles', $count, true);
// Prepare temp area.
$tempfolder = make_temp_directory('tool_generator');
$tempfile = $tempfolder . '/' . rand();
// Create resources and files.
$fs = get_file_storage();
$resourcegenerator = $this->generator->get_plugin_generator('mod_resource');
for ($i = 0; $i < $count; $i++) {
// Create resource.
$record = array('course' => $this->course, 'name' => get_string('bigfile', 'tool_generator', $i));
$options = array('section' => $this->get_target_section());
$resource = $resourcegenerator->create_instance($record, $options);
// Write file.
$handle = fopen($tempfile, 'w');
if (!$handle) {
throw new coding_exception('Failed to open temporary file');
}
for ($j = 0; $j < $blocks; $j++) {
$data = random_bytes_emulate($blocksize);
fwrite($handle, $data);
$this->dot($i * $blocks + $j, $count * $blocks);
}
fclose($handle);
// Add file.
$context = context_module::instance($resource->cmid);
$filerecord = array('component' => 'mod_resource', 'filearea' => 'content', 'contextid' => $context->id, 'itemid' => 0, 'filepath' => '/', 'filename' => 'bigfile' . $i . '.dat');
$fs->create_file_from_pathname($filerecord, $tempfile);
}
unlink($tempfile);
$this->end_log();
}