当前位置: 首页>>代码示例>>PHP>>正文


PHP random_bytes_emulate函数代码示例

本文整理汇总了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;
}
开发者ID:lucaboesch,项目名称:moodle,代码行数:25,代码来源:moodlelib.php

示例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();
 }
开发者ID:alanaipe2015,项目名称:moodle,代码行数:18,代码来源:moodlelib_test.php

示例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);
}
开发者ID:aleph-n,项目名称:lms.aaenl,代码行数:40,代码来源:moodlelib.php

示例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();
 }
开发者ID:evltuma,项目名称:moodle,代码行数:41,代码来源:course_backend.php


注:本文中的random_bytes_emulate函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。