Module: RbNaCl::Random

Extended by:
Sodium
Defined in:
lib/rbnacl/random.rb

Overview

Functions for random number generation

This uses the underlying source of random number generation on the OS, so /dev/urandom on UNIX-like systems, and the MS crypto providor on windows.

Class Method Summary collapse

Methods included from Sodium

primitive, sodium_constant, sodium_function, sodium_function_with_return_code, sodium_primitive, sodium_type

Class Method Details

.random_bytes(n = 32) ⇒ String

Returns a string of random bytes

Parameters:

  • n (Integer) (defaults to: 32)

    number of random bytes desired

Returns:

  • (String)

    random bytes.



23
24
25
26
27
# File 'lib/rbnacl/random.rb', line 23

def self.random_bytes(n = 32)
  buf = RbNaCl::Util.zeros(n)
  @mutex.synchronize { c_random_bytes(buf, n) }
  buf
end