Class: Solitaire::Cipher
- Defined in:
- lib/quiz1/t/solutions/Moses Hohman/cipher.rb
Constant Summary collapse
- ENCRYPTED_TEXT_PATTERN =
/\A[A-Z]{5}( [A-Z]{5})*\Z/
Instance Attribute Summary collapse
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Instance Method Summary collapse
- #crypt ⇒ Object
-
#initialize(text, deck = Deck.new) ⇒ Cipher
constructor
A new instance of Cipher.
Constructor Details
#initialize(text, deck = Deck.new) ⇒ Cipher
Returns a new instance of Cipher.
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/quiz1/t/solutions/Moses Hohman/cipher.rb', line 74 def initialize(text, deck=Deck.new) @chunker = Chunker.new(text) keystream = Keystream.new(deck) @keystream_chunker = Chunker.new(keystream.keystream_letters(@chunker.chunks).join) if text =~ ENCRYPTED_TEXT_PATTERN @mode = "decrypt" @calc_number = proc { |num, keystream_num| num - keystream_num } else @mode = "encrypt" @calc_number = proc { |num, keystream_num| num + keystream_num } end end |
Instance Attribute Details
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
86 87 88 |
# File 'lib/quiz1/t/solutions/Moses Hohman/cipher.rb', line 86 def mode @mode end |
Instance Method Details
#crypt ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/quiz1/t/solutions/Moses Hohman/cipher.rb', line 88 def crypt ciphered = [@chunker.number_chunks, @keystream_chunker.number_chunks].collect_peel do |num_chunk, keystream_num_chunk| [num_chunk, keystream_num_chunk].collect_peel do |num, keystream_num| @calc_number.call(num,keystream_num).offset_mod(ALPHABET_SIZE) end end Chunker.to_letters(ciphered).join(" ").rstrip end |