module SecureRandom

Secure random number generation using system RNG facilities

Constants

DEFAULT_LENGTH

For some reason SecureRandom defaults to 16 bytes

VERSION

Public Class Methods

Sysrandom#__random_uint32 → Integer click to toggle source

Generates a random unsigned 32-bit integer using the OS CSPRNG

static VALUE
Sysrandom_random_uint32(VALUE self)
{
    return UINT2NUM(__randombytes_sysrandom());
}
Sysrandom#random_bytes(n=nil) → String click to toggle source

::random_bytes generates a random binary string via the OS CSPRNG.

The argument n specifies how long the resulting string should be.

For compatibility with SecureRandom, if n is not specified, 16 is assumed, and if n is 0, an empty string is returned.

The resulting string may contain any byte (i.e. “x00” - “xff”)

static VALUE
Sysrandom_random_bytes(int argc, VALUE * argv, VALUE self)
{
    VALUE n_obj, str;
    int n;

    if (rb_scan_args(argc, argv, "01", &n_obj) == 1) {
        if(n_obj == Qnil) {
            n = DEFAULT_N_BYTES;
        } else {
            n = NUM2INT(n_obj);

            if(n < 0) {
              rb_raise(rb_eArgError, "negative string size");
            }
        }
    } else {
        n = DEFAULT_N_BYTES;
    }

    if(n > 0) {
        str = rb_str_new(0, n);
        __randombytes_sysrandom_buf(RSTRING_PTR(str), n);
    } else {
        // n == 0, return empty string
        str = rb_str_new(0, 0);
    }

    return str;
}

Public Instance Methods

Sysrandom#__random_uint32 → Integer click to toggle source

Generates a random unsigned 32-bit integer using the OS CSPRNG

static VALUE
Sysrandom_random_uint32(VALUE self)
{
    return UINT2NUM(__randombytes_sysrandom());
}
Also aliased as: __random_uint32
base64(n = nil) click to toggle source
# File lib/sysrandom.rb, line 53
def base64(n = nil)
  Base64.encode64(random_bytes(n)).chomp
end
hex(n = nil) click to toggle source
# File lib/sysrandom.rb, line 62
def hex(n = nil)
  random_bytes(n).unpack("h*").first
end
Sysrandom#random_bytes(n=nil) → String click to toggle source

::random_bytes generates a random binary string via the OS CSPRNG.

The argument n specifies how long the resulting string should be.

For compatibility with SecureRandom, if n is not specified, 16 is assumed, and if n is 0, an empty string is returned.

The resulting string may contain any byte (i.e. “x00” - “xff”)

static VALUE
Sysrandom_random_bytes(int argc, VALUE * argv, VALUE self)
{
    VALUE n_obj, str;
    int n;

    if (rb_scan_args(argc, argv, "01", &n_obj) == 1) {
        if(n_obj == Qnil) {
            n = DEFAULT_N_BYTES;
        } else {
            n = NUM2INT(n_obj);

            if(n < 0) {
              rb_raise(rb_eArgError, "negative string size");
            }
        }
    } else {
        n = DEFAULT_N_BYTES;
    }

    if(n > 0) {
        str = rb_str_new(0, n);
        __randombytes_sysrandom_buf(RSTRING_PTR(str), n);
    } else {
        // n == 0, return empty string
        str = rb_str_new(0, 0);
    }

    return str;
}
Also aliased as: random_bytes
random_number(n = 0) click to toggle source
# File lib/sysrandom.rb, line 42
def random_number(n = 0)
  result = __random_uint32 / (2**32).to_f

  if n <= 0
    result
  else
    result *= n
    n.is_a?(Integer) ? result.floor : result
  end
end
urlsafe_base64(n = nil, padding = false) click to toggle source
# File lib/sysrandom.rb, line 57
def urlsafe_base64(n = nil, padding = false)
  result = Base64.urlsafe_encode64(random_bytes(n)).chomp
  padding ? result : result.tr("=", "")
end
uuid() click to toggle source
# File lib/sysrandom.rb, line 66
def uuid
  values = hex(16).match(/\A(.{8})(.{4})(.)(.{3})(.)(.{3})(.{12})\z/)
  "#{values[1]}-#{values[2]}-4#{values[4]}-#{'89ab'[values[5].ord % 4]}#{values[6]}-#{values[7]}"
end