适用于 Windows 平台的安全随机数,例如 /dev/random 中的整数?
在我的应用程序中,我需要一个函数来生成(不可预测的)随机值,这些值每次调用时都不同,例如在快速循环内。
在 Linux 平台上,我将发布我的脚本(该脚本将在 PHP 中的 SSL 下运行),我将通过查询 /dev/random(可能与 OpenSSL 结合)结合可能的多个设施来确保种子或哈希完全随机。设施并包括系统特定的值,例如脚本的上次修改时间和创建时间。
我正在使用这些特定值,因为即使人 A 拥有脚本并知道方法,他们也无法猜测(/dev/random 内容、当前内存使用情况、可能的修改时间等),并且不会实际上能够降低用户 B 运行相同脚本的安全性。
不幸的是,我目前必须在 Windows 平台上进行开发(我仍然在 Linux 上进行测试,但频率较低),我需要上面描述的随机值,只是为了提供至少有限的保护来防止预测种子或密钥。
我第一次尝试使用 memory_get_usage()
(有或没有可用的 true
参数来表示 PHP 的“真实”内存使用情况),并且这些值似乎仍然非常静态即使每次迭代执行大量内存繁重计算。
使用这个(有点动态的)内存使用作为种子,让 PRNG 生成更多(快速)随机数可能是明智的吗?或者事实是,内存的范围如此有限,他们只能创建 2^xx 种子并粗略地猜测它。我开始模糊现实随机的界限,如果甚至有可能猜测我的操作,即使它们真的“不”那么随机。
In my application, I require a function to generate (unpredictably) random values that differ each time when called such as inside a fast loop.
On Linux platforms which is the platform I will release my script (of which shall be run under SSL in PHP) I will combine possibly multiple facilities to ensure a seed or hash is completely random, by querying /dev/random, possibly combined with OpenSSL's facilities and including system-specific values such as script last modified and creation time.
I am using these specific values, as even if person A had the script and knows the methods, they would not be able to guess the (/dev/random contents, memory usage at the moment, modification time likely, etc) and will not realistically be able to reduce the security of user B running the same script.
On the Windows platform which unfortunately I must develop on for the moment (I still test on Linux, but less often) I require random values of which I described above, just to provided at least limited protection from predicting the seeds or keys.
I had tried as a first attempt using memory_get_usage()
(with or without available true
parameter for 'true' memory usage for PHP) and it seems that the values remain very static even when each iteration performs a fair amount of memory heavy computation.
Would it maybe be wise to use this (somewhat dynamic) memory usage as a seed, for a PRNG to generate more (quickly) random numbers? Or would the fact that memory is such a limited range they could just create 2^xx seeds and roughly guess it.. I am starting to blur the line of what is realistically random, if it is even possible to guess my operations even if they are 'not' really that random.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
相当于
/dev/random
的标准(或一般推荐/dev/urandom
) Windows 上的 Unix 设备是CryptGenRandom
函数。在 PHP 中,您应该能够将
mcrypt_create_iv()
与 < code>MCRYPT_DEV_URANDOM,在 Unix 上使用/dev/urandom
,在 Windows 上使用(显然)CryptGenRandom
。The standard equivalent of the
/dev/random
(or the generally recommended/dev/urandom
) Unix device on Windows is theCryptGenRandom
function from CryptoAPI.In PHP, you should be able to use
mcrypt_create_iv()
withMCRYPT_DEV_URANDOM
, which uses/dev/urandom
on Unix and (apparently)CryptGenRandom
on Windows.Mersenne Twister(mt_rand 使用的算法)对于非安全目的来说是一个很好的算法,但它不应该用于安全目的。 维基百科:Mersenne Twister:
“该算法的本机形式不适合密码学……观察足够数量的迭代(MT19937 中为 624 次)允许人们预测所有未来的迭代。”
相反,只需获取计数器的输出,将其与一些盐连接(或异或),然后使用 SHA-2 等加密安全哈希算法对其进行哈希处理即可。如果没有人知道你的盐,那么它绝对是安全的。那么盐就相当于梅森的种子。
我不是 Windows 上哪里可以获得好的随机盐的专家,但您始终可以连接(或异或)诸如系统时间、内存使用情况等内容,并使用 SHA-2 对其进行哈希处理。您甚至可以到 Random.org 之类的地方获取一些真正的随机数(如果您不要经常调用它)。将随机源与 SHA-2 相结合的最佳之处在于,每个附加源只能增加随机性,而不能减少随机性。
A Mersenne Twister (what mt_rand uses) is a good algorithm for non-security purposes but it shouldn't be used for security. Wikipedia: Mersenne Twister:
"The algorithm in its native form is not suitable for cryptography... Observing a sufficient number of iterates (624 in the case of MT19937) allows one to predict all future iterates."
Instead it's just as simple to just take the output of a counter, concatenate (or XOR) it with some salt, and hash it with a cryptographically secure hash algorithm like SHA-2. If no one knows your salt, it will be absolutely secure. The salt is then equivalent to Mersenne's seed.
I'm no expert on where to get good random salt on Windows, but you can always concatenate (or XOR) things like system time, memory usage, etc, and hash that with SHA-2. You can even reach outside to a place like Random.org for some true random numbers (if you don't call it too often). The best part about combining sources of randomness with SHA-2 is that every additional source can only add randomness, not subtract it.
为什么不直接使用类似的东西呢?
更多信息在这里: http://php.net/manual/en/function.mt -rand.php
Why not just use something like?
More info here: http://php.net/manual/en/function.mt-rand.php