读取 /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 , 10988 阅读
< /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.

Avatar_small
pokemesh 说:
2017年10月22日 14:18

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
Tamilrockers 说:
2017年11月16日 17:25

This is great, That's a great website
https://tamilrockers.org.in/julie-2-tamilrockers/
https://tamilgun.org.in/julie-2-tamilgun/
https://tamilyogi.org.in/julie-2-tamilyogi/
https://tamilrockrs.org/julie-2-tamilrockers/
https://hdvideosong.org/julie-2-video-songs/
https://xmovies8.net.in/tumhari-sulu-xmovies8/
https://khatrimaza.net.in/dashakriya-khatrimaza/
https://khatrimaza.net.in/dil-jo-na-keh-saka-khatrimaza/
https://khatrimaza.net.in/tumhari-sulu-khatrimaza/
https://xmovies8.net.in/dashakriya-xmovies8/
https://xmovies8.net.in/dil-jo-na-keh-saka-xmovies8/
https://tamilrockrs.org/en-aaloda-seruppa-kaanom-tamilrockers/
https://tamilgun.org.in/en-aaloda-seruppa-kaanom-tamilgun/
https://tamilrockers.org.in/en-aaloda-seruppa-kaanom-tamilrockers/
https://tamilyogi.org.in/en-aaloda-seruppa-kaanom-tamilyogi/
https://tamilgun.org.in/theeran-adhigaram-ondru-tamilgun/
https://tamilyogi.org.in/theeran-adhigaram-ondru-tamilyogi/
https://tamilrockrs.org/theeran-adhigaram-ondru-tamilrockers/
https://tamilrockers.org.in/theeran-adhigaram-ondru-tamilrockers/

Avatar_small
jio4gvoice 说:
2017年11月23日 01:55

I really like reading through a post that can make people think. Also, many thanks for permitting me to comment!

Avatar_small
yareel 说:
2017年12月18日 19:54

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
vid2mp3 说:
2017年12月18日 19:56

I'm very happy to find this page. I need to to thank you for your time due to this fantastic read!! I definitely enjoyed every part of it and I have you book marked to see new stuff on your web site.

Avatar_small
khatrimaza 说:
2017年12月21日 19:14

This is the right site for everyone who would like to find out about this topic. You realize so much its almost tough to argue with you (not that I actually would want to…HaHa). You definitely put a fresh spin on a topic which has been discussed for ages. Wonderful stuff, just great!

Avatar_small
mod apk game 说:
2017年12月25日 01:30

I hope that it does not disappoint me as much as this particular one. I mean, I know it was my choice to read

Avatar_small
adbfire 说:
2018年1月09日 01:45

Thanks a lot for sharing this informative blog. I am new to this field, and thus i am hoping for the similar post so that i can enhance my knowledge. Thank you.

Avatar_small
jio Cinema 说:
2018年2月01日 19:26

This is very nice post.Awesome keep sharing.

Avatar_small
cinni 说:
2018年2月17日 20:37

<a href="https://plus.google.com/u/0/105950225954697108155">call of duty black ops zombies</a>

Avatar_small
viper for android do 说:
2018年3月26日 15:14

I found your post so interesting.

Avatar_small
Assignment help 说:
2018年10月06日 17:24

Students Assignment Help provides the services of customer psychology assignment help to the students.All Assignment Help is an online Assignment Help and assignment writing service offering expert assignment help.Hire Australian,US assignment writing experts.

Avatar_small
mobile price in bd 说:
2018年10月08日 09:56

thank you, brother, its good information

Avatar_small
Assignment help 说:
2018年10月12日 17:19

We have a strict quality control unit. They scrutinize the solutions and assure us about the quality. We do not compromise with the quality. If we are not satisfied, we ask the writers to revise the assignment solutions.Expert assignment helpers of Allassignmenthelp are well efficient and capable of creating unique assignments for college or university students all across the globe

Avatar_small
samsung mobile price 说:
2018年10月18日 17:47

I am waiting for your next article

Avatar_small
Mark Carey 说:
2018年10月29日 18:52

For regular security and support regarding apple account theft, loss and reset please read the information below:

According to a survey conducted recently, 72% of apple accounts are insecure and vulnerable to theft. There is a strong chance your apple account is also one of them. But the working solution is here:

Softpro services provide all kind of password-related queries for Apple products. With 24x7 online support, they are best apple id reset service providers on board.

[24x7] Toll-Free number: 1844-797-8692

For free consultation and queries regarding your apple id, here is the direct link of their website:

http://softproserv.com/iforgot-apple-id-password/index.html

Avatar_small
Mark Carey 说:
2018年10月29日 18:55

Are you facing troubles regarding your Microsoft account security? Are ids getting hacked?
There are strong chances that your account is also insecure like 81% other accounts.
Softpro services provide all kind of password-related queries for Microsoft products. With 24x7 online support, they are best email and account live password reset service providers on board.
Call Now Toll-Free number: 1-844-797-8692

For free consultation and queries regarding your Microsoft account, here is the direct link of their website.


登录 *


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