Class: MailDaemon::Encryption

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_daemon/encryption.rb

Instance Method Summary collapse

Constructor Details

#initializeEncryption

Returns a new instance of Encryption.



6
7
8
9
# File 'lib/mail_daemon/encryption.rb', line 6

def initialize()
  @key = ENV["CASEBLOCKS_ENCRYPTION_KEY"] || "YLX0IBT+OXaO4mP2bVYqzMPbrrss8eUcX1XtgLxlVH8="
  @iv = ENV["CASEBLOCKS_ENCRYPTION_IV"] || "vvSVfoWvZQ3T/DfjsjO/9w=="
end

Instance Method Details

#decrypt(value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mail_daemon/encryption.rb', line 26

def decrypt(value)
  unless value.nil?
    decipher = OpenSSL::Cipher::AES.new(128, :CBC)
    decipher.decrypt
    decipher.key = Base64.decode64(@key)
    decipher.iv = Base64.decode64(@iv)

    encrypted = Base64.decode64(value)
    decipher.update(encrypted) + decipher.final
  else
    value
  end
end

#encrypt(data) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mail_daemon/encryption.rb', line 11

def encrypt(data)
  unless data.nil?
    cipher = OpenSSL::Cipher::AES.new(128, :CBC)
    cipher.encrypt
    cipher.key = Base64.decode64(@key)
    cipher.iv = Base64.decode64(@iv)
    encrypted = cipher.update(data) + cipher.final

    # [0..-2] strip off trailing carriage return
    Base64.encode64(encrypted)[0..-2]
  else
    data
  end
end