# module OpenSSL::Random

### Public Class Methods

egd(filename) → true
Source

    static VALUE
    ossl_rand_egd(VALUE self, VALUE filename)
    {
        if (RAND_egd(StringValueCStr(filename)) == -1) {
            ossl_raise(eRandomError, NULL);
        }
        return Qtrue;
    }

Same as `::egd_bytes` but queries 255 bytes by default.

egd_bytes(filename, length) → true
Source

    static VALUE
    ossl_rand_egd_bytes(VALUE self, VALUE filename, VALUE len)
    {
        int n = NUM2INT(len);

        if (RAND_egd_bytes(StringValueCStr(filename), n) == -1) {
            ossl_raise(eRandomError, NULL);
        }
        return Qtrue;
    }

Queries the entropy gathering daemon EGD on socket path given by filename.

Fetches length number of bytes and uses ::add to seed the `OpenSSL` built-in PRNG.

load_random_file(filename) → true
Source

    static VALUE
    ossl_rand_load_file(VALUE self, VALUE filename)
    {
        if(!RAND_load_file(StringValueCStr(filename), -1)) {
            ossl_raise(eRandomError, NULL);
        }
        return Qtrue;
    }

Reads bytes from filename and adds them to the PRNG.

add(str, entropy) → self
Source

    static VALUE
    ossl_rand_add(VALUE self, VALUE str, VALUE entropy)
    {
        StringValue(str);
        RAND_add(RSTRING_PTR(str), RSTRING_LENINT(str), NUM2DBL(entropy));

        return self;
    }

Mixes the bytes from str into the Pseudo `Random` Number Generator(PRNG) state.

Thus, if the data from str are unpredictable to an adversary, this increases the uncertainty about the state and makes the PRNG output less predictable.

The entropy argument is (the lower bound of) an estimate of how much randomness is contained in str, measured in bytes.

### Example¶ ↑

    pid = $$
    now = Time.now
    ary = [now.to_i, now.nsec, 1000, pid]
    OpenSSL::Random.add(ary.join, 0.0)
    OpenSSL::Random.seed(ary.join)

random_bytes(length) → string
Source

    static VALUE
    ossl_rand_bytes(VALUE self, VALUE len)
    {
        VALUE str;
        int n = NUM2INT(len);
        int ret;

        str = rb_str_new(0, n);
        ret = RAND_bytes((unsigned char *)RSTRING_PTR(str), n);
        if (ret == 0) {
            ossl_raise(eRandomError, "RAND_bytes");
        } else if (ret == -1) {
            ossl_raise(eRandomError, "RAND_bytes is not supported");
        }

        return str;
    }

Generates a `String` with length number of cryptographically strong pseudo-random bytes.

### Example¶ ↑

    OpenSSL::Random.random_bytes(12)
    #=> "..."

seed(str) → str
Source

    static VALUE
    ossl_rand_seed(VALUE self, VALUE str)
    {
        StringValue(str);
        RAND_seed(RSTRING_PTR(str), RSTRING_LENINT(str));

        return str;
    }

`::seed` is equivalent to ::add where entropy is length of str.

status? → true | false
Source

    static VALUE
    ossl_rand_status(VALUE self)
    {
        return RAND_status() ? Qtrue : Qfalse;
    }

Return `true` if the PRNG has been seeded with enough data, `false` otherwise.

write_random_file(filename) → true
Source

    static VALUE
    ossl_rand_write_file(VALUE self, VALUE filename)
    {
        if (RAND_write_file(StringValueCStr(filename)) == -1) {
            ossl_raise(eRandomError, NULL);
        }
        return Qtrue;
    }

Writes a number of random generated bytes (currently 1024) to filename which can be used to initialize the PRNG by calling `::load_random_file` in a later session.