首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

openssl_random_pseudo_bytes

(PHP 5 >= 5.3.0, PHP 7)

openssl_random_pseudo_bytes — Generate a pseudo-random string of bytes

描述

代码语言:javascript
复制
string openssl_random_pseudo_bytes ( int $length [, bool &$crypto_strong ] )

生成一串伪随机字节,其中由length参数确定的字节数。

它还指示是否使用密码强的算法来生成伪随机字节,并通过可选crypto_strong参数执行此操作。这很少见FALSE,但有些系统可能会破坏或老化。

参数

length

所需字节串的长度。必须是正整数。PHP会尝试将这个参数转换为一个非空整数来使用。

crypto_strong

如果传递到函数中,它将保存一个布尔值,该值确定所用算法是否“密码学上强大”,例如,对于GPG,密码等使用是安全的TRUE,否则FALSE

返回值

成功时返回生成的字节串或失败时返回FALSE

示例

Example #1 openssl_random_pseudo_bytes() example

代码语言:javascript
复制
<?php
for?($i?=?-1;?$i?<=?4;?$i++)?{
????$bytes?=?openssl_random_pseudo_bytes($i,?$cstrong);
????$hex???=?bin2hex($bytes);

????echo?"Lengths:?Bytes:?$i?and?Hex:?"?.?strlen($hex)?.?PHP_EOL;
????var_dump($hex);
????var_dump($cstrong);
????echo?PHP_EOL;
}
?>

上面的例子会输出类似于:

代码语言:javascript
复制
Lengths: Bytes: -1 and Hex: 0
string(0) ""
NULL

Lengths: Bytes: 0 and Hex: 0
string(0) ""
NULL

Lengths: Bytes: 1 and Hex: 2
string(2) "42"
bool(true)

Lengths: Bytes: 2 and Hex: 4
string(4) "dc6e"
bool(true)

Lengths: Bytes: 3 and Hex: 6
string(6) "288591"
bool(true)

Lengths: Bytes: 4 and Hex: 8
string(8) "ab86d144"
bool(true)

See Also

  • random_bytes() - 生成密码安全的伪随机字节
  • bin2hex() - 将二进制数据转换为十六进制表示
  • crypt() - 单向字符串散列
  • mt_rand() - 通过Mersenne Twister随机数生成器生成一个随机值
  • uniqid() - 生成一个唯一的ID

← openssl_public_encrypt

openssl_seal →

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com