【问题标题】:Cannot open source device in random_bytes() Laravel PHP 7.0无法在 random_bytes() Laravel PHP 7.0 中开源设备
【发布时间】:2017-06-06 06:24:48
【问题描述】:

我访问了很多论坛,但我找不到解决问题的最佳方法。 在 laravel 5 中,我有这个错误:

Cannot open source device in random_bytes() : 
Exception in Str.php line 243:
Cannot open source device
in Str.php line 243
at random_bytes('25') in Str.php line 243
at Str::randomBytes('25') in Str.php line 227
at Str::random('25') in Store.php line 197
at Store->generateSessionId() in Store.php line 173

str.php 位于:httpdocs/vendor/laravel/framework/src/Illuminate/Support/Str.php 在:

/**
 * Generate a more truly "random" alpha-numeric string.
 *
 * @param  int  $length
 * @return string
 */
public static function random($length = 16)
{
    $string = '';

    while (($len = strlen($string)) < $length) {
        $size = $length - $len;

        $bytes = static::randomBytes($size);

        $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
    }

    return $string;
}

/**
 * Generate a more truly "random" bytes.
 *
 * @param  int  $length
 * @return string
 */
public static function randomBytes($length = 16)
{
    return random_bytes($length);
}

我使用 php 7。我还通过 openssl_random_pseudo_bytes() 更改了 random_bytes,它可以工作,但我有另一个错误,即框架无法为密码生成哈希。所以我认为 openssl_random_bytes 不是最好的解决方案。

如果有人可以提供帮助。

谢谢

【问题讨论】:

  • 您使用的是哪个版本的 PHP 7?
  • 这 : # php -v PHP 7.0.19 (cli) (built: May 12 2017 21:01:27) (NTS) 版权所有 (c) 1997-2017 The PHP Group Zend Engine v3. 0.0,版权所有 (c) 1998-2017 Zend Technologies
  • 我遇到了类似的问题和很多段错误,我升级到 PHP 7.1.1,它们现在可以正常工作了。
  • 我安装了 PHP 7.1.5,但我有同样的错误:PHP 7.1.5 (cli) (built: May 12 2017 21:54:58) (NTS) 版权所有 (c) 1997-2017 PHP Group Zend Engine v3.1.0,版权所有 (c) 1998-2017 Zend Technologies 和 Zend OPcache v7.1.5,版权所有 (c) 1999-2017,Zend Technologies

标签: php laravel php-7


【解决方案1】:

来自random_bytesdocumentation

该函数使用的随机性来源如下:

  • 在 Windows 上,将始终使用 » CryptGenRandom()
  • 在 Linux 上,如果可用,将使用 » getrandom(2) 系统调用。
  • 在其他平台上,将使用/dev/urandom
  • 如果上述来源均不可用,则会引发异常。

所以,你的案子是最后一个。这不是您的代码问题,而是服务器设置。环境是什么?托管、云、虚拟服务器、本地计算机?

【讨论】:

  • 感谢您的回复,我使用 vps : CentOS Linux release 7.3.1611 (Core) 所以我会检查 /dev/uradom...
  • 我做了一个测试:#
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2017-04-18
  • 1970-01-01
  • 2014-08-04
  • 2019-01-18
  • 1970-01-01
相关资源
最近更新 更多