Skip to content

Instantly share code, notes, and snippets.

@peiyush13
Created September 5, 2019 19:36
Show Gist options
  • Save peiyush13/5275533c3ce2701096a13038043c9bef to your computer and use it in GitHub Desktop.
Save peiyush13/5275533c3ce2701096a13038043c9bef to your computer and use it in GitHub Desktop.
Helper for encryption/decryption API cycle (Sessions for this case)
require 'rsa_encryptor'
# Module SessionsHelper provides public key encryption/decryption for password
#
# @author Peiyush13 <piyushwww13@gmail.com>
#
module SessionsHelper
# initializes public key used for password encryption
#
def generate_rsa_key
rsa_key = RsaEncryptor.new
session[:rsa_key] = rsa_key.serialized_data
@public_key = rsa_key.public_key.to_s
end
# decrypts the password using private key
#
# @param [Array<string>] user_params_keys An array specifying user parameters to be decrypted
#
def authenticate_encryptor(user_params_keys)
rsa_key = RsaEncryptor.build_key(session[:rsa_key])
user_params_keys.each do |key|
request.params[:user][key] = rsa_key.private_decrypt(Base64.decode64(request.params[:user][key])) rescue ''
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment