Created
April 8, 2016 14:30
-
-
Save pulkit21/fca91ab48ba7b2f3dfe3f593a1e620d0 to your computer and use it in GitHub Desktop.
Encrypt and decrypt data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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