Class: VIGENERE::EmailCipher

Inherits:
Object
  • Object
show all
Defined in:
lib/vigenere/email_cipher.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ EmailCipher

Returns a new instance of EmailCipher.



3
4
5
6
7
8
9
10
11
12
# File 'lib/vigenere/email_cipher.rb', line 3

def initialize(args = {})
  @alphabet = []

  # TODO: Add any characters that need to work here
  @alphabet.concat(('a'..'z').to_a)
  @alphabet.concat(('A'..'Z').to_a)
  @alphabet.concat(('0'..'9').to_a)
  @alphabet.concat(['&','*','+','-','/','=','?','^','_','`','~','.'])
  @key = args[:key]
end

Instance Method Details

#alpha_decode(str) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vigenere/email_cipher.rb', line 39

def alpha_decode(str)
  decoded = ''
  escaping = false
  escaped = false
  str.chars.each_with_index do |char, i|
    if escaping then
      decoded << 'a' if char == '0'
      decoded << '@' if char == '1'
      decoded << '&' if char == '2'
      decoded << '*' if char == '3'
      decoded << '+' if char == '4'
      decoded << '-' if char == '5'
      decoded << '/' if char == '6'
      decoded << '=' if char == '7'
      decoded << '?' if char == '8'
      decoded << '^' if char == '9'
      decoded << '_' if char == 'a'
      decoded << '`' if char == 'b'
      decoded << '~' if char == 'c'
      decoded << '.' if char == 'd'
      escaping = false
    elsif char == 'a' then
      escaping = true
    else
      decoded << char
    end
  end
  decoded
end

#alpha_encode(str) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vigenere/email_cipher.rb', line 21

def alpha_encode(str)
  str = str.gsub('a','a0')
  str = str.gsub('@','a1')
  str = str.gsub('&','a2')
  str = str.gsub('*','a3')
  str = str.gsub('+','a4')
  str = str.gsub('-','a5')
  str = str.gsub('/','a6')
  str = str.gsub('=','a7')
  str = str.gsub('?','a8')
  str = str.gsub('^','a9')
  str = str.gsub('_','aa')
  str = str.gsub('`','ab')
  str = str.gsub('~','ac')
  str = str.gsub('.','ad')
  str
end

#cycle(length) ⇒ Object



14
15
16
17
18
19
# File 'lib/vigenere/email_cipher.rb', line 14

def cycle(length)
  @key.chars.cycle.inject('') do |str, char|
    return str if str.length == length
    str + char
  end
end

#decode(str) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/vigenere/email_cipher.rb', line 83

def decode(str)
  cycled_key = cycle str.length
  decoded = ''
  str = alpha_decode str
  str.chars.each_with_index do |char, i|
    cipher_index = @alphabet.find_index(cycled_key[i])
    char_index = @alphabet.find_index(char)
    dec_char_index = (char_index - cipher_index) % @alphabet.length
    dec_char = @alphabet[dec_char_index]
    decoded << dec_char
  end
  alpha_decode decoded
end

#encode(str) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/vigenere/email_cipher.rb', line 69

def encode(str)
  encoded = ''
  str = alpha_encode str
  cycled_key = cycle (str.length)
  str.chars.each_with_index do |char, i|
    cipher_index = @alphabet.find_index(cycled_key[i])
    char_index = @alphabet.find_index(char)
    enc_char_index = (char_index + cipher_index) % @alphabet.length
    enc_char = @alphabet[enc_char_index]
    encoded << enc_char
  end
  alpha_encode encoded
end