Sha256: c0de1c538f07cfe736693fedf46a0f7a22f9c41e25f12e28f6fb5c12ef5650bc

Contents?: true

Size: 756 Bytes

Versions: 5

Compression:

Stored size: 756 Bytes

Contents

# encoding: binary
# frozen_string_literal: true

require "thread"

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

5 entries across 5 versions & 1 rubygems

Version Path
rbnacl-5.0.0 lib/rbnacl/random.rb
rbnacl-4.0.2 lib/rbnacl/random.rb
rbnacl-4.0.1 lib/rbnacl/random.rb
rbnacl-4.0.0 lib/rbnacl/random.rb
rbnacl-4.0.0.pre lib/rbnacl/random.rb