class Mail2cb::Encryption

Public Class Methods

new(auth_token) click to toggle source

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

# 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

Public Instance Methods

decrypt(value) click to toggle source
# 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