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
54
# File 'lib/shift_ciphers/hardened_vigenere.rb', line 48

def initialize(key, max, initial_seed)
  @key_stream = key.bytes.cycle
  @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



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

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