python中令牌是什么
In this piece, you’ll learn the proper ways to generate strong random passwords and tokens that are cryptographically secured. Having secure random numbers allows us to manage sensitive information, such as password and security tokens. We will be using the secrets module, available since Python 3.6. The official documentation states:
在本文中,您将学习生成安全的强密码和令牌的正确方法。 拥有安全的随机数使我们能够管理敏感信息,例如密码和安全令牌。 我们将使用从Python 3.6开始可用的secrets模块。 官方文档指出:
“… secrets should be used in preference to the default pseudo-random number generator in the random module, which is designed for modelling and simulation, not security or cryptography.”
“……机密应优先于随机模块中默认的伪随机数生成器使用,后者是为建模和仿真而设计的,而不是安全性或加密技术。”
There are three sections in this article:
本文分为三个部分:
- Basic Usage 基本用法
- Examples 例子
- Conclusion 结论
Let’s get started.
让我们开始吧。
1.基本用法 (1. Basic Usage)
The secrets module provides a few built-in functions that we can use to generate numbers and tokens. No setup is required but we need to import the module before we use it.
Secrets模块提供了一些内置函数,可用于生成数字和令牌。 不需要任何设置,但是在使用模块之前,我们需要先导入模块。
import secrets
产生一个随机数 (Generate a random number)
Use the randbelow
function to generate a number. It accepts an integer and the number generated is between 0 and the input integer minus 1. The input integer must be higher than 0
使用randbelow
函数生成一个数字。 它接受一个整数,并且生成的数字在0到输入整数减1之间。输入整数必须大于0。
secrets.randbelow(2) # generate either 0 or 1
secrets.randbelow(10) # generate a number from 0 to 9
secrets.rand