linux系統產生隨機數的6種方法

方法一:通過系統環境變量($RANDOM)

[root@localhost script]# echo $RANDOM

15491

[root@localhost script]# echo $RANDOM

31703

方法二:通過openssl產生隨機數

[root@localhost script]# openssl rand -base64 8

Ii3RMp2rWW4=

[root@localhost script]# openssl rand -base64 10

5vAx7WpCndOW+g==

方法三:通過時間獲得隨機數

[root@localhost script]# date +%s%N

1471315388251547446

[root@localhost script]# date +%s%N

1471315393753792762

方法四:通過urandom設備

 [root@localhost script]# head /dev/urandom |cksum

1190288578 3139

[root@localhost script]# head /dev/urandom |cksum

2086682940 1895

方法五:UUID方式

 [root@localhost script]# cat /proc/sys/kernel/random/uuid 

caa88f84-9ffc-4815-b6a3-311b48ec46a9

[root@localhost script]# cat /proc/sys/kernel/random/uuid 

2fe09db8-883b-45f3-ba43-78daeba053e9

方法六:expect 方式

[root@localhost script]# yum install -y expect

[root@localhost script]# mkpasswd -l 8

b8qHNj3{

[root@localhost script]# mkpasswd -l 8

GLo\mv14

若以上隨機數長短不一,如何統一格式,使用md5sum命令

[root@localhost script]# echo $RANDOM |md5sum|cut -c 1-8

752a2fb4

[root@localhost script]# openssl rand -base64 10 |md5sum|cut -c 1-8

63abdb08

[root@localhost script]# date +%s%N |md5sum|cut -c 1-8    

821eda55

[root@localhost script]# head /dev/urandom |cksum |md5sum|cut -c 1-8    

37abe446

[root@localhost script]# cat /proc/sys/kernel/random/uuid  |md5sum|cut -c 1-8  

24147761

[root@localhost script]# mkpasswd -l 8 |md5sum|cut -c 1-8  

781bdda1


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章