![linux中生成随机数](https://img-blog.csdnimg.cn/img_convert/b9b74745816f7a4eef8c5bfd5856096f.png)
linux中生成随机数
Generating random numbers can seem odd. But it is very important for security. Insecurity field whatever best cipher or algorithm you use if you can’t generate random numbers it is useless from point of view of security. So how can we generate random numbers in a Linux environment?
生成随机数似乎很奇怪。 但这对于安全性非常重要。 如果无法生成随机数,无论使用哪种最佳密码或算法,“不安全性”字段从安全性的角度来看都是没有用的。 那么我们如何在Linux环境中生成随机数呢?
用urandom生成
文件
(Generate With urandom File)
![](https://img-blog.csdnimg.cn/img_convert/b5fd1a6535022ea5ff3b816d4c633a4d.png)
urandom
is a device which resides under the /dev
. We can read this file which will output some binary values. We can use cat or similar command to print or redirect into a file.
urandom
是位于/dev
下的设备。 我们可以读取此文件,该文件将输出一些二进制值。 我们可以使用cat或类似的命令来打印或重定向到文件中。
$ cat /dev/urandom
![Generate With urandom File](https://img-blog.csdnimg.cn/img_convert/89b41edc3bb94cc89e95f5d39692a5d9.png)
使用Openssl生成随机数(Generate Random Numbers With Openssl)
The second option is to use OpenSSL which is a core security library for a lot of application. OpenSSL rand 256 commands will generate 256 bytes of random data with the binary format. to generate more readable format like hex use -hex
option rand -hex 256
.
第二种选择是使用OpenSSL,它是许多应用程序的核心安全性库。 OpenSSL rand 256命令将以二进制格式生成256字节的随机数据。 要生成更易读的格式(例如十六进制),请使用-hex
选项rand -hex 256
。
$ openssl rand -hex 256
![Generate With Openssl](https://img-blog.csdnimg.cn/img_convert/9342facc6fb4bd64c18c4204a04dd7d3.png)
使用Python生成随机数(Generate Random Numbers With Python)
The third option is using python random library. But this library generates random numbers rather than random data. We will use random
module and random()
function like below. This will generate a random number between 1 and 0.
第三种选择是使用python随机库。 但是该库生成随机数,而不是随机数据。 我们将使用如下所示的random
模块和random()
函数。 这将生成一个介于1和0之间的随机数。
import random
random.random()
![Generate With Python](https://img-blog.csdnimg.cn/img_convert/54371350cc29239b6507d7b3754a1004.png)
linux中生成随机数