Sha256: 6277dad4fe479cf8712cecd88323e58fe5d2cc0f0b9c0a0f3f13df0c2f9c85fc

Contents?: true

Size: 724 Bytes

Versions: 3

Compression:

Stored size: 724 Bytes

Contents

require "thread"

# encoding: binary
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,
                    [:pointer, :ulong_long]
    # 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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rbnacl-3.4.0 lib/rbnacl/random.rb
rbnacl-3.3.0 lib/rbnacl/random.rb
rbnacl-3.2.0 lib/rbnacl/random.rb