Method: OpenSSL::Random.random_bytes

Defined in:
ossl_rand.c

.random_bytes(len) ⇒ Object

random_bytes(length) -> string

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

Example

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


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'ossl_rand.c', line 105

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;
}