Php 查看返回的openssl_random_psuedo_bytes()字符串

Php 查看返回的openssl_random_psuedo_bytes()字符串,php,casting,return-type,php-openssl,Php,Casting,Return Type,Php Openssl,为什么不回显openssl\u random\u pseudo\u bytes(12)打印任何内容,但如果我将其与另一个字符串关联,它会显示输出?根据openssl\u random\u pseudo\u bytes的返回类型,是字符串,为什么会出现问题?我尝试用(string)对其进行类型转换,但没有成功。openssl\u random\u pseudo\u bytes(…)函数返回指定长度的字符串形式的二进制数(即ASCII值) 例如,一个可能的输出: $number_of_bytes =

为什么
不回显openssl\u random\u pseudo\u bytes(12)
打印任何内容,但如果我将其与另一个字符串关联,它会显示输出?根据
openssl\u random\u pseudo\u bytes的返回类型,
是字符串,为什么会出现问题?我尝试用
(string)
对其进行类型转换,但没有成功。
openssl\u random\u pseudo\u bytes(…)
函数返回指定长度的字符串形式的二进制数(即ASCII值)

例如,一个可能的输出:

$number_of_bytes = 1;
$bin = openssl_random_pseudo_bytes($number_of_bytes, $cstrong);
$hex=bin2hex($bin);
$dec=hexdec($hex);
可以是:

var_dump($bin); // string(1) "ã"
var_dump($hex); // string(2) "e3"
var_dump($dec); // int(227)
var_dump($cstrong); // bool(true)
注:

  • $dec
    是一个整数随机值,最多可等于2^(8*$number of_bytes)-1。
    • 其中一个字节包含8位
    • PHP有一个最多2^31-1或2^63-1位的整数溢出限制(使用4字节或8字节的有符号整数的限制,具体取决于您是使用32位还是64位平台),在此之后,它将溢出/转换为浮点值(可能限制精度)。
      • 因此,使用4(或8)字节调用时,一半的时间
        $dec
        将是一个浮点值
  • 在字节数较高时,
    $bin
    $hex
    值保持其精度和准确性(因为所有数字/位都保存在(可变长度)字符串中)
  • openssl\u random\u pseudo\u bytes
    失败时返回
    false
  • $cstrong==true
    表示
    openssl_random_pseudo_bytes
    未返回加密强算法生成的结果。()

  • 示例函数(演示如何处理
    false
    返回值或$cstrong为false时)

    手册:

    用法

    他是你的朋友。
    class Random
    {
        public static function Get($number_of_bytes=4)
        {
            $binary_value = openssl_random_pseudo_bytes($number_of_bytes, $cstrong);
    
            // Unable to produce a cryptographically strong value
            if($binary_value==false || $cstrong!==true) return false; // failure
    
            // other processing
            $hexadecimal_value = bin2hex($binary_value);
            $decimal_value = hexdec($hexadecimal_value);  
    
            // Returns a positive integer (or a float 
            // in the case of an integer overflow)
            return $decimal_value; 
        }
    }
    
    echo Random::Get(12); // returns a large float value most of the time