Created
July 16, 2013 00:24
-
-
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.
This file contains 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 '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) |
This file contains 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 '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