Method: OpenSSL::BN.rand_range

Defined in:
ossl_bn.c

.rand_range(range) ⇒ Object

Generates a cryptographically strong pseudo-random number in the range 0…range.

See also the man page BN_rand_range(3).



850
851
852
853
854
855
856
857
858
859
860
861
862
863
# File 'ossl_bn.c', line 850

static VALUE
ossl_bn_s_rand_range(VALUE klass, VALUE range)
{
    BIGNUM *bn = GetBNPtr(range), *result;
    VALUE obj = NewBN(klass);
    if (!(result = BN_new()))
        ossl_raise(eBNError, "BN_new");
    if (BN_rand_range(result, bn) <= 0) {
        BN_free(result);
        ossl_raise(eBNError, "BN_rand_range");
    }
    SetBN(obj, result);
    return obj;
}