class VIGENERE::EmailCipher

Public Class Methods

new(args = {}) click to toggle source
# File lib/vigenere/email_cipher.rb, line 5
def initialize(args = {})
  @alphabet = []

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

Public Instance Methods

alpha_decode(str) click to toggle source
# File lib/vigenere/email_cipher.rb, line 40
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) click to toggle source
# File lib/vigenere/email_cipher.rb, line 22
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
convert_dashes_to_uppercase(str) click to toggle source
# File lib/vigenere/email_cipher.rb, line 138
def convert_dashes_to_uppercase(str)
  str.scan(/-\w/).each do |match|
    str[match] = match[1].upcase
  end
  str
end
convert_upercase_to_dashes(str) click to toggle source
# File lib/vigenere/email_cipher.rb, line 128
def convert_upercase_to_dashes(str)
  str = str.split('')
  str.each_with_index do |char, index|
    if /[[:upper:]]/.match(char)
      str[index] = "-#{char.downcase}"
    end
  end
  str.join
end
create_bitly(str) click to toggle source
# File lib/vigenere/email_cipher.rb, line 122
def create_bitly(str)
  str = "http://#{str}".scan(/.{1,24}/).join('.')
  bitly_shortcut = Bitly.client.shorten(str).user_hash
  convert_upercase_to_dashes(bitly_shortcut)
end
create_email_address(from, to, domain) click to toggle source
# File lib/vigenere/email_cipher.rb, line 145
def create_email_address(from, to, domain)
  encrypted_email = "#{encode(from)}_#{encode(to)}"
  bitly_shortcut = create_bitly(encrypted_email)
  "fb_user_#{bitly_shortcut}@#{domain}"
end
cycle(length) click to toggle source
# File lib/vigenere/email_cipher.rb, line 15
def cycle(length)
  @key.chars.cycle.inject('') do |str, char|
    return str if str.length == length
    str + char
  end
end
decode(str) click to toggle source
# File lib/vigenere/email_cipher.rb, line 84
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) click to toggle source
# File lib/vigenere/email_cipher.rb, line 70
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
expand_bitly(email) click to toggle source
# File lib/vigenere/email_cipher.rb, line 115
def expand_bitly(email)
  bitly_shortcut = email.gsub!(/(fb_user_|@gofundbase.com)/, '')
  bitly_shortcut = convert_dashes_to_uppercase(bitly_shortcut.downcase)
  long_url = Bitly.client.expand("http://bit.ly/#{bitly_shortcut}").long_url
  long_url.gsub!(/(\/|\.|http:)/,'')
end
parse( email_address ) click to toggle source
# File lib/vigenere/email_cipher.rb, line 105
def parse( email_address )
  encoded_addresses_string = expand_bitly(email_address)
  encoded_addresses = split_addresses encoded_addresses_string

  from_addr = decode(encoded_addresses[:from])
  to_addr = decode(encoded_addresses[:to])

  {from: from_addr, to: to_addr}
end
split_addresses(str) click to toggle source
# File lib/vigenere/email_cipher.rb, line 98
def split_addresses(str)
  from = str[0, str.index('_')]
  to = str[str.index('_')+1, str.length]

  addresses = { from: from, to: to }
end