-
Notifications
You must be signed in to change notification settings - Fork 0
/
sk_vs_pk_bench.rb
47 lines (36 loc) · 1.26 KB
/
sk_vs_pk_bench.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true
require 'openssl'
require 'benchmark'
# Setup basic parameters
plaintext = 'Hello darkness my old friend'
n = 100_000
# Setup AES ciphers
aes_cipher = OpenSSL::Cipher.new('aes-128-cbc').encrypt
key = aes_cipher.random_key
iv = aes_cipher.random_iv
aes_ciphertext = aes_cipher.update(plaintext) + aes_cipher.final
aes_decipher = OpenSSL::Cipher.new('aes-128-cbc').decrypt
aes_decipher.key = key
aes_decipher.iv = iv
# Setup RSA ciphers
rsa_private = OpenSSL::PKey::RSA.generate 2048
rsa_ciphertext = rsa_private.public_encrypt plaintext
rsa_public = rsa_private.public_key
# Benchmark AES vs. RSA, encrypt vs. decrypt
results = Benchmark.bm(15) do |bench|
bench.report('AES encrypt') do
n.times { aes_cipher.update(plaintext) + aes_cipher.final }
end
bench.report('AES decrypt') do
n.times { aes_decipher.update(aes_ciphertext) + aes_decipher.final }
end
puts '-' * 61
bench.report('RSA encrypt') do
n.times { rsa_public.public_encrypt plaintext }
end
bench.report('RSA decrypt') do
n.times { rsa_private.private_decrypt rsa_ciphertext }
end
end;
puts "SK encryption faster by: #{(results[2].utime / results[0].utime).round} times"
puts "SK decryption faster by: #{(results[3].utime / results[1].utime).round} times"