Class: ShiftCiphers::HardenedVigenere::RandomOffsetsStream

Inherits:
Object
  • Object
show all
Defined in:
lib/shift_ciphers/hardened_vigenere.rb

Constant Summary collapse

SEEDING_RAND_MAX =
2**31-1

Instance Method Summary collapse

Constructor Details

#initialize(key, max, initial_seed) ⇒ RandomOffsetsStream

Returns a new instance of RandomOffsetsStream.



48
49
50
51
52
53
# File 'lib/shift_ciphers/hardened_vigenere.rb', line 48

def initialize(key, max, initial_seed)
  @random = key.bytes.reduce(Random.new(initial_seed)) do |random, byte|
    Random.new(random.rand(SEEDING_RAND_MAX) ^ byte)
  end
  @max = max
end

Instance Method Details

#next(plaintext_char) ⇒ Object



55
56
57
58
59
# File 'lib/shift_ciphers/hardened_vigenere.rb', line 55

def next(plaintext_char)
  plaintext_byte = plaintext_char.bytes.reduce(0){|a,e| a ^ e}
  @random = Random.new(@random.rand(SEEDING_RAND_MAX) ^ plaintext_byte)
  @random.rand(@max)
end