如何创建一个在python中加密安全的随机数?

时间:2022-11-25 19:42:46

I'm making a project in python and I would like to create a random number that is cryptographically secure, How can I do that? I have read online that the numbers generated by the regular randomizer are not cryptographically secure, and that the function os.urandom(n) returns me a string, and not a number.

我正在用python做一个项目,我想创建一个随机数,它在密码上是安全的,我怎么做呢?我在网上读到,常规随机数生成的数字在密码上并不安全,函数os.urandom(n)返回的是一个字符串,而不是一个数字。

5 个解决方案

#1


35  

You can get a list of random numbers by just applying ord function over the bytes returned by os.urandom, like this

只要对os返回的字节应用ord函数,就可以得到随机数的列表。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]

Quoting os.urandom documentation,

引用操作系统。urandom文档,

Return a string of n random bytes suitable for cryptographic use.

返回一个适合加密使用的n个随机字节的字符串。

This function returns random bytes from an OS-specific randomness source. The returned data should be unpredictable enough for cryptographic applications, though its exact quality depends on the OS implementation. On a UNIX-like system this will query /dev/urandom, and on Windows it will use CryptGenRandom().

该函数从特定于os的随机性源中返回随机字节。对于加密应用程序来说,返回的数据应该足够不可预测,尽管它的确切质量取决于OS实现。在类unix系统上,它将查询/dev/urandom,在Windows上它将使用CryptGenRandom()。

#2


48  

Since you want to generate integers in some specific range, it's a lot easier to use the random.SystemRandom class instead. Creating an instance of that class gives you an object that supports all the methods of the random module, but using os.urandom() under the covers. Examples:

因为您希望在某些特定的范围内生成整数,所以使用随机数要容易得多。SystemRandom类。创建该类的一个实例将向您提供一个对象,该对象支持随机模块的所有方法,但是在幕后使用os.urandom()。例子:

>>> from random import SystemRandom
>>> cryptogen = SystemRandom()
>>> [cryptogen.randrange(3) for i in range(20)] # random ints in range(3)
[2, 2, 2, 2, 1, 2, 1, 2, 1, 0, 0, 1, 1, 0, 0, 2, 0, 0, 0, 0]
>>> [cryptogen.random() for i in range(3)]  # random floats in [0., 1.)
[0.2710009745425236, 0.016722063038868695, 0.8207742461236148]

Etc. Using urandom() directly, you have to invent your own algorithms for converting the random bytes it produces to the results you want. Don't do that ;-) SystemRandom does it for you.

等等。直接使用urandom(),您必须发明自己的算法来将它产生的随机字节转换为您想要的结果。不要那样做;-)系统随机为你做。

Note this part of the docs:

请注意文件的这一部分:

class random.SystemRandom([seed])

类random.SystemRandom((种子))

Class that uses the os.urandom() function for generating random numbers from sources provided by the operating system. Not available on all systems. Does not rely on software state and sequences are not reproducible. Accordingly, the seed() and jumpahead() methods have no effect and are ignored. The getstate() and setstate() methods raise NotImplementedError if called.

使用os.urandom()函数的类,用于从操作系统提供的源生成随机数。不是所有系统都可用。不依赖于软件状态和序列是不可复制的。因此,seed()和jumpahead()方法没有效果,并且被忽略。getstate()和setstate()方法在调用时引发NotImplementedError。

#3


16  

Python 3.6 introduces a new secrets module, which "provides access to the most secure source of randomness that your operating system provides." In order to generate some cryptographically secure numbers, you can call secrets.randbelow().

Python 3.6引入了一个新的秘密模块,它“提供了您的操作系统所提供的最安全的随机性源”。为了生成一些密码安全的数字,可以调用secret .randbelow()。

secrets.randbelow(n)

which will return a number between 0 and n.

它会返回一个0到n之间的数。

#4


5  

If you want an n-bit random number, under Python 2.4+, the easiest method I've found is

如果你想要一个n位随机数,在Python 2.4+下,我找到的最简单的方法是

import random
random.SystemRandom().getrandbits(n)

Note that SystemRandom uses os.urandom(), so the result of this method is only as good as your system's urandom() implementation.

注意,SystemRandom使用os.urandom(),因此此方法的结果仅与系统的urandom()实现相同。

#5


0  

To generate a cryptographically secure pseudorandom integer, you can use the following code:

要生成密码安全的伪随机数,可以使用以下代码:

int(binascii.hexlify(os.urandom(n)),16)

