From 12f75ff403db252befc8d1ad70836486c6aa527f Mon Sep 17 00:00:00 2001 From: Yan Date: Wed, 15 Nov 2017 12:01:49 +0630 Subject: [PATCH] licensing --- app/models/my_aes_crypt.rb | 38 ++++++++++++++++++++++++++++++++++ config/initializers/secrets.rb | 6 ++++++ 2 files changed, 44 insertions(+) create mode 100644 app/models/my_aes_crypt.rb create mode 100755 config/initializers/secrets.rb diff --git a/app/models/my_aes_crypt.rb b/app/models/my_aes_crypt.rb new file mode 100644 index 00000000..7427a000 --- /dev/null +++ b/app/models/my_aes_crypt.rb @@ -0,0 +1,38 @@ +class MyAesCrypt + @cipher = "" + + def initialize + @cipher = OpenSSL::Cipher::Cipher.new(ENV["CIPHER_TYPE"]) + end + + def export_key(passphrase) + # We want a 256 bit key symetric key based on passphrase + digest = Digest::SHA256.new + key = digest.update(passphrase) + key = digest.digest + ENV['AES_KEY'] = cipher_key = key # stores the key in key, and also sets the generated key on the @cipher + ENV['AES_IV'] = cipher_iv = @cipher.random_iv # stores the iv in iv, and also sets the generated iv on the @cipher + return cipher_key, cipher_iv + end + + private + def encrypt(data) + cipher.encrypt + cipher.key = ENV["aes_key"] + cipher.iv = ENV["aes_iv"] + encrypted = cipher.update(data) + cipher.final + encrypted = Base64.encode64(encrypted) + return encrypted + end + + def decrypt(data) + cipher.decrypt + cipher.key = ENV["aes_key"] + cipher.iv = ENV["aes_iv"] + + # Start the decryption + decoded = Base64.decode64(data) + decrypted = cipher.update(decoded) + cipher.final + return decrypted + end +end \ No newline at end of file diff --git a/config/initializers/secrets.rb b/config/initializers/secrets.rb new file mode 100755 index 00000000..a88ce110 --- /dev/null +++ b/config/initializers/secrets.rb @@ -0,0 +1,6 @@ +config = YAML.load_file("#{Rails.root}/config/secrets.yml") +config.fetch(Rails.env, {}).each do |key, value| + ENV[key.upcase] = value.to_s +end + +# SECRETS_CONFIG = YAML.load_file("#{Rails.root}/config/secrets.yml")[Rails.env]