¡¡¡¡µ«ÊÇÈç¹ûÎÒÃǵÄÖÖ×ÓËã·¨±»ÖªµÀÁË£¬ÄÇôÏÔÈ»¾Í²»ÐÐÁË¡£µ±È»×÷ΪijЩӦÓÃÒ²ÎÞËùν£¬±ÈÈçÎÒÃÇÒªÔÚÆÁÄ»ÉÏËæ»úµÄ»Ò»Ö»Ð¡Öí¡£ÕâÑùµÄÓ¦Óü¸ºõ²»»áÓÐË»áÈ¥¹ØÐÄÏÂÒ»´Î»áÊÇʲô½á¹û¡£²»¹ýÓÐЩӦÓþͲ»Ò»ÑùÁË£¬´óµÄ²»Ëµ£¬¾ÍÊÇһЩÓÎÏ·£¬Ò²µÃ¿¼ÂÇËæ»úÊýµÄ°²È«ÐÔÎÊÌâ¡£
¡¡¡¡¼òµ¥µÄ°ì·¨ÊÇ£¬ÎÒÃǵÄÖÖ×ÓÒ²ÓÃËæ»úÊýÀ´±íʾ¡£²»¹ýÕâÑùÒ»À´£¬Ëƺõ¾ÍÓм¦Éúµ°»¹Êǵ°Éú¼¦µÄÎÊÌâ¡£ºÃÔÚlinux¸øÎÒÃÇÌṩÁË¡°ÕæÕýµÄ¡±Ëæ»úÊý£¬ÔÚÄÚºËÖУ¬linux»áά»¤Ò»Ð©Å¼È»³öÏÖµÄÊý¾Ý£¬²¢ÇÒΪÓû§Ìṩ·ÃÎʽӿڡ£Ö®ËùÒÔ³Æ֮ΪÕæÕýµÄËæ»úÊý£¬ÊÇÒòΪÕâЩÊý¾ÝÀ´Ô´ÓÚ¼ÆËã»ú±¾ÉíµÄżȻ²Ù×÷£¬±ÈÈçÓ²Å̲Ù×÷¡¢¼üÅ̺ÍÊó±êµÄ²Ù×÷£¬µÈµÈ£¬ÕâЩ²Ù×÷±ÈÆðÄÇЩͨ¹ý¹Ì¶¨Ëã·¨Éú³ÉµÄαËæ»úÊýÀ´Ëµ£¬µ±È»ÊǸüÕæʵһЩÁË£¬ÔÚÕâÀïÎÒÃÇÓÐÒ»¸öºÜ¿áµÄÃû×Ö½Ð×ö¡°ìØ¡±¡£ÄÚºËÌṩµÄ½Ó¿ÚÊÇ/dev/randomºÍ/dev/urandomÉ豸£¬¶þÕßµÄÇø±ðÊǶÁȡʱrandom¿Ï¶¨»á·µ»ØÒ»¸öÊý£¬Èç¹ûûÓÐ×ã¹»µÄÊý¾Ý£¬¾Í»á×èÈû¡£¶øurandomÔò²»»á×èÈû£¬µ«ÊDz»±£Ö¤·µ»ØµÄÊǺÏÊʵÄÊý¾Ý¡£
¡¡¡¡ÏÂÃæµÄ´úÂëÖУ¬º¯Êýinit_randomÓÃÀ´Éú³ÉÒ»¸öËæ»úµÄÖÖ×Ó£¬Ö®ºóÖ±½Óµ÷rand¾Í¿ÉÒԵõ½Ëæ»úÊý¡£Ö®ËùÒÔ¶ÁÁË512´Î£¬È»ºóÈ«²¿×éºÏÔÚÒ»Æ𣬾ÍÊÇÒòΪurandomÉ豸²»±£Ö¤Ã¿Ò»´Î¶Áµ½µÄ¶¼ÊÇÕæʵµÄÊý¾Ý¡£
¡¡¡¡ÁíÒ»¸öº¯Êýmy_randÔòÊÇͨ¹ýÖ±½ÓÈ¥¶ÁrandomÉ豸À´µÃµ½ÕæʵµÄËæ»úÊý¡£ÕâÑùÿһ´Î¶¼ÊÇÕæÕýµÄËæ»úÊý£¬µ«ÊÇÎÊÌâÔÚÓÚÈç¹ûϵͳµÄ¡°ìØ¡±²»¹»£¬ÄÇô³ÌÐò¾Í»á×èÈû¡£¶ÔÓÚ°²È«ÐÔÒªÇó±È½Ï¸ßµÄÓ¦ÓÃÀ´Ëµ£¬¿ÉÒÔʹÓÃÕâÑùµÄ·½Ê½¡£Èç¹û¡°ìØ¡±²»¹»£¬¿ÉÒÔÈËΪµÄÈ¥¡°ÖÆÔ족һЩìØ¡£±ÈÈçÏÂÃæµÄ³ÌÐò£¬Èç¹ûÄã²»×öÈκβÙ×÷£¬Ò²ÐíÊä³ö¼¸¸öËæ»úÊýÖ®ºó³ÌÐò¾Í»áÍ£Ö¹Êä³ö£¬ÕâÊÇÄãÔÚÁíÒ»¸öÖÕ¶ËÔËÐÐһЩ±È½Ï·±Ã¦µÄ½ø³Ì£¬±ÈÈç"find /"£¬¾Í»á·¢ÏÖÎÒÃǵijÌÐòÓÖ¿ªÊ¼Ô´Ô´²»¶ÏµÄÊä³öËæ»úÊý¡£
¡¡¡¡Ê¹ÓÃrandomÉ豸µÄÀý×Ӻܶ࣬±ÈÈçgpg¾Í»áÔÚÉú³ÉkeyµÄʱºòÈÃÄã²»¶ÏµÄÇüüÅÌÖ±µ½ËüÂúÒâΪֹ¡£¶ÔÓÚ2.4ÒÔºóµÄÄںˣ¬Ä㻹¿ÉÒÔͨ¹ýprocÎļþϵͳµÄ½Ó¿ÚµÃµ½randomÉ豸µÄ¸ü¶àÐÅÏ¢£¬±ÈÈç /proc/sys/kernel/random/entropy_avail¿ÉÒÔÖªµÀϵͳÖл¹ÓжàÉÙ¡°ìØ¡±¿ÉÒÔÓá£ÔËÐÐÎÒÃǵÄÀý×Ó¿ÉÒÔ·¢ÏÖ£¬Õâ¸öÖµÒ»ÏÂ×ӾͼõÉÙµ½¸öλÊý£¬Ö±µ½³ÌÐò×èÈû¡£ÁíÍâÒ»¸öÎļþÒ²ºÜÓÐÒâ˼£¬ÄǾÍÊÇ/proc/sys/kernel/random/uuid£¬Í¨¹ýÕâ¸ö½Ó¿Ú¿ÉÒÔºÜÈÝÒ׵ĵõ½ÕæÕýΨһµÄuuid¡£
¡¡¡¡¹ØÓÚrandomÉ豸»¹ÓÐһЩ¿É˵µÄ£¬ÄǾÍÊÇϵͳ°²È«¡£ÓÐÈ˾ÓÈ»ÄÜͨ¹ýϵͳÆô¶¯¹ý³Ì»òÕßϵͳÔËÐеÄij¶Îʱ¼ä²úÉúµÄìصÄÄÚÈÝÀ´ÆÆÒë³öijЩÐÅÏ¢£¬ÕâÌýÆðÀ´Ï൱¸ßÉ²»¹ýÀíÂÛÉÏÀ´½²È´ÍêÈ«¿ÉÄÜ¡£ÒòΪrandomÉ豸ËäÈ»ÊÇͨ¹ýϵͳÔëÉùÀ´µÃµ½ìØ£¬µ«ÊÇÈç¹ûÁ½´ÎϵͳÆô¶¯ÍêÈ«Ò»Ö£¬ÄÇôÆô¶¯¹ý³ÌÖÐÉú³ÉµÄìص±È»»áÍêÈ«Ò»Ö¡£²»¹ýÕâЩ°²È«Â©¶´²»ÓÃÎÒÃǵ£ÐÄ£¬ÒòΪÏÖÔÚµÄϵͳ¶¼ÓÐÏà¹ØµÄ²¹¶¡¡£
ÒýÓÃ:#include
#include
#include
void init_random ()
...{
unsigned int ticks;
struct timeval tv;
int fd;
gettimeofday (&tv, NULL);
ticks = tv.tv_sec + tv.tv_usec;
fd = open ("/dev/urandom", O_RDONLY);
if (fd > 0)
...{
unsigned int r;
int i;
for (i = 0; i < 512; i++)
...{
read (fd, &r, sizeof (r));
ticks += r;
}
close (fd);
}
srand (ticks);
printf("init finished ");
}
unsigned int new_rand ()
...{
int fd;
unsigned int n = 0;
fd = open ("/dev/urandom", O_RDONLY);
if (fd > 0)
...{
read (fd, &n, sizeof (n));
}
close (fd);
return n;
}
int main ()
...{
int n, i;
init_random ();
n = rand ();
printf ("n=%d ", n);
for(i=0;i<10;i++)
printf ("%u ", new_rand());
}
taotie ÓÚ 2010-09-09 14:14:34·¢±í:
:0)1