In my one project I need to store crypted data to the column in database.
I found a few projects on Ruby Toolbox for this.
But main issues of this projects is:
- They are too big
- They are not maintained
- They are not customizable
So I found better solution for me.
As you know ActiveRecord can serialize any column. ActiveRecord uses Marshal to serialize column by default. But we can change it to using any custom serialization class. Main requirements of this custom class is to have
I think my example will helps you to decrease time of you development.
# Example of custom serialization class
class Example < ActiveRecord::Base
def initialize(default = '')
@default = default
# You can use any two ways encryption ruby gem for this
# I'm used gibberish bacause it very usable for me
@cipher ||= ::Gibberish::AES.new('p@ssw0rd')
s.present? ? cipher.dec(s) : @default.clone
cipher.enc(s || @default)
serialize :key, Encryptor.new('')