Skip to content

Instantly share code, notes, and snippets.

@gevans
Created July 16, 2013 00:24
Show Gist options
  • Save gevans/6004752 to your computer and use it in GitHub Desktop.
Save gevans/6004752 to your computer and use it in GitHub Desktop.
A couple examples of using asymmetric RSA signing and encryption using Ruby's OpenSSL libraries.
require 'openssl'
key = OpenSSL::PKey::RSA.new(2048)
p encrypted_string = key.public_encrypt('my plaintext string', OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
p decrypted_string = key.private_decrypt(encrypted_string, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
require 'openssl'
key = OpenSSL::PKey::RSA.new(2048)
data = 'my string to sign'
p signature = key.sign(OpenSSL::Digest::SHA256.new, data)
pubkey = key.public_key
if pubkey.verify(OpenSSL::Digest::SHA256.new, signature, data)
puts 'the signature is valid'
else
puts 'the signature is invalid'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment