Build Status

ActiveRecordEncryption

Decorate encrypted binary column with attribute that transparently encrypt and decrypt sensitive data. It uses the ActiveRecord's Attribute API, and it is a simpler implementation than other gems.

Installation

Add this line to your application's Gemfile:

gem 'active_record_encryption'

Usage

Define serialized type in application

Here is an example of passing a type of object you want:

class Post < ActiveRecord::Base
  include ActiveRecordEncryption::EncryptedAttribute

  encrypted_attribute(:name, :string)
  encrypted_attribute(:published_on, :date)
end

---

$ post = Post.new(name: 'Author name', published_on: Date.current)
#=> #<Post:0x00007f92169ac158 id: nil, name: "Author name", published_on: Thu, 29 Mar 2018>
> post.save
DEBUG -- :    (0.1ms)  begin transaction
DEBUG -- :   Post Create (0.1ms)  INSERT INTO "posts" ("name", "published_on") VALUES (?, ?)  [["name", "N\xFA\xDD\xC2\xB0&\xAE\x9A..."], ["published_on", "N\xFA\xDD\xX2\xB0&\xAE\x9A..."]]
DEBUG -- :    (0.0ms)  commit transaction

#encrypted_attribute options

You can pass the same arguments as ActiveRecord::Attributes.attribute

class PointLog < ActiveRecord::Base
  encrypted_attribute(:date, :date)
  encrypted_attribute(:point, :integer, default: -> { Current.user.current_point })
  encrypted_attribute(:price, Money.new)
end

Migration

Create or modify the table that your model uses to add a column like the following:

NOTE: Default limit of binary is too long. Please set limit of encrypted columns

-ActiveRecord::Schema.define do
  create_table(:posts, force: true) do |t|
    t.binary :name, limit: 256
    t.binary :point, limit: 256
    t.binary :price, limit: 256
  end
end

Tips How to calculate limit of encrypted column? - It depends on algorithm of cipher.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Run spec

bundle exec appraisal install
bundle exec appraisal 5.0-stable rspec
bundle exec appraisal 5.1-stable rspec
bundle exec appraisal 5.2-stable rspec

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/alpaca-tc/active_record_encryption. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the ActiveRecord::Encryption project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.