Devise Encryptable AES
Add AES encryption support for Devise
Table of Contents
- Getting started
- Configuration
- Usage
Getting started
Add the following line to your Gemfile:
gem 'devise', '~> 4.9'
gem 'devise-encryptable', '~> 0.2.0'
gem 'devise_encryptable_aes', '~> 0.0.6'
Then run bundle install
Configuration
Add the encryptable
module to your model:
class User < ActiveRecord::Base
devise :database_authenticatable, :encryptable
end
And add the password_salt
field to the database through a migration:
class DeviseCreateUsers < ActiveRecord::Migration
def change
add_column :users, :password_salt, :string
end
end
Enable the AES encryptor in config/initializers/devise.rb
# Uncomment the generated pepper
config.pepper = "long random string"
# Enable the AES encryptor
config.encryptor = :aes256
Usage
Compare password
::Devise::Encryptable::Encryptors::Aes256.compare(encrypted_password, password, Devise.pepper)
Decrypt password
::Devise::Encryptable::Encryptors::Aes256.decrypt(encrypted_password, Devise.pepper)
If you get an error when using valid_password?, you can customize the valid_password? function to
def valid_password?(password)
::Devise::Encryptable::Encryptors::Aes256.compare(encrypted_password, password, Devise.pepper)
end