Class: IOP::SecureRandomGenerator
- Inherits:
-
Object
- Object
- IOP::SecureRandomGenerator
- 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!
Instance Attribute Summary
Attributes included from Feed
Instance Method Summary collapse
-
#initialize(size, block_size: DEFAULT_BLOCK_SIZE) ⇒ SecureRandomGenerator
constructor
Creates class instance.
- #process! ⇒ Object
Methods included from Feed
Constructor Details
#initialize(size, block_size: DEFAULT_BLOCK_SIZE) ⇒ SecureRandomGenerator
Creates class instance.
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
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 |