Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kymmt90/active_record-type-encrypted_string
Provides encrypted string attributes to Active Record models
https://github.com/kymmt90/active_record-type-encrypted_string
activerecord encryption rails ruby
Last synced: about 1 month ago
JSON representation
Provides encrypted string attributes to Active Record models
- Host: GitHub
- URL: https://github.com/kymmt90/active_record-type-encrypted_string
- Owner: kymmt90
- License: mit
- Created: 2019-05-16T14:23:05.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-02-02T06:39:35.000Z (almost 5 years ago)
- Last Synced: 2024-04-24T12:26:47.074Z (8 months ago)
- Topics: activerecord, encryption, rails, ruby
- Language: Ruby
- Homepage:
- Size: 43 KB
- Stars: 3
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# ActiveRecord::Type::EncryptedString
[![Gem Version](https://badge.fury.io/rb/active_record-type-encrypted_string.svg)](https://badge.fury.io/rb/active_record-type-encrypted_string) [![Build Status](https://github.com/kymmt90/active_record-type-encrypted_string/workflows/build/badge.svg)](https://github.com/kymmt90/active_record-type-encrypted_string/actions?workflow=build)
> Provides encrypted string attributes to Active Record models
ActiveRecord::Type::EncryptedString is a subtype of ActiveRecord::Type::String. This gem enables Active Record to encrypt/decrypt string attributes seamlessly.
## Usage
You can try this gem as follows:
```ruby
require 'bundler/inline'gemfile do
source 'https://rubygems.org'
gem 'rails'
gem 'active_record-type-encrypted_string'
gem 'sqlite3'
endrequire 'active_record'
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: ':memory:',
)ActiveRecord::Schema.define do
create_table :users do |t|
t.string :token
end
end# Define an string attribute
class User < ActiveRecord::Base
attribute :token, :encrypted_string
end# Settings for encryption
ENV['ENCRYPTED_STRING_PASSWORD'] = 'password'
ENV['ENCRYPTED_STRING_SALT'] = SecureRandom.random_bytes# "token" is saved as encrypted string value
token = 'token'
user = User.create(token: token)
ActiveRecord::Base.connection.select_value('SELECT token FROM users') #=> "eVZzbUlXME1xSlZ5ZWZPQnIvY..."# Get the encrypted value as a decrypted value transparently
user.token #=> "token"
```## Installation
Add this line to your application's Gemfile:
```ruby
gem 'active_record-type-encrypted_string'
```And then execute:
```bash
$ bundle
```Or install it yourself as:
```bash
$ gem install active_record-type-encrypted_string
```## Configuration
The password and the salt for encryption can be configured through environment variables or class attributes. Environment variables take precedence over class attributes.
```ruby
ENV['ENCRYPTED_STRING_PASSWORD'] = 'password'
ENV['ENCRYPTED_STRING_SALT'] = 'salt'ActiveRecord::Type::EncryptedString.encryption_password = 'password'
ActiveRecord::Type::EncryptedString.encryption_salt = 'salt'
```## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/kymmt90/active_record-type-encrypted_string.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).