Skip to content

Instantly share code, notes, and snippets.

@andersonbosa
Forked from ChunAllen/crypt.rb
Created September 20, 2022 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersonbosa/32c10c2b865bfef386d1018461cfe204 to your computer and use it in GitHub Desktop.
Save andersonbosa/32c10c2b865bfef386d1018461cfe204 to your computer and use it in GitHub Desktop.
Encryption using AES-256-CBC with Rails
module Crypt
class << self
ENCRYPTION_KEY = Rails.application.secrets[:encryption_key]
ALGO = 'aes-256-cbc'.freeze
def encrypt(value)
crypt(:encrypt, value)
end
def decrypt(value)
crypt(:decrypt, value)
end
def crypt(cipher_method, value)
cipher = OpenSSL::Cipher.new(ALGO)
cipher.send(cipher_method)
cipher.pkcs5_keyivgen(ENCRYPTION_KEY)
result = cipher.update(value)
result << cipher.final
end
end
end
@jackromo888
Copy link

Thanjs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment