pike.git / lib / modules / Crypto / randomness.pmod

version» Context lines:

pike.git/lib/modules/Crypto/randomness.pmod:1: - /* $Id: randomness.pmod,v 1.19 2001/04/27 13:35:23 grubba Exp $ + /* $Id: randomness.pmod,v 1.20 2001/11/08 01:45:39 nilsson Exp $    */    - //! Assorted stronger or weaker randomnumber generators. + //! Assorted stronger or weaker randomnumber generators. + //! These devices try to collect entropy from the environment. + //! They differ in behaviour when they run low on entropy, /dev/random + //! will block if it can't provide enough random bits, while /dev/urandom + //! will degenerate into a reasonably strong pseudo random generator    - /* These devices try to collect entropy from the environment. -  * They differ in behaviour when they run low on entropy, /dev/random -  * will block if it can't provide enough random bits, while /dev/urandom -  * will degenerate into a reasonably strong pseudo random generator -  */ -  +    #pike __REAL_VERSION__      static constant RANDOM_DEVICE = "/dev/random";   static constant PRANDOM_DEVICE = "/dev/urandom";      /* Collect somewhat random data from the environment. Unfortunately,    * this is quite system dependent    */   static constant PATH = "/usr/sbin:/usr/etc:/usr/bin/:/sbin/:/etc:/bin";   
pike.git/lib/modules/Crypto/randomness.pmod:82:    };    }       destruct(child_pipe);       return parent_pipe->read();   #endif   }       - //! A pseudo random generator based on the ordinary random() function. + //! A pseudo random generator based on the ordinary random() function.   class pike_random {    -  //! Returns a string of length len with pseudo random values. +  //! Returns a string of length len with pseudo random values.    string read(int len)    {   #if 1 // major optimization /Hubbe    return random_string(len);   #else   #if 1 // 30% optimization /Hubbe    string ret="";    if(len>=16384)    {    array x=allocate(16384,random);
pike.git/lib/modules/Crypto/randomness.pmod:109:    return ret;   #else    if (len > 16384) return read(len/2)+read(len-len/2);    return (string)allocate(len, random)(256);   #endif   #endif    }   }      #if constant(Crypto.arcfour) - //! A pseudo random generator based on the arcfour crypto. + //! A pseudo random generator based on the arcfour crypto.   class arcfour_random {       inherit Crypto.arcfour : arcfour;    -  //! Initialize and seed the arcfour random generator. +  //! Initialize and seed the arcfour random generator.    void create(string secret)    {    object hash = Crypto.sha();    hash->update(secret);       arcfour::set_encrypt_key(hash->digest());    }    -  //! Return a string of the next len random characters from the -  //! arcfour random generator. +  //! Return a string of the next len random characters from the +  //! arcfour random generator.    string read(int len)    {    if (len > 16384) return read(len/2)+read(len-len/2);    return arcfour::crypt("\47" * len);    }   }      #endif /* constant(Crypto.arcfour) */    -  + //!   object reasonably_random()   {    if (file_stat(PRANDOM_DEVICE))    {    object res = Stdio.File();    if (res->open(PRANDOM_DEVICE, "r"))    return res;    }       if (global_arcfour)
pike.git/lib/modules/Crypto/randomness.pmod:157: Inside #if constant(Crypto.arcfour)
   string seed = some_entropy();    if (strlen(seed) > 2000)    return (global_arcfour = arcfour_random(sprintf("%4c%O%s", time(), _memory_usage(), seed)));   #else /* !constant(Crypto.arcfour) */    /* Not very random, but at least a fallback... */    return global_arcfour = pike_random();   #endif /* constant(Crypto.arcfour) */    throw( ({ "Crypto.randomness.reasonably_random: No source found\n", backtrace() }) );   }    + //!   object really_random(int|void may_block)   {    object res = Stdio.File();    if (may_block && file_stat(RANDOM_DEVICE))    {    if (res->open(RANDOM_DEVICE, "r"))    return res;    }       if (file_stat(PRANDOM_DEVICE))    {    if (res->open(PRANDOM_DEVICE, "r"))    return res;    }       throw( ({ "Crypto.randomness.really_random: No source found\n", backtrace() }) );   }