Hi All,
I just created an encrypter model to automatically encrypt all of the
content_columns in any model that uses it (I used the Encrypter example
from “Pragmatic Programming with Ruby on Rails” as a guide, and Sentry
for actual encryption).
The problem I’m facing now is actually searching the encrypted data.
Overriding the default “find” method is fairly straightforward, but
things get messy when trying to use dynamic attribute-based finders.
Has anyone else run across a similar problem and/or found an elegant
solution?
Thanks much,
-Mike.
P.S. for reference, here is the code for encrypter.rb. Encrypter.key is
set in application.rb from user input.
class Encrypter
require ‘sentry’
@@s=Sentry::SymmetricSentry
def self.key=(k)
@@key=k
end
def self.key
@@key
end
def before_save(model)
model.class.content_columns.each do |field|
model[field.name]=@@s.encrypt(model[field.name].to_s,
self.class.key) if model[field.name].class==String
end
end
def after_save(model)
model.class.content_columns.each do |field|
model[field.name]=@@s.decrypt(model[field.name].to_s,
self.class.key) if model[field.name].class==String
end
end
alias_method :after_find, :after_save
end
class ActiveRecord::Base
def self.encrypt()
encrypter = Encrypter.new()
before_save encrypter
after_save encrypter
after_find encrypter
define_method(:after_find) { }
end
end