Skip to content

Instantly share code, notes, and snippets.

@adityagodbole
Last active December 19, 2015 23:49
Show Gist options
  • Save adityagodbole/6036935 to your computer and use it in GitHub Desktop.
Save adityagodbole/6036935 to your computer and use it in GitHub Desktop.

This is a note on storing encrypted data in Rails

DB neutral encryption

The gen attr_encrypted is a very popular option to get DB independent and framework independent encryption. It is known to work with -

It offers the following features:

  • Specify attributes to be encrypted - attr_encrypted :ssn, :key => 'a secret key'
  • Specify alternate attribute name - attr_encrypted :email, :key => 'a secret key', :attribute => 'email_encrypted'
  • Calculate encryption keys dynamically
    • by specifying a method - attr_encrypted :email, :key => :get_key
    • by specifying a proc - attr_encrypted :email, :key => proc { |user| user.key }
  • Specify custom encryptors
  • All encryption algorithms suported by the Encryptor gem
  • Store any serialisable data in the encrypted field - attr_encrypted :credentials, :key => 'some secret key', :marshal => true

Encryption at the DB level

Different databases provide transparent, client independent database encryption mechanisms. This makes the encryption transparent to the client programs and is relatively faster. This comes at a cost to the ease of implementation and supported encryption algorithms.

  • Postgres - Postgres provides the pgcrypto module to handle encryption
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment