class IOP::SecureRandomGenerator

Feed class to generate and send a random sequence of bytes of specified size.

This is the adapter for the standard Ruby SecureRandom module.

### Use case: generate 1024 bytes of random data and compute MD5 hash sum of it.

require 'iop/digest'
require 'iop/securerandom'
( IOP::SecureRandomGenerator.new(1024) | IOP::DigestComputer.new(Digest::MD5.new) ).process!

@since 0.1

Public Class Methods

new(size, block_size: DEFAULT_BLOCK_SIZE) click to toggle source

Creates class instance.

@param size [Integer] total random data size

@param block_size [Integer] size of block the data in split into

# File lib/iop/securerandom.rb, line 30
def initialize(size, block_size: DEFAULT_BLOCK_SIZE)
  @size = size
  @block_size = block_size
end

Public Instance Methods

process!() click to toggle source
# File lib/iop/securerandom.rb, line 35
def process!
  written = 0
  (0..@size/@block_size - 1).each do
    process(SecureRandom.bytes(@block_size))
    written += @block_size
  end
  left = @size - written
  process(SecureRandom.bytes(left)) unless left.zero?
  process
end