Module: OpenPGP::Random

Defined in:
lib/openpgp/random.rb

Class Method Summary (collapse)

Class Method Details

+ (String) byte

Generates a random byte.

Returns:

  • (String)


31
# File 'lib/openpgp/random.rb', line 31

def self.byte() bytes(1) end

+ (String) bytes(count, &block)

Generates a string of random bytes.

Parameters:

  • (Integer) count

Returns:

  • (String)


38
39
40
41
# File 'lib/openpgp/random.rb', line 38

def self.bytes(count, &block)
  octets = File.open('/dev/urandom', 'r') {|f| f.read(count) } # FIXME
  block_given? ? octets.each_byte(&block) : octets
end

+ (Integer) number(bits = 32, options = {})

Generates a random number.

Parameters:

  • (Integer) bits (defaults to: 32)
  • (Hash{Symbol => Object}) options (defaults to: {})

Returns:

  • (Integer)


9
10
11
12
13
# File 'lib/openpgp/random.rb', line 9

def self.number(bits = 32, options = {})
  octets = bytes((bits / 8.0).ceil).unpack('C*')
  number = octets.inject { |number, octet| number = (number << 8) | octet }
  number & ((1 << bits) - 1)
end

+ (Integer) prime(bits, options = {})

Generates a pseudo-random prime number of the specified bit length.

Parameters:

  • (Integer) bits
  • (Hash{Symbol => Object}) options (defaults to: {})

Returns:

  • (Integer)

Raises:

  • (NotImplementedError)

See Also:



23
24
25
# File 'lib/openpgp/random.rb', line 23

def self.prime(bits, options = {})
  raise NotImplementedError # TODO
end