Skip to content

Instantly share code, notes, and snippets.

@faoiseamh
Created May 12, 2014 13:37
Show Gist options
  • Save faoiseamh/9a478944befffb19760b to your computer and use it in GitHub Desktop.
Save faoiseamh/9a478944befffb19760b to your computer and use it in GitHub Desktop.
Crypt keeper decrypted fields in large queries
# Not a very elegant solution. I'd love to build something along these lines but more automated into crypt_keeper when I have more time.
module EncryptionHelper
def self.mysql_decrypted_field(field_name, options = {})
options[:as] = "#{field_name.gsub('.','_')}_decrypted" unless options[:as] == false
decrypt_query = "AES_DECRYPT(from_base64(#{field_name}), '#{MyApp::Application.config.crypt_passphrase}')"
decrypt_query << " AS #{options[:as]}" unless options[:as] == false
decrypt_query
end
end
# Then when building a query...
.select(... other fields ...,
EncryptionHelper.mysql_decrypted_field('users.first_name'),
EncryptionHelper.mysql_decrypted_field('users.last_name'),
EncryptionHelper.mysql_decrypted_field('users.email'),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment