From: gabriele renzi Date: 2004-03-22T21:44:54+09:00 Subject: how to create random numbers with OpenSSL ? Hi gurus and nubys, I wonder what is the procedure to create crypto-strong random numbers using OpenSSL. It seem that I should use OpenSSL::Random or OpenSSL::BN, but I don't know how. Random does not seem to have a method to generate numbers, while BN do. BTW, the BN_* functions in OpenSSL docs say that I should seed the prng. I believe that I should call Random.seed, correct? Then, BN.rand asks me for an argument more. OpenSSL docs say that: int BN_rand(BIGNUM *rnd, int bits, int top, int bottom); BN_rand() generates a cryptographically strong pseudo-random number of bits bits in length and stores it in rnd. If top is -1, the most significant bit of the random number can be zero. If top is 0, it is set to 1, and if top is 1, the two most significant bits of the number will be set to 1, so that the product of two such random numbers will always have 2*bits length. If bottom is true, the number will be odd. what am I passing to the BN.rand ruby method? I believe this makes sense: irb(main):001:0> require 'openssl' => true irb(main):002:0> include OpenSSL => Object irb(main):003:0> Random.seed Time.now.to_s # wants a char* => "Mon Mar 22 12:41:37 ora solare Europa occidentale 2004" irb(main):004:0> BN.rand 10 => 911 irb(main):005:0> BN.rand 10 => 927 irb(main):006:0> BN.rand 10 => 669 But I actually don't know why it works. It seem that this argument relates with the size of the number, thus mapping to the "bits" argument of the C function: irb(main):016:0> BN.rand 1 => 1 irb(main):017:0> BN.rand 100 => 1200934617331004658893671293273 irb(main):018:0> BN.rand 1000 => 67828525440791600922714959563204722534244040648022725704586283705093551497878 65306083030679708261616701006619918719292418247784471100385905740725701505560535 87496275051260321531557402897722416641791658838262060614015710574928894857388599 4148133750954390395099101176573040312500358316315972900721884786 is this correct? It's anyway interesting tha the first number generation takes more time than the others. Is it gathering entropy? thanks in advance