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

version» Context lines:

pike.git/lib/modules/Crypto/randomness.pmod:1: - /* $Id: randomness.pmod,v 1.18 2000/09/28 03:38:39 hubbe Exp $ + /* $Id: randomness.pmod,v 1.19 2001/04/27 13:35:23 grubba Exp $    */    - //! module Crypto - //! submodule randomness +    //! Assorted stronger or weaker randomnumber generators.    - /* These devices tries to collect entropy from the environment. + /* 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 */ +  * 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 */ +  * this is quite system dependent +  */   static constant PATH = "/usr/sbin:/usr/etc:/usr/bin/:/sbin/:/etc:/bin";      #ifndef __NT__   static constant SYSTEM_COMMANDS = ({    "last -256", "arp -a",    "netstat -anv","netstat -mv","netstat -sv",    "uptime","ps -fel","ps aux",    "vmstat -s","vmstat -M",    "iostat","iostat -cdDItx"   });   #else   static constant SYSTEM_COMMANDS = ({    "mem /c", "arp -a", "vol", "dir", "net view",    "net statistics workstation","net statistics server",    "net user"   });   #endif    - #define PRIVATE + // ***************************** + #define private    - PRIVATE object global_arcfour; + private object global_arcfour;    - // method string some_entropy() +    // Executes several programs to generate some entropy from their output. - PRIVATE string some_entropy() + private string some_entropy()   {   #ifdef __NT__    object ctx = Crypto.nt.CryptAcquireContext(0, 0, Crypto.nt.PROV_RSA_FULL,    Crypto.nt.CRYPT_VERIFYCONTEXT    /*|Crypto.nt.CRYPT_SILENT*/);    if(!ctx)    throw(({ "Crypto.random: couldn't create crypto context\n", backtrace()}));       string res = ctx->CryptGenRandom(8192);   
pike.git/lib/modules/Crypto/randomness.pmod:82:    };    }       destruct(child_pipe);       return parent_pipe->read();   #endif   }       - //! class pike_random +    //! A pseudo random generator based on the ordinary random() function.   class pike_random { -  //! method string read(int len) +     //! 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)    {
pike.git/lib/modules/Crypto/randomness.pmod:110:    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) - //! class arcfour_random +    //! A pseudo random generator based on the arcfour crypto.   class arcfour_random { -  +     inherit Crypto.arcfour : arcfour;    -  //! method void create(string secret) +     //! Initialize and seed the arcfour random generator.    void create(string secret)    {    object hash = Crypto.sha();    hash->update(secret);       arcfour::set_encrypt_key(hash->digest());    }    -  //! method string read(int len) +     //! 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) */