diff --git a/app/models/user.rb b/app/models/user.rb index b2ccf52..05eb101 100755 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,7 @@ +require 'encryption' + class User < ActiveRecord::Base + attr_accessible :email, :admin, :first_name, :last_name, :user_id, :password, :password_confirmation validates :password, :presence => true, :confirmation => true, @@ -87,42 +90,9 @@ private end end - # Added a re-usable encryption routine, shouldn't be an issue! - def encrypt_sensitive_value(val="") - aes = OpenSSL::Cipher::Cipher.new(cipher_type) - aes.encrypt - aes.key = key - aes.iv = iv if iv != nil - #self.encrypted_ssn = aes.update(self.SSN) + aes.final - #self.SSN = nil - end - - def decrypt_ssn - aes = OpenSSL::Cipher::Cipher.new(cipher_type) - aes.decrypt - aes.key = key - aes.iv = iv if iv != nil - #aes.update(self.encrypted_ssn) + aes.final - end - - # Should be able to just re-use the same key we already have! - def key - raise "Key Missing" if !(KEY) - KEY - end - - def iv - raise "No IV for this User" if !(self.key_management.iv) - #self.key_management.iv - end - - def cipher_type - 'aes-256-cbc' - end - def generate_token(column) begin - #self[column] = + self[column] = Encryption.encrypt_sensitive_value(self.user_id) end while User.exists?(column => self[column]) end diff --git a/config/initializers/constants.rb b/config/initializers/constants.rb index 7fdcd8f..086522f 100644 --- a/config/initializers/constants.rb +++ b/config/initializers/constants.rb @@ -1 +1,3 @@ -ACCESS_TOKEN_SALT = "S4828341189aefiasd#ASDF" \ No newline at end of file +ACCESS_TOKEN_SALT = "S4828341189aefiasd#ASDF" + +RG_IV = "PPKLKAJDKGHALDJL482823458028" \ No newline at end of file diff --git a/lib/encryption.rb b/lib/encryption.rb new file mode 100644 index 0000000..defa525 --- /dev/null +++ b/lib/encryption.rb @@ -0,0 +1,36 @@ +module Encryption + + # Added a re-usable encryption routine, shouldn't be an issue! + def self.encrypt_sensitive_value(val="") + aes = OpenSSL::Cipher::Cipher.new(cipher_type) + aes.encrypt + aes.key = key + aes.iv = iv if iv != nil + new_val = aes.update("#{val}") + aes.final + Base64.strict_encode64(new_val).encode('utf-8') + end + + def self.decrypt_sensitive_value(val="") + aes = OpenSSL::Cipher::Cipher.new(cipher_type) + aes.decrypt + aes.key = key + aes.iv = iv if iv != nil + decoded = Base64.strict_decode64("#{val}") + aes.update("#{decoded}") + aes.final + end + + # Should be able to just re-use the same key we already have! + def self.key + raise "Key Missing" if !(KEY) + KEY + end + + def self.iv + RG_IV + end + + def self.cipher_type + 'aes-256-cbc' + end + +end \ No newline at end of file