Created
May 12, 2014 13:37
-
-
Save faoiseamh/9a478944befffb19760b to your computer and use it in GitHub Desktop.
Crypt keeper decrypted fields in large queries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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