/ dev / urandom vs / dev / random [英] /dev/urandom vs. /dev/random

查看:68
本文介绍了/ dev / urandom vs / dev / random的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的一段代码中,它简单地生成一系列

(随机)八进制代码,我很惊讶

的明显非随机性的/ dev /随机的。除非RAND_LENGTH很大,否则它并不明显。假设/ dev / random是更随机的假设我是

而不是

/ dev / urandom,并且如果它用完了熵,它会阻止

得到更多。为什么我在输出中看到这么多零?


#include< stdio.h>

#include< fcntl.h>

#define RAND_LEN 1024

void

read_random(const char * dev){

int i,fd ;

char dat [RAND_LEN];


fd = open(dev,O_RDONLY);


dat [ RAND_LEN] =''\''';


if(fd!= -1){

read(fd,dat,RAND_LEN);

for(i = 0; i< RAND_LEN; i ++){

dat [i] =(dat [i]>> 4& 0x07)+ 48 ;

}

printf("%s:%s \ n \ n",dev,dat);

}其他{

退出(1);

}


关闭(fd);

}


int

main(无效){

read_random(" / dev / random");

read_random(" / dev / urandom");

return(0);

}


506 $。 / test


/ dev / random:200050617063526437 60251034665507247403176742630660 50462223206040017306150002434274350703035033674561 20754173510400040000000050000000000007737204577300 00177356040504450022043404000034041773610464046404 00000000000000000000000000000000000000000000000000 00000000000000000000000000640423461000000000000000 00000000000000000000000000002404000000004100700000 00634610000000000000000000000000000000000000000000 00000000000000000000000000005604000000000000340437 73040037730504300000004773000000000000000000000000 00003000100000000000000000000000000000000000000000 00000000000000000000000000000450045004000000002000 70041304330407040000170434041704050427730704050400 30160457732404000067040000000000000000000070042000 64040000340400006404677300045004000000005773200070 04130433040704000017043404640440004773070440000770 00007773240400005704000000000000000000000000000034 04710007047773220417046704000000000000777300000000 00000000077007735404050416046304070457733773020470 002000400447735773110464146414 37736504641457733404 000054041773220464045704


的/ dev / urandom的:10313700276075133047145322754731744715330540305544 55772411741125544374737071744665152471472301655103 21673032612147044376361336471103425426601777564215 27537506506760532046755677545002343626131545501130 60140067534031123241466461750705230751645572440071 57744425536470274352332415164131435115125573101442 74556544717777054520152351764241666207571007700122 35623563012311717741547124617262165454244161061517 74201651063200446176135471662402411424412532245563 11545142476223664702172000553652612247511550424514 72612104741103553240132614710710775524572432011176 15642610621436346764640170271720054525337367306022 20367455521635765367573615753123615162510360064030 26113642124742630634422243216772330177360713406462 47534536645354770361405340770217451441530701353725 67322557767603402406511316575637667506305617702430 35222250564070154307150656704155003661646277201332 4652304564176034356626644273166731336 5016666076430 23673210310073545356647564170524251663066260134503 70701275067537603265673560312322526077022157223404 211323047413633213326054

-

Ron Peterson

Network&系统经理

Mount Holyoke College

解决方案

./ test


/开发/随机:20005061706352643760251034665507247403176742630660 50462223206040017306150002434274350703035033674561 20754173510400040000000050000000000007737204577300 00177356040504450022043404000034041773610464046404 00000000000000000000000000000000000000000000000000 00000000000000000000000000640423461000000000000000 00000000000000000000000000002404000000004100700000 00634610000000000000000000000000000000000000000000 00000000000000000000000000005604000000000000340437 73040037730504300000004773000000000000000000000000 00003000100000000000000000000000000000000000000000 00000000000000000000000000000450045004000000002000 70041304330407040000170434041704050427730704050400 30160457732404000067040000000000000000000070042000 64040000340400006404677300045004000000005773200070 04130433040704000017043404640440004773070440000770 000077732404000057040000 00000000000000000000000034 04710007047773220417046704000000000000777300000000 00000000077007735404050416046304070457733773020470 00200040044773577311046414641437736504641457733404 000054041773220464045704


的/ dev / urandom的:10313700276075133047145322754731744715330540305544 55772411741125544374737071744665152471472301655103 21673032612147044376361336471103425426601777564215 27537506506760532046755677545002343626131545501130 60140067534031123241466461750705230751645572440071 57744425536470274352332415164131435115125573101442 74556544717777054520152351764241666207571007700122 35623563012311717741547124617262165454244161061517 74201651063200446176135471662402411424412532245563 11545142476223664702172000553652612247511550424514 72612104741103553240132614710710775524572432011176 15642610621436346764640170271720054525337367306022 20367455521635765367573615753123615162510360064030 26113642124742630634422243216772330177360713406462 4753453664535477036140534077021 7451441530701353725 67322557767603402406511316575637667506305617702430 35222250564070154307150656704155003661646277201332 46523045641760343566266442731667313365016666076430 23673210310073545356647564170524251663066260134503 70701275067537603265673560312322526077022157223404 211323047413633213326054


-

罗恩·彼得森

网络和放大器;系统经理

Mount Holyoke College


Ron Peterson< rp ****** @ mtholyoke.edu>潦草地写道:

在下面的一段代码中,它简单地生成一系列
(随机)八进制代码,我很惊讶于
/ dev / random。除非RAND_LENGTH很大,否则它并不明显。在/ dev / random是更随机的假设下,我是

/ dev / urandom,并且如果它耗尽熵直到它获得更多,它会阻止它。为什么我在输出中看到如此多的零?


您的问题是关于Unix设备/ dev / random和/ dev / urandom,

不是关于C.请继续关于此问题的讨论
comp.unix.programmer。

#include< stdio.h>
#include< fcntl.h>
#define RAND_LEN 1024
void
read_random(const char * dev){
int i,fd;
char dat [RAND_LEN];
fd = open(dev,O_RDONLY);
dat [RAND_LEN] =''\''';
if(fd!= -1){
read(fd,dat,RAND_LEN);
for(i = 0; i< RAND_LEN; i ++){
dat [ i] =(dat [i]>> 4& 0x07)+ 48;
}
printf("%s:%s \ n \ n",dev,dat) ;
} else {
exit(1);
}
close(fd);
}
int
main(void){
read_random(" / dev / random");
read_random(" / dev / urandom");
return(0);
}
506

./测试
的/ dev /随机:20005061706352643760251034665507247403176742630660 50462223206040017306150002434274350703035033674561 20754173510400040000000050000000000007737204577300 00177356040504450022043404000034041773610464046404 00000000000000000000000000000000000000000000000000 00000000000000000000000000640423461000000000000000 00000000000000000000000000002404000000004100700000 00634610000000000000000000000000000000000000000000 000000000000000000000000000056040000000000 00340437 7304003773050430000000477300000000000000000000
00000000300010000000000000000000000000000000000000 00000000000000000000000000000000045004500400000000 20007004130433040704000017043404170405042773070405 04003016045773240400006704000000000000000000007004 20006404000034040000640467730004500400000000577320 00700413043304070400001704340464044000477307044000 07700000777324040000570400000000000000000000000000 00340471000704777322041704670400000000000077730000 00000000000007700773540405041604630407045773377302 04700020004004477357731104641464143773650464145773 34040000540

41773220464045704

的/ dev / urandom的:10313700276075133047145322754731744715330540305544 55772411741125544374737071744665152471472301655103 21673032612147044376361336471103425426601777564215 27537506506760532046755677545002343626131545501130 60140067534031123241466461750705230751645572440071 57744425536470274352332415164131435115125573101442 745565447177770545201523517642416662075 71007700122 35623563012311717741547124617262165454244161061517 74201651063200446176135471662402411424412532245563 115451424762236647021720005536526122475115504


24514726121047411035532401326147107107755245724320 11176156426106214363467646401702717200545253373673 06022203674555216357653675736157531236151625103600 64030261136421247426306344222432167723301773607134 06462475345366453547703614053407702174514415307013 53725673225577676034024065113165756376675063056177 02430352222505640701543071506567041550036616462772 01332465230456417603435662664427316673133650166660 76430236732103100735453566475641705242516630662601 34503707012750675376032656735603123225260770221572 23404211323

047413633213326054


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰-------- \\ \\

\ --------------------------------------- -----------------规则! -------- /

我再也不会公开露面了。

- 荷马辛普森


In the following piece of code, which simply generates a sequence of
(random) octal codes, I''m surprised by the apparent non-randomness of
/dev/random. It''s not noticeable unless RAND_LENGTH is largish. I was
under the assumption that /dev/random was "more random" than
/dev/urandom, and that it would block if it ran out of entropy until it
got more. Why am I seeing so many zeroes in my output?

#include <stdio.h>
#include <fcntl.h>
#define RAND_LEN 1024

void
read_random( const char* dev ) {
int i, fd;
char dat[RAND_LEN];

fd = open( dev, O_RDONLY );

dat[RAND_LEN] = ''\0'';

if( fd != -1 ) {
read( fd, dat, RAND_LEN );
for( i = 0; i < RAND_LEN; i++ ) {
dat[i] = (dat[i] >> 4 & 0x07) + 48;
}
printf( "%s: %s\n\n", dev, dat );
} else {
exit( 1 );
}

close( fd );
}

int
main( void ) {
read_random( "/dev/random" );
read_random( "/dev/urandom" );
return( 0 );
}

506$ ./test

/dev/random: 20005061706352643760251034665507247403176742630660 50462223206040017306150002434274350703035033674561 20754173510400040000000050000000000007737204577300 00177356040504450022043404000034041773610464046404 00000000000000000000000000000000000000000000000000 00000000000000000000000000640423461000000000000000 00000000000000000000000000002404000000004100700000 00634610000000000000000000000000000000000000000000 00000000000000000000000000005604000000000000340437 73040037730504300000004773000000000000000000000000 00003000100000000000000000000000000000000000000000 00000000000000000000000000000450045004000000002000 70041304330407040000170434041704050427730704050400 30160457732404000067040000000000000000000070042000 64040000340400006404677300045004000000005773200070 04130433040704000017043404640440004773070440000770 00007773240400005704000000000000000000000000000034 04710007047773220417046704000000000000777300000000 00000000077007735404050416046304070457733773020470 00200040044773577311046414641437736504641457733404 000054041773220464045704

/dev/urandom: 10313700276075133047145322754731744715330540305544 55772411741125544374737071744665152471472301655103 21673032612147044376361336471103425426601777564215 27537506506760532046755677545002343626131545501130 60140067534031123241466461750705230751645572440071 57744425536470274352332415164131435115125573101442 74556544717777054520152351764241666207571007700122 35623563012311717741547124617262165454244161061517 74201651063200446176135471662402411424412532245563 11545142476223664702172000553652612247511550424514 72612104741103553240132614710710775524572432011176 15642610621436346764640170271720054525337367306022 20367455521635765367573615753123615162510360064030 26113642124742630634422243216772330177360713406462 47534536645354770361405340770217451441530701353725 67322557767603402406511316575637667506305617702430 35222250564070154307150656704155003661646277201332 46523045641760343566266442731667313365016666076430 23673210310073545356647564170524251663066260134503 70701275067537603265673560312322526077022157223404 211323047413633213326054

--
Ron Peterson
Network & Systems Manager
Mount Holyoke College

解决方案

./test

/dev/random: 20005061706352643760251034665507247403176742630660 50462223206040017306150002434274350703035033674561 20754173510400040000000050000000000007737204577300 00177356040504450022043404000034041773610464046404 00000000000000000000000000000000000000000000000000 00000000000000000000000000640423461000000000000000 00000000000000000000000000002404000000004100700000 00634610000000000000000000000000000000000000000000 00000000000000000000000000005604000000000000340437 73040037730504300000004773000000000000000000000000 00003000100000000000000000000000000000000000000000 00000000000000000000000000000450045004000000002000 70041304330407040000170434041704050427730704050400 30160457732404000067040000000000000000000070042000 64040000340400006404677300045004000000005773200070 04130433040704000017043404640440004773070440000770 00007773240400005704000000000000000000000000000034 04710007047773220417046704000000000000777300000000 00000000077007735404050416046304070457733773020470 00200040044773577311046414641437736504641457733404 000054041773220464045704

/dev/urandom: 10313700276075133047145322754731744715330540305544 55772411741125544374737071744665152471472301655103 21673032612147044376361336471103425426601777564215 27537506506760532046755677545002343626131545501130 60140067534031123241466461750705230751645572440071 57744425536470274352332415164131435115125573101442 74556544717777054520152351764241666207571007700122 35623563012311717741547124617262165454244161061517 74201651063200446176135471662402411424412532245563 11545142476223664702172000553652612247511550424514 72612104741103553240132614710710775524572432011176 15642610621436346764640170271720054525337367306022 20367455521635765367573615753123615162510360064030 26113642124742630634422243216772330177360713406462 47534536645354770361405340770217451441530701353725 67322557767603402406511316575637667506305617702430 35222250564070154307150656704155003661646277201332 46523045641760343566266442731667313365016666076430 23673210310073545356647564170524251663066260134503 70701275067537603265673560312322526077022157223404 211323047413633213326054

--
Ron Peterson
Network & Systems Manager
Mount Holyoke College


Ron Peterson <rp******@mtholyoke.edu> scribbled the following:

In the following piece of code, which simply generates a sequence of
(random) octal codes, I''m surprised by the apparent non-randomness of
/dev/random. It''s not noticeable unless RAND_LENGTH is largish. I was
under the assumption that /dev/random was "more random" than
/dev/urandom, and that it would block if it ran out of entropy until it
got more. Why am I seeing so many zeroes in my output?
Your question is about the Unix devices /dev/random and /dev/urandom,
not about C. Please continue discussion about this on
comp.unix.programmer.
#include <stdio.h>
#include <fcntl.h>
#define RAND_LEN 1024 void
read_random( const char* dev ) {
int i, fd;
char dat[RAND_LEN]; fd = open( dev, O_RDONLY ); dat[RAND_LEN] = ''\0''; if( fd != -1 ) {
read( fd, dat, RAND_LEN );
for( i = 0; i < RAND_LEN; i++ ) {
dat[i] = (dat[i] >> 4 & 0x07) + 48;
}
printf( "%s: %s\n\n", dev, dat );
} else {
exit( 1 );
} close( fd );
} int
main( void ) {
read_random( "/dev/random" );
read_random( "/dev/urandom" );
return( 0 );
} 506


./test /dev/random: 20005061706352643760251034665507247403176742630660 50462223206040017306150002434274350703035033674561 20754173510400040000000050000000000007737204577300 00177356040504450022043404000034041773610464046404 00000000000000000000000000000000000000000000000000 00000000000000000000000000640423461000000000000000 00000000000000000000000000002404000000004100700000 00634610000000000000000000000000000000000000000000 00000000000000000000000000005604000000000000340437 7304003773050430000000477300000000000000000000 00000000300010000000000000000000000000000000000000 00000000000000000000000000000000045004500400000000 20007004130433040704000017043404170405042773070405 04003016045773240400006704000000000000000000007004 20006404000034040000640467730004500400000000577320 00700413043304070400001704340464044000477307044000 07700000777324040000570400000000000000000000000000 00340471000704777322041704670400000000000077730000 00000000000007700773540405041604630407045773377302 04700020004004477357731104641464143773650464145773 34040000540
41773220464045704
/dev/urandom: 10313700276075133047145322754731744715330540305544 55772411741125544374737071744665152471472301655103 21673032612147044376361336471103425426601777564215 27537506506760532046755677545002343626131545501130 60140067534031123241466461750705230751645572440071 57744425536470274352332415164131435115125573101442 74556544717777054520152351764241666207571007700122 35623563012311717741547124617262165454244161061517 74201651063200446176135471662402411424412532245563 115451424762236647021720005536526122475115504


24514726121047411035532401326147107107755245724320 11176156426106214363467646401702717200545253373673 06022203674555216357653675736157531236151625103600 64030261136421247426306344222432167723301773607134 06462475345366453547703614053407702174514415307013 53725673225577676034024065113165756376675063056177 02430352222505640701543071506567041550036616462772 01332465230456417603435662664427316673133650166660 76430236732103100735453566475641705242516630662601 34503707012750675376032656735603123225260770221572 23404211323
047413633213326054

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"I will never display my bum in public again."
- Homer Simpson


这篇关于/ dev / urandom vs / dev / random的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