Where n is an integer and, the larger n is, the larger the integer generated is.

n是整数,n越大,生成的整数越大。

You will have to import os and binascii first.

您必须首先导入操作系统和binascii。

The result of this code can vary by platform.

此代码的结果可能因平台而异。

#1


35  

You can get a list of random numbers by just applying ord function over the bytes returned by os.urandom, like this

只要对os返回的字节应用ord函数,就可以得到随机数的列表。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]

Quoting os.urandom documentation,

引用操作系统。urandom文档,

Return a string of n random bytes suitable for cryptographic use.

返回一个适合加密使用的n个随机字节的字符串。

This function returns random bytes from an OS-specific randomness source. The returned data should be unpredictable enough for cryptographic applications, though its exact quality depends on the OS implementation. On a UNIX-like system this will query /dev/urandom, and on Windows it will use CryptGenRandom().

该函数从特定于os的随机性源中返回随机字节。对于加密应用程序来说,返回的数据应该足够不可预测,尽管它的确切质量取决于OS实现。在类unix系统上,它将查询/dev/urandom,在Windows上它将使用CryptGenRandom()。

#2


48  

Since you want to generate integers in some specific range, it's a lot easier to use the random.SystemRandom class instead. Creating an instance of that class gives you an object that supports all the methods of the random module, but using os.urandom() under the covers. Examples:

因为您希望在某些特定的范围内生成整数,所以使用随机数要容易得多。SystemRandom类。创建该类的一个实例将向您提供一个对象,该对象支持随机模块的所有方法,但是在幕后使用os.urandom()。例子:

>>> from random import SystemRandom
>>> cryptogen = SystemRandom()
>>> [cryptogen.randrange(3) for i in range(20)] # random ints in range(3)
[2, 2, 2, 2, 1, 2, 1, 2, 1, 0, 0, 1, 1, 0, 0, 2, 0, 0, 0, 0]
>>> [cryptogen.random() for i in range(3)]  # random floats in [0., 1.)
[0.2710009745425236, 0.016722063038868695, 0.8207742461236148]

Etc. Using urandom() directly, you have to invent your own algorithms for converting the random bytes it produces to the results you want. Don't do that ;-) SystemRandom does it for you.

等等。直接使用urandom(),您必须发明自己的算法来将它产生的随机字节转换为您想要的结果。不要那样做;-)系统随机为你做。

Note this part of the docs:

请注意文件的这一部分:

class random.SystemRandom([seed])

类random.SystemRandom((种子))

Class that uses the os.urandom() function for generating random numbers from sources provided by the operating system. Not available on all systems. Does not rely on software state and sequences are not reproducible. Accordingly, the seed() and jumpahead() methods have no effect and are ignored. The getstate() and setstate() methods raise NotImplementedError if called.

使用os.urandom()函数的类,用于从操作系统提供的源生成随机数。不是所有系统都可用。不依赖于软件状态和序列是不可复制的。因此,seed()和jumpahead()方法没有效果,并且被忽略。getstate()和setstate()方法在调用时引发NotImplementedError。

#3


16  

Python 3.6 introduces a new secrets module, which "provides access to the most secure source of randomness that your operating system provides." In order to generate some cryptographically secure numbers, you can call secrets.randbelow().

Python 3.6引入了一个新的秘密模块,它“提供了您的操作系统所提供的最安全的随机性源”。为了生成一些密码安全的数字,可以调用secret .randbelow()。

secrets.randbelow(n)

which will return a number between 0 and n.

它会返回一个0到n之间的数。

#4


5  

If you want an n-bit random number, under Python 2.4+, the easiest method I've found is

如果你想要一个n位随机数,在Python 2.4+下,我找到的最简单的方法是

import random
random.SystemRandom().getrandbits(n)

Note that SystemRandom uses os.urandom(), so the result of this method is only as good as your system's urandom() implementation.

注意,SystemRandom使用os.urandom(),因此此方法的结果仅与系统的urandom()实现相同。

#5


0  

To generate a cryptographically secure pseudorandom integer, you can use the following code:

要生成密码安全的伪随机数,可以使用以下代码:

int(binascii.hexlify(os.urandom(n)),16)

Where n is an integer and, the larger n is, the larger the integer generated is.

n是整数,n越大,生成的整数越大。

You will have to import os and binascii first.

您必须首先导入操作系统和binascii。

The result of this code can vary by platform.

此代码的结果可能因平台而异。