Module: OpenPGP::Random
- Defined in:
- lib/openpgp/random.rb
Class Method Summary collapse
-
.byte ⇒ Object
Generates a random byte.
-
.bytes(count, &block) ⇒ Object
Generates a string of random bytes.
-
.number(bits = 32, options = {}) ⇒ Object
Generates a random number.
-
.prime(bits, options = {}) ⇒ Object
Generates a pseudo-random prime number of the specified bit length.
Class Method Details
.byte ⇒ Object
Generates a random byte.
22 |
# File 'lib/openpgp/random.rb', line 22 def self.byte() bytes(1) end |
.bytes(count, &block) ⇒ Object
Generates a string of random bytes.
26 27 28 29 |
# File 'lib/openpgp/random.rb', line 26 def self.bytes(count, &block) octets = File.open('/dev/urandom', 'r') {|f| f.read(count) } # FIXME block_given? ? octets.each_byte(&block) : octets end |
.number(bits = 32, options = {}) ⇒ Object
Generates a random number.
5 6 7 8 9 |
# File 'lib/openpgp/random.rb', line 5 def self.number(bits = 32, = {}) octets = bytes((bits / 8.0).ceil).unpack('C*') number = octets.inject { |number, octet| number = (number << 8) | octet } number & ((1 << bits) - 1) end |
.prime(bits, options = {}) ⇒ Object
Generates a pseudo-random prime number of the specified bit length.
16 17 18 |
# File 'lib/openpgp/random.rb', line 16 def self.prime(bits, = {}) raise NotImplementedError # TODO end |