class ShiftCiphers::HardenedVigenere::RandomOffsetsStream

Constants

SEEDING_RAND_MAX

Public Class Methods

new(key, max, initial_seed) click to toggle source
# 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

Public Instance Methods

next(plaintext_char) click to toggle source
# 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