Using the attr_encrypted gem

class User
  attr_accessor :name
  attr_encrypted :ssn, :key => 'a secret key'

  def load
    # loads the stored data
  end

  def save
    # saves the :name and :encrypted_ssn attributes somewhere (e.g. filesystem, database, etc)
  end
end

@user = User.new
@user.ssn = '123-45-6789'
@user.encrypted_ssn # returns the encrypted version of :ssn
@user.save

@user = User.load
@user.ssn # decrypts :encrypted_ssn and returns '123-45-6789'

Generates attr_accessors that encrypt and decrypt attributes transparently

It works with ANY class, however, you get a few extra features when you're using it with ActiveRecord.

Resources

Tags:
Source:
1627hrs.txt
Published:
08-06-2014 16:27