Module: SecureRandom

Extended by:
Random::Formatter
Defined in:
opal/stdlib/securerandom.rb

Class Method Summary collapse

Class Method Details

.bytes(bytes = nil) ⇒ Object


49
50
51
# File 'opal/stdlib/securerandom.rb', line 49

def self.bytes(bytes = nil)
  gen_random(bytes)
end

.gen_random(count = nil) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
# File 'opal/stdlib/securerandom.rb', line 53

def self.gen_random(count = nil)
  count = Random._verify_count(count)
  out = ''
  %x{
    var bytes = gen_random_bytes(#{count});
    for (var i = 0; i < #{count}; i++) {
      out += String.fromCharCode(bytes[i]);
    }
  }
  out.encode('ASCII-8BIT')
end