读取 /dev/urandom or /dev/random 生成随机数 - yanbin's Blog

读取 /dev/urandom or /dev/random 生成随机数

yanbin posted @ 2015年3月29日 19:28 in Programming with tags programming linux random , 6407 阅读
< /dev/urandom tr -dc A-NP-Za-kmnp-z2-9 | head -c 8
 获取一个 8 位的随机数,除了 0, 1, o,O, l 之外.
 
tr -dc a-z < /dev/urandom 从 /dev/urandom 读入数据并且把所有的小写字母输出。
-d, delete characters in SET1 (a-z);
-c, use the complement of SET1
一个删除,一个补全,最终的效果就是获得读入数据中的所有小写字母。
tr 是从标准输入读入数据,直到遇到 EOF 才会停止,但是 /dev/urandom 是没有 EOF 的。
head -c 8 的作用很明显,就是获得 tr 标准输出中的前 8 个字符,这样这条命令/程序,才可以退出。
 
节省熵池的方法, 生成了长度为 12 的随机数:
dd if=/dev/urandom bs=1 count=6 2> /dev/null | od -t x1 | tee test | sed '2d;s/^0\+ //;s/ //g'
 
生成一个随机 MAC 地址, 节省熵池的方法:
dd if=/dev/urandom bs=1 count=6 2> /dev/null | od -t x1 | sed '2d;s/^0\+ //;s/ /:/g'
 
不建议使用的方法:
< /dev/urandom tr -dC a-f0-9 | head -c 12 | sed s/../\&:/g | head -c 17
 
/dev/urandom:u 是 unlocked 的意思.
/dev/urandom 是 /dev/random 的非阻塞副本。
/dev/random 是 Linux/Unix  操作系统中的一个设备文件,用来作为随机数发生器/伪随机数发生器。
它允许程序访问来自设备驱动程序或其它来源的背景噪声,Linux是第一个以背景噪声产生真正的随机的实现。
 
在 Linux 系统上,有一个存储噪声的熵池,读取时 /dev/random 设备返回小于熵池字节总数的随机字节。
/dev/random 可生成高随机性的公钥一次性密码本。若熵池空了,对 /dev/random 的读操作将会被阻塞,直到收集到了足够的环境噪声为止[3]
这样的设计使得 /dev/random 是真正的随机数发生器,提供了最大可能的随机数据熵,建议在需要生成高强度的密钥时使用。
/dev/random 的一个副本是 /dev/urandom ("unlocked",非阻塞的随机数发生器[4]),它会重复使用熵池中的数据以产生伪随机数据。
这表示对/dev/urandom的读取操作不会产生阻塞,但其输出的熵可能小于 /dev/random 的。
它可以作为生成较低强度密码的伪随机数生成器,不建议用于生成高强度长期密码。

感谢 依云 教我用节省熵池的方法。

Avatar_small
mobogenie 说:
2017年1月22日 19:03

Really appreciate for your amazing article. Keep going on, good stuff. Thank you for this valuable information.

Avatar_small
Banggood 说:
2017年4月09日 23:37

Really appreciate for your amazing article. Keep going on, good stuff. Thank you for this valuable information.

Avatar_small
Songsterr 说:
2017年4月15日 18:28

Really appreciate for your amazing article. Keep going on, good stuff. Thank you for this valuable information.

Avatar_small
fast pokemap 说:
2017年4月22日 15:14

Valuable information! Looking forward to seeing your notes posted. Thank you for sharing the nice article. Good to see your article.

Avatar_small
Avito.ma 说:
2017年5月11日 01:04

This is a very interesting web page and I have enjoyed reading many of the articles and posts contained on the website, keep up the good work and hope to read some more interesting content in the future.

Avatar_small
fetlife apk 说:
2017年5月21日 17:42

Valuable information! Looking fo

Avatar_small
hotmail login 说:
2017年7月17日 14:21

The article you have shared here very good. This is really interesting information for me. Thanks for sharing!

Avatar_small
free hotmail login 说:
2017年7月17日 14:22

Thanks for sharing this great. Keep sharing more useful and conspicuous stuff like this. Thank you so much

Avatar_small
slader 说:
2017年7月23日 16:36

Valuable information! Looking forward to seeing your notes posted. The information you have posted is very useful. Keep going on, good stuff. Thank you for this valuable information. I have enjoyed reading many of the articles and posts contained on the website, keep up the good work and hope to read some more interesting content in the future.

Avatar_small
rediffmail 说:
2017年7月25日 01:17

This is a very interesting web page and I have enjoyed reading many of the articles and posts contained on the website, keep up the good work and hope to read some more interesting content in the future.

Avatar_small
Ace2three 说:
2017年7月25日 10:55

