diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2017-03-02 19:12:13 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-02 19:12:13 +0100 |
commit | 0203c376704bec36d07923d8c2924f9dded2317c (patch) | |
tree | 6aa662215f7b14989ca747640e1239fad41aa4f5 /railties/lib | |
parent | f294e649b7e659d36744e3af0c462d80728e5f41 (diff) | |
parent | 6aa6f9ae44ed999e972a58f729bdc5b2fcdc127b (diff) | |
download | rails-0203c376704bec36d07923d8c2924f9dded2317c.tar.gz rails-0203c376704bec36d07923d8c2924f9dded2317c.tar.bz2 rails-0203c376704bec36d07923d8c2924f9dded2317c.zip |
Merge pull request #28139 from stouset/update-secrets-to-use-modern-crypto
Update secrets to use modern crypto
Diffstat (limited to 'railties/lib')
-rw-r--r-- | railties/lib/rails/secrets.rb | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/railties/lib/rails/secrets.rb b/railties/lib/rails/secrets.rb index a083914109..3d68e30d1d 100644 --- a/railties/lib/rails/secrets.rb +++ b/railties/lib/rails/secrets.rb @@ -1,4 +1,4 @@ -require "yaml" +require "active_support/message_encryptor" module Rails # Greatly inspired by Ara T. Howard's magnificent sekrets gem. 😘 @@ -12,6 +12,8 @@ module Rails end end + CIPHER = "aes-128-gcm" + @read_encrypted_secrets = false @root = File # Wonky, but ensures `join` uses the current directory. @@ -30,20 +32,22 @@ module Rails end def generate_key - cipher = new_cipher - SecureRandom.hex(cipher.key_len)[0, cipher.key_len] + SecureRandom.hex( + OpenSSL::Cipher.new(CIPHER).key_len + ) end def key - ENV["RAILS_MASTER_KEY"] || read_key_file || handle_missing_key + [(ENV["RAILS_MASTER_KEY"] || read_key_file || handle_missing_key)] + .pack("H*") end - def encrypt(text) - cipher(:encrypt, text) + def encrypt(data) + encryptor.encrypt_and_sign(data) end def decrypt(data) - cipher(:decrypt, data) + encryptor.decrypt_and_verify(data) end def read @@ -97,14 +101,8 @@ module Rails end end - def new_cipher - OpenSSL::Cipher.new("aes-256-cbc") - end - - def cipher(mode, data) - cipher = new_cipher.public_send(mode) - cipher.key = key - cipher.update(data) << cipher.final + def encryptor + @encryptor ||= ActiveSupport::MessageEncryptor.new(key, cipher: CIPHER) end end end |