Class: Botan::RNG

Inherits:
Object
  • Object
show all
Defined in:
lib/botan/rng.rb

Overview

Random Number Generator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rng_type = nil) ⇒ RNG

Returns a new instance of RNG.

Parameters:

  • rng_type (String) (defaults to: nil)

    the type of RNG to create

Raises:



17
18
19
20
21
22
23
# File 'lib/botan/rng.rb', line 17

def initialize(rng_type = nil)
  ptr = FFI::MemoryPointer.new(:pointer)
  Botan.call_ffi(:botan_rng_init, ptr, rng_type)
  ptr = ptr.read_pointer
  raise Botan::Error, 'botan_rng_init returned NULL' if ptr.null?
  @ptr = FFI::AutoPointer.new(ptr, self.class.method(:destroy))
end

Instance Attribute Details

#ptrObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
# File 'lib/botan/rng.rb', line 15

def ptr
  @ptr
end

Class Method Details

.destroy(ptr) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'lib/botan/rng.rb', line 26

def self.destroy(ptr)
  LibBotan.botan_rng_destroy(ptr)
end

.get(length) ⇒ String

Retrieves some data from the default RNG.

Parameters:

  • length (Integer)

    the number of bytes to retrieve

Returns:

  • (String)


34
35
36
# File 'lib/botan/rng.rb', line 34

def self.get(length)
  RNG.new.get(length)
end

Instance Method Details

#get(length) ⇒ String

Retrieves some data from the RNG.

Parameters:

  • length (Integer)

    the number of bytes to retrieve

Returns:

  • (String)


51
52
53
54
55
# File 'lib/botan/rng.rb', line 51

def get(length)
  out_buf = FFI::MemoryPointer.new(:uint8, length)
  Botan.call_ffi(:botan_rng_get, @ptr, out_buf, length)
  out_buf.read_bytes(length)
end

#inspectObject



57
58
59
# File 'lib/botan/rng.rb', line 57

def inspect
  Botan.inspect_ptr(self)
end

#reseed(bits = 256) ⇒ self

Reseeds the RNG from the system RNG.

Parameters:

  • bits (Integer) (defaults to: 256)

    the number of bits to reseed with

Returns:

  • (self)


42
43
44
45
# File 'lib/botan/rng.rb', line 42

def reseed(bits = 256)
  Botan.call_ffi(:botan_rng_reseed, @ptr, bits)
  self
end