The sites you have referred were good. This is a great article, that I really enjoyed reading. Thanks for sharing.

Avatar_small
symbolab 说:
2017年7月30日 21:36


Valuable​ ​information!​ ​Looking​ ​forward​ ​to​ ​seeing​ ​your​ ​notes​ ​posted.​ ​The​ ​information​ ​you​ ​have
posted​ ​is​ ​very​ ​useful.​ ​Keep​ ​going​ ​on,​ ​good​ ​stuff.​ ​Thank​ ​you​ ​for​ ​this​ ​valuable​ ​information.​ ​I​ ​have
enjoyed​ ​reading​ ​many​ ​of​ ​the​ ​articles​ ​and​ ​posts​ ​contained​ ​on​ ​the​ ​website,​ ​keep​ ​up​ ​the​ ​good
work​ ​and​ ​hope​ ​to​ ​read​ ​some​ ​more​ ​interesting​ ​content​ ​in​ ​the​ ​future.

Avatar_small
chat avenue 说:
2017年8月01日 10:09

It is a great article. You will surely like this also because it is a great stuff, yeah it’s give us lots of interest and pleasure. Their opportunities are so fantastic and working style so speedy. Thank you for sharing the nice article.

Avatar_small
chat avenue 说:
2017年8月01日 20:33

It is a great article. You will surely like this also because it is a great stuff, yeah it’s give us lots of interest and pleasure. Their opportunities are so fantastic and working style so speedy. Thank you for sharing the nice article.

Avatar_small
Ace2three 说:
2017年8月02日 12:13

Good stuff, really superb. Because we can get more views through your site. So please keep update like this.

Avatar_small
chat avenue 说:
2017年8月03日 23:32

http://ssapptricks.com/chat-avenue/ Valuable information! Looking forward to seeing your notes posted. The information you have posted is very useful. Keep going on, good stuff. Thank you for this valuable information.

Avatar_small
bancolombia 说:
2017年8月11日 15:44


Valuable​ ​information!​ ​Looking​ ​forward​ ​to​ ​seeing​ ​your​ ​notes​ ​posted.​ ​The​ ​information​ ​you​ ​have
posted​ ​is​ ​very​ ​useful.​ ​Keep​ ​going​ ​on,​ ​good​ ​stuff.​ ​Thank​ ​you​ ​for​ ​this​ ​valuable​ ​information.​ ​I​ ​have
enjoyed​ ​reading​ ​many​ ​of​ ​the​ ​articles​ ​and​ ​posts​ ​contained​ ​on​ ​the​ ​website,​ ​keep​ ​up​ ​the​ ​good
work​ ​and​ ​hope​ ​to​ ​read​ ​some​ ​more​ ​interesting​ ​content​ ​in​ ​the​ ​future.

Avatar_small
tipsforapps 说:
2017年8月18日 22:13

Valuable information! Looking forward to seeing your notes posted. Thank you for sharing the nice article. Good to see your article.

Avatar_small
free hotmail login 说:
2017年8月23日 16:16

This is great, That's a great website, it really is what I was looking for, thank you for sharing!

Avatar_small
tv online streaming 说:
2017年9月20日 21:52

Good stuff, really superb. Because we can get more views through your site. So please keep update like this.

Avatar_small
lolnexus 说:
2017年9月20日 23:52

It is a great article. You will surely like this also because it is a great stuff, yeah it’s give us lots of interest and pleasure. Their opportunities are so fantastic and working style so speedy. Thank you for sharing the nice article.

Avatar_small
lolnexus 说:
2017年9月27日 15:06

I have enjoyed reading many of the articles and posts contained on the website, keep up the good work and hope to read some more interesting content in the future.

Avatar_small
shelly 说:
2017年10月16日 18:38

Info nya mantap gan..
Jangan lupa kunjungi blog kami juga ya gan

<a title="Agen Bola Online" href="http://www.arenasarana.com" rel"dofollow"><strong>Agen Bola Online</strong></a>
<a title="Casino Online" href="http://www.arenasarana.com" rel"dofollow"><strong>Casino Online</strong></a>
<a title="Judi Online" href="http://www.arenasarana.com" rel"dofollow"><strong>Judi Online</strong></a>
<a title="Agen Togel" href="http://www.arenasarana.com" rel"dofollow"><strong>Agen Togel</strong></a>
<a title="Bandar Bola" href="http://www.arenasarana.com" rel"dofollow"><strong>Bandar Bola</strong></a>

Avatar_small
lolnexus 说:
2017年10月19日 19:05

Valuable information! Looking forward to seeing your notes posted. The information you have posted is very useful. Keep going on, good stuff. Thank you for this valuable information.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee