Using attr_encrypted with Rails 5.2 credentials master key

A short tutorial on how to use attr_encrypted with the same key as Rails credentials

Warning

This post was written more than 5 years ago, and its contents may be out of date

Today’s another short tutorial is about using encryption key from Rails 5.2 credentials to encrypt/decrypt fields with attr_encrypted.

This way you can encrypt your database fields in Rails 5.2 without using separate keys for the fields and for the credentials:

attr_encrypted :super_secret_token, key: Rails.application.credentials.key

Aaaaand… that’s it ¯\_(ツ)_/¯

Licensed under CC BY-NC 4.0

Comments