gpt4 book ai didi

python-3.x - 如果在每次使用之前都使用高熵种子进行播种,Python 3 random.random 的安全性是否足够好?

转载 作者:行者123 更新时间:2023-12-03 09:04:59 24 4
gpt4 key购买 nike

如果在每次使用之前都使用高熵种子进行播种,Python 3 random.random() 的安全性是否足够好?除了验证分布形状之外,如何了解和验证。

所以问题是关于使用 random.random 将一些熵源转换为数组项目内的许多选择。

举例来说,我们有一本包含 9k 个单词的字典,并且想要随机选择 18 个单词。我们不希望其他人能够通过使用 python 的 random.random() 来找到它们。

dic = ["word1", "word2", ... , "word19k"]
while(true):
seed = os.urandom(50)
random.seed(seed)
print(random.choice(dic))

最佳答案

Python documentation of random specifies :

Warning The pseudo-random generators of this module should not be used for security purposes. Use os.urandom() or SystemRandom if you require a cryptographically secure pseudo-random number generator.

正如问题评论中所述,生成随机数(用于加密目的)的更好、更安全的方法是 os.urandom()。

链接问题的示例代码

>>> import os
>>> os.urandom(10)
'm\xd4\x94\x00x7\xbe\x04\xa2R'
>>> type(os.urandom(10))
<type 'str'>
>>> map(ord, os.urandom(10))
[65, 120, 218, 135, 66, 134, 141, 140, 178, 25]

关于python-3.x - 如果在每次使用之前都使用高熵种子进行播种,Python 3 random.random 的安全性是否足够好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47773114/

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