linux隨機數生成

  隨機數多應用在密碼的隨機生成安全

#隨機數生成
$RANDOM (1-32767)

十一、內部系統變量($RANDOM) 1-32767
2二、 awk 'BEGIN{srand();print rand()*1000000}' 
3三、openssl base64/hex
openssl rand -base64 8 | md5sum | cut -c1-8 #八位字母和數字的組合,3a61800e
openssl rand -base64 8 | cksum | cut -c1-8 #八位數字,10784736
4四、系統內惟一數據生成隨機數(/dev/random及/dev/urandom)
/dev/random存儲系統當前運行的環境的實時數據,能夠看做系統某時候的惟一值數據,提供優質隨機數。
/dev/urandom是非阻塞的隨機數產生器,讀取時不會產生阻塞,速度更快、安全性較差的隨機數發生器。

cat /dev/urandom | head -n 10 | md5sum | head -c 10 #32f1e953ac
cat /dev/urandom | strings -n 8 | head -n 1 #生成全字符的隨機字符串,08?WU$ZU
cat /dev/urandom | sed -e 's/[^a-zA-Z0-9]//g' | strings -n 8 | head -n 1 #生成數字加字母的隨機字符串,Ql2q9CXS

其中 strings -n設置字符串的字符數,head -n設置輸出的行數。
相關文章
相關標籤/搜索