class Ciphers::Autokey
Attributes
alphabet[R]
key[R]
Public Class Methods
new(alphabet: Ciphers::LATIN, key: )
click to toggle source
# File lib/ciphers/autokey.rb, line 3 def initialize(alphabet: Ciphers::LATIN, key: ) alphabet = alphabet.chars if alphabet.is_a? String @alphabet = alphabet.to_a.freeze @key = key.upcase.freeze end
Public Instance Methods
decrypt(string)
click to toggle source
# File lib/ciphers/autokey.rb, line 19 def decrypt(string) string.each_char.with_index.with_object('') do |(char, i), ret| ret << char and next unless alphabet.include?(char) key_char = "#{key}#{ret}"[i] row = table.fetch key_char col = row.index(char) ret << alphabet[col] end end
encrypt(string)
click to toggle source
# File lib/ciphers/autokey.rb, line 9 def encrypt(string) string.each_char.with_index.with_object('') do |(char, i), ret| ret << char and next unless alphabet.include?(char) key_char = "#{key}#{string}"[i] row = table.fetch key_char col = alphabet.index(char) ret << row[col] end end
Private Instance Methods
table()
click to toggle source
# File lib/ciphers/autokey.rb, line 33 def table @table = {}.tap do |hash| alphabet.each_with_index {|char, i| hash[char] = alphabet.rotate(i) } end end