Class: VIGENERE::EmailCipher
- Inherits:
-
Object
- Object
- VIGENERE::EmailCipher
- Defined in:
- lib/vigenere/email_cipher.rb
Instance Method Summary collapse
- #alpha_decode(str) ⇒ Object
- #alpha_encode(str) ⇒ Object
- #convert_dashes_to_uppercase(str) ⇒ Object
- #convert_upercase_to_dashes(str) ⇒ Object
- #create_bitly(str) ⇒ Object
- #create_email_address(from, to, domain) ⇒ Object
- #cycle(length) ⇒ Object
- #decode(str) ⇒ Object
- #encode(str) ⇒ Object
- #expand_bitly(email) ⇒ Object
-
#initialize(args = {}) ⇒ EmailCipher
constructor
A new instance of EmailCipher.
- #parse(email_address) ⇒ Object
- #split_addresses(str) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ EmailCipher
Returns a new instance of EmailCipher.
5 6 7 8 9 10 11 12 13 |
# 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 |
Instance Method Details
#alpha_decode(str) ⇒ Object
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 68 |
# 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) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# 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) ⇒ Object
138 139 140 141 142 143 |
# 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) ⇒ Object
128 129 130 131 132 133 134 135 136 |
# 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) ⇒ Object
122 123 124 125 126 |
# 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) ⇒ Object
145 146 147 148 149 |
# 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) ⇒ Object
15 16 17 18 19 20 |
# 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) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# 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) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# 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) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/vigenere/email_cipher.rb', line 115 def (email) bitly_shortcut = email.gsub!(/(fb_user_|@gofundbase.com)/, '') bitly_shortcut = convert_dashes_to_uppercase(bitly_shortcut.downcase) long_url = Bitly.client.("http://bit.ly/#{bitly_shortcut}").long_url long_url.gsub!(/(\/|\.|http:)/,'') end |
#parse(email_address) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/vigenere/email_cipher.rb', line 105 def parse( email_address ) encoded_addresses_string = (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) ⇒ Object
98 99 100 101 102 103 |
# 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 |