在 Python 中,secrets 模块如何制作完美的随机序列 [英] What does secrets module do to make perfect random sequences in Python

查看:43
本文介绍了在 Python 中,secrets 模块如何制作完美的随机序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我对数学有了很好的了解,并且我知道可以使用特定的数学算法创建伪随机序列.我也知道在 Python 中,有一个 secrets 模块显然可以产生随机数.我试着稍微调整一下,但我仍然不明白它应该如何工作.让我们说这段代码:

Now I have a decent knowledge of math, and I know it's possible to create pseudo-random sequences using a specific mathematics algorithm. I also know that in Python, there is a secrets module that apparently can produce random numbers. I tried tweaking around with it a little, but I still don't understand how it's supposed to work. Lets say this piece of code:

import secrets

secret_num = secrets.choice([1, 2, 3])
print(secret_num)

它应该输出一个完美的随机数.但是用电脑怎么可能呢?

It's supposed to output a perfectly random number. But how is that possible using computers?

推荐答案

文档secrets 模块说它产生适合管理密码、帐户身份验证、安全令牌和相关机密等数据的加密强随机数".文档没有具体说明它是如何做到的.

The documentation for the secrets module says it produces "cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets". The documentation doesn't specify how it does so exactly.

然而,通常需要加密强随机数".是他们应该很难被外部攻击者猜到.为此,secrets 模块可能依赖于操作系统提供的随机数生成器(例如 secrets.SystemRandom 所做的),以及如何 生成器的工作方式又取决于操作系统.但总的来说,专为信息安全设计的随机数生成器最终依赖于从不确定性来源收集难以猜测的位,如下面的问题进一步解释:

However, a usual requirement for "cryptographically strong random numbers" is that they should be hard to guess by outside attackers. To this end, the secrets module may rely on the random number generator provided by the operating system (as secrets.SystemRandom does, for example), and how that generator works depends in turn on the operating system. But in general, a random number generator designed for information security ultimately relies on gathering hard-to-guess bits from nondeterministic sources, as further explained in the following question:

这篇关于在 Python 中,secrets 模块如何制作完美的随机序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