Created
July 28, 2015 01:35
-
-
Save oestrich/71429f60d671488a3966 to your computer and use it in GitHub Desktop.
encryption benchmark
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 'openssl' | |
require 'benchmark' | |
data = "Hello, world!" | |
Benchmark.bm do |x| | |
x.report do | |
private_key = OpenSSL::PKey::RSA.new 4096 | |
public_key = private_key.public_key | |
cipher = OpenSSL::Cipher.new('AES-128-CBC') | |
cipher.encrypt | |
aes_key = cipher.random_key | |
aes_iv = cipher.random_iv | |
encrypted_aes_key = public_key.public_encrypt(aes_key) | |
encrypted_aes_iv = public_key.public_encrypt(aes_iv) | |
encrypted_data = cipher.update(data) + cipher.final | |
decipher = OpenSSL::Cipher.new('AES-128-CBC') | |
decipher.decrypt | |
decipher.key = private_key.private_decrypt(encrypted_aes_key) | |
decipher.iv = private_key.private_decrypt(encrypted_aes_iv) | |
puts decipher.update(encrypted_data) + decipher.final | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment