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

version» Context lines:

pike.git/lib/modules/Crypto/randomness.pmod:1: - /* $Id: randomness.pmod,v 1.14 1999/12/01 22:37:22 marcus Exp $ + /* $Id: randomness.pmod,v 1.15 2000/03/28 12:24:32 grubba Exp $    */      //! module Crypto   //! submodule randomness   //! Assorted stronger or weaker randomnumber generators.      /* These devices tries 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.git/lib/modules/Crypto/randomness.pmod:28:   #else   static constant SYSTEM_COMMANDS = ({    "mem /c", "arp -a", "vol", "dir", "net view",    "net statistics workstation","net statistics server",    "net user"   });   #endif      #define PRIVATE    - PRIVATE object global_rc4; + PRIVATE object global_arcfour;      // method string some_entropy()   // Executes several programs to generate some entropy from their output.   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)
pike.git/lib/modules/Crypto/randomness.pmod:103: Inside #if 1 // 30% optimization /Hubbe
   }    ret+=(string)allocate(len % 16384, random)(256);    return ret;   #else    if (len > 16384) return read(len/2)+read(len-len/2);    return (string)allocate(len, random)(256);   #endif    }   }    - #if constant(Crypto.rc4) - //! class rc4_random - //! A pseudo random generator based on the rc4 crypto. - class rc4_random { -  inherit Crypto.rc4 : rc4; + #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 rc4 random generator. +  //! Initialize and seed the arcfour random generator.    void create(string secret)    {    object hash = Crypto.sha();    hash->update(secret);    -  rc4::set_encrypt_key(hash->digest()); +  arcfour::set_encrypt_key(hash->digest());    }       //! method string read(int len)    //! Return a string of the next len random characters from the -  //! rc4 random generator. +  //! arcfour random generator.    string read(int len)    {    if (len > 16384) return read(len/2)+read(len-len/2); -  return rc4::crypt("\47" * len); +  return arcfour::crypt("\47" * len);    }   }    - #endif /* constant(Crypto.rc4) */ + #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_rc4) -  return global_rc4; +  if (global_arcfour) +  return global_arcfour;    - #if constant(Crypto.rc4) + #if constant(Crypto.arcfour)    string seed = some_entropy();    if (strlen(seed) > 2000) -  return (global_rc4 = rc4_random(sprintf("%4c%O%s", time(), _memory_usage(), seed))); - #else /* !constant(Crypto.rc4) */ +  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_rc4 = pike_random(); - #endif /* constant(Crypto.rc4) */ +  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;