Skip to content

Instantly share code, notes, and snippets.

@pulkit21
Created April 8, 2016 14:30
Show Gist options
  • Save pulkit21/fca91ab48ba7b2f3dfe3f593a1e620d0 to your computer and use it in GitHub Desktop.
Save pulkit21/fca91ab48ba7b2f3dfe3f593a1e620d0 to your computer and use it in GitHub Desktop.
Encrypt and decrypt data
require 'json/jwt'
require 'jwt'
module SignToken
extend self
def encrypt_data(payload)
rsa_private = OpenSSL::PKey::RSA.generate 2048
rsa_public = rsa_private.public_key
token = JWT.encode payload, rsa_private, 'RS256'
return {
public_key: rsa_public.to_s,
signed_token: token
}
end
def decrypt_data(token, rsa_public)
begin
public_key = OpenSSL::PKey::RSA.new(rsa_public)
JWT.decode token, public_key, true, { :algorithm => 'RS256' }
return true
rescue Exception => e
return e.message
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment