Class: IOP::SecureRandomGenerator

Inherits:
Object
  • Object
show all
Includes:
Feed
Defined in:
lib/iop/securerandom.rb

Overview

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

This is the adapter for standard SecureRandom generator 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

Instance Attribute Summary

Attributes included from Feed

#downstream

Instance Method Summary collapse

Methods included from Feed

#process, #|

Constructor Details

#initialize(size, block_size: DEFAULT_BLOCK_SIZE) ⇒ SecureRandomGenerator

Creates class instance.

Parameters:

  • size (Integer)

    total random data size

  • block_size (Integer) (defaults to: DEFAULT_BLOCK_SIZE)

    size of block the data in split into

Since:

  • 0.1



30
31
32
33
# File 'lib/iop/securerandom.rb', line 30

def initialize(size, block_size: DEFAULT_BLOCK_SIZE)
  @size = size
  @block_size = block_size
end

Instance Method Details

#process!Object

Since:

  • 0.1



35
36
37
38
39
40
41
42
43
44
# 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