Skip to content

Instantly share code, notes, and snippets.

@oestrich
Created July 28, 2015 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oestrich/71429f60d671488a3966 to your computer and use it in GitHub Desktop.
Save oestrich/71429f60d671488a3966 to your computer and use it in GitHub Desktop.
encryption benchmark
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