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

version» Context lines:

pike.git/lib/modules/Crypto/randomness.pmod:8:    * will block if it can't provide enough random bits, while /dev/urandom    * will degenerate into a reasonably strong pseudo random generator */      #define RANDOM_DEVICE "/dev/random"   #define PRANDOM_DEVICE "/dev/urandom"      /* Collect somewhat random data from the environment. Unfortunately,    * this is quite system dependent */   #define PATH "/usr/sbin:/usr/etc:/usr/bin/:/sbin/:/etc:/bin"    - #ifndef __NT__ +    #define 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 - #define SYSTEM_COMMANDS ({ "mem /c", "arp -a", "vol", "dir", "net view", \ -  "net statistics workstation","net statistics server", "net view" \ -  "net user" }) - #endif +       #define PRIVATE      PRIVATE object global_rc4;      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); +  +  if(!res) +  throw(({ "Crypto.random: couldn't generate randomness\n", backtrace()})); +  +  destruct(ctx); +  +  return res; + #else /* !__NT__ */    string res;    object parent_pipe, child_pipe;    mapping env=getenv()+([]);       parent_pipe = Stdio.File();    child_pipe = parent_pipe->pipe();    if (!child_pipe)    throw( ({ "Crypto.random->popen: couldn't create pipe\n", backtrace() }) );       - #ifndef __NT__ +     object null=Stdio.File("/dev/null","rw");    env["PATH"]=PATH; - #else -  object null=Stdio.File("nul:","rw"); - #endif +        foreach(SYSTEM_COMMANDS, string cmd)    {    catch {    Process.create_process(Process.split_quoted_string(cmd),    (["stdin":null,    "stdout":child_pipe,    "stderr":null,    "env":env]));    };    }       destruct(child_pipe);       return parent_pipe->read(); -  + #endif   }         class pike_random {    string read(int len)    {    return sprintf("%@c", Array.map(allocate(len), lambda(int dummy)    {    return random(256);    } ));