Class: Mail2cb::Encryption

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

Instance Method Summary collapse

Constructor Details

#initialize(auth_token) ⇒ Encryption

def initialize()

@key = ENV["CASEBLOCKS_ENCRYPTION_KEY"] || "YLX0IBT+OXaO4mP2bVYqzMPbrrss8eUcX1XtgLxlVH8="
@iv = ENV["CASEBLOCKS_ENCRYPTION_IV"] || "vvSVfoWvZQ3T/DfjsjO/9w=="

end

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

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



41
42
43
44
45
46
47
# File 'lib/mail2cb/encryption.rb', line 41

def initialize(auth_token)
  # @current_user = current_user
  # config = YAML.load_file(File.join(Rails.root, "config", "secret_keys.yml"))[Rails.env.to_sym]
  # @key = config[:key]
  # @iv = config[:iv]
  @auth_token = auth_token
end

Instance Method Details

#decrypt(value) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/mail2cb/encryption.rb', line 49

def decrypt(value)
  unless value.nil?
    url = "#{ENV["CRYPT_API_ENDPOINT"]}/decrypt?auth_token=#{@auth_token}"
    result = ::RestClient.post url, {base64ciphertext: value}.to_json, :content_type => :json, :accept => :json
    JSON.parse(result)["plaintext"]
  else
    value
  end
end