【问题标题】:PHP random_int(0,63) : Cannot open source devicePHP random_int(0,63):无法开源设备
【发布时间】:2017-02-04 14:12:06
【问题描述】:

这个 PHP 代码:

<?php
    random_int(0,63);
?>

失败并出现此错误:

致命错误:未捕获的异常:无法在 /usr/htdocs/rand.php:2 中打开源设备

堆栈跟踪:

#0 /usr/htdocs/rand.php(2): random_int(0, 2)

#1 {main} 在第 2 行的 /usr/htdocs/rand.php 中抛出

由于这个错误,我无法设置我的 nextcloud 服务器...请问有什么问题?

【问题讨论】:

标签: php nextcloud


【解决方案1】:

来自https://secure.php.net/manual/en/function.random-int.php

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

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

关于列表中的算法:

  • CryptGenRandom 是一种基于软件的伪随机算法
  • getrandom(2) 和 /dev/urandom 从机器上物理设备上的噪声中获取随机性

在您的托管情况下,如果可用,则没有,因此会抛出 Exception(我以前从未见过这种情况,这可能意味着您的托管服务提供商不是很好)。

你可以使用的,我推荐的,而不是random_int,是openssl_random_pseudo_bytes。它需要编译Openssl库,这是一种规范。

注意;上述函数返回二进制结果,您可以使用bin2hex轻松将其转换为十六进制,或使用hexdec将十六进制转换为int。

例子:

$int = hexdec(bin2hex(openssl_random_pseudo_bytes(1, $strong))); // 1 byte int
$int = hexdec(bin2hex(openssl_random_pseudo_bytes(2, $strong))); // 2 byte int

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2017-04-18
    • 2017-10-28
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 2022-06-19
    相关资源
    最近更新 更多