对 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/