1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
# encoding: binary
# frozen_string_literal: true
module RbNaCl
# 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.
module Random
extend Sodium
@mutex = Mutex.new
sodium_function :c_random_bytes,
:randombytes_buf,
%i[pointer size_t]
# Returns a string of random bytes
#
# @param [Integer] n number of random bytes desired
#
# @return [String] random bytes.
def self.random_bytes(n = 32)
buf = RbNaCl::Util.zeros(n)
@mutex.synchronize { c_random_bytes(buf, n) }
buf
end
end
end
|