PHP:openssl_random_pseudo_bytes() 和加密安全与极端随机性

标签 php cryptography openssl

对 PHP 中的 openssl_random_pseudo_bytes() 进行一些研究,我注意到在 PHP 源代码中 openssl_random_pseudo_bytes() 函数的实现中。 OpenSSL 的 RAND_pseudo_bytes 函数用于生成返回值,这与 OpenSSL 中也提供的 RAND_bytes 不同。

OpenSSL对这两个函数的文档如下:

RAND_pseudo_bytes() puts num pseudo-random bytes into buf. Pseudo-random byte sequences generated by RAND_pseudo_bytes() will be unique if they are of sufficient length, but are not necessarily unpredictable. They can be used for non-cryptographic purposes and for certain purposes in cryptographic protocols, but usually not for key generation etc.

RAND_bytes() puts num cryptographically strong pseudo-random bytes into buf. An error occurs if the PRNG has not been seeded with enough randomness to ensure an unpredictable byte sequence.

我想我的问题是为什么没有使用 RAND_bytes 或者为什么 PHP 中没有 openssl_rand_bytes() 函数(如果根据 OpenSSL,它更随机的话)。

只是好奇。是速度问题吗?不够可靠?还是 PRNG 是问题所在(即:当伪代码在大多数用途下都可以正常工作时,很难实现)?

谢谢

最佳答案

这个选择可能是基于实用性而不是加密货币的健全性。如果RAND_bytes()使用时,该功能可能会由于可用的随机性不足而失败。 PHP 代码的作者无疑希望避免 PHP 函数失败。

我注意到 openssl_random_pseudo_bytes()函数确实有一个可选的 crypto_strong参数,它让调用者知道返回的字节在 OpenSSL 看来是否确实具有强加密性。

顺便说一句,可以使用外部引擎配置 OpenSSL,其中一些引擎(例如 CHIL)使用基于硬件的随机源 RAND_pseudo_bytes()RAND_bytes()如果这就是您所需要的。

此外,在 Windows 上,PHP 代码使用 CryptGenRandom .

关于PHP:openssl_random_pseudo_bytes() 和加密安全与极端随机性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28113145/

相关文章:

php ssh2_exec 不执行 'su' 命令

java - 在 Android keystore 中存储 hmac key

c - 如何从密文中获取ciphertext_len以在c程序中的openssl EVP_中使用

c++ - 有效创建数字签名的正确方法是什么?我可以使用DSA_sign_setup()吗?

ssl - MyDomain.com 仅提供正向 SSL 证书的可复制文本 : one for cert and one for key. 如何在我的 nginx Web 服务器上安装它?

php - 使用php callable调用构造函数

php - 网络机器人可以增加下载量吗?

php - Gmagick 在加载 PDF 之前设置密度

c - 通过 getauxval 检测 Power8 核心加密?

.net - AES 256 加密