3

os.urandom or ssl.RAND_bytes: when generating random bytes for cryptographic purposes, which is more recommended?

If neither one is better, what are the tradeoffs and what is to be expected from each in terms of differences?

Note that ssl.RAND_bytes only exists in Python 3.

4

1 回答 1

2

ssl.RAND_bytes需要播种才能使用。所以你不能只靠ssl.RAND_bytes一个人。两者都是随机数生成器os.urandom。PRNG 是确定性的;当使用相同的数据播种时,它们将返回相同的伪随机数字节流。如果观察者不知道种子值,这些字节应该与真正的随机字节没有区别。然而,通常使用操作系统内的熵源(重新)播种。`ssl.RAND_bytesos.urandom

os.urandom因此应该优先使用 using ssl.RAND_bytes。首先,它已经播种(并将由操作系统重新播种)。此外,它不需要对 SSL 库的额外依赖。一个缺点可能是性能。在任何时候检索数据时都需要系统调用,因此使用ssl.RAND_bytes具有足够大值的种子os.urandom可能会更快。os.urandom

于 2013-08-07T21:32:27.183 回答