gpt4 book ai didi

java - 在 Java 中重新实现 openssl_random_pseudo_bytes 函数的正确方法

转载 作者:行者123 更新时间:2023-11-30 08:34:14 26 4
gpt4 key购买 nike

我需要根据其 description 重新实现 PHP 函数 openssl_random_pseudo_bytes 的行为:

openssl_random_pseudo_bytes — Generate a pseudo-random string of bytes

因为它只是伪随机字节串,而且我需要它的十六进制表示,所以我可以只使用this function .但还有额外的参数:

crypto_strong

If passed into the function, this will hold a boolean value that determines if the algorithm used was "cryptographically strong", e.g., safe for usage with GPG, passwords, etc. TRUE if it did, otherwise FALSE

所以我的问题的核心是:这个 crypto_strong 参数到底是什么?我应该如何正确地重新实现此行为?

注意!我可以为此目的只使用 java.security.SecureRandom() 类吗?

最佳答案

函数openssl_random_pseudo_bytes($length, &$crypto_strong)在第二个参数中指示接收生成的字节是否具有加密强度。

此代码似乎用作:

openssl_random_pseudo_bytes(XX, true)

这没有意义,因为它似乎被视为输入参数而不是输出参数

在(嵌入式)设备上没有良好的熵源来播种 RNG,可能是 crypto_strong 参数被函数设置为 false。显然不可能将 true 设置为 false 所以这将失败;也就是说,这是 PHP 代码中的一个错误(显然没有被 PHP 本身相当宽松的类型系统捕获)。


PHP 的程序员可能试图表明需要加密强度高的 PRNG。所以 new SecureRandom() 似乎确实是最好的方法。

关于java - 在 Java 中重新实现 openssl_random_pseudo_bytes 函数的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38953599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com