-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.rb
185 lines (158 loc) · 4.41 KB
/
benchmark.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
require "base64"
require "benchmark/ips"
require "ffi/hydrogen"
require "rbnacl"
require "urlcrypt"
TEST_STRINGS = [
"0123456789",
"<abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv>",
"<abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#^&*()_+>",
]
TEST_STRINGS.each do |str|
puts "============================ Test string length: #{str.size} ============================"
label = "#{str.size} characters"
encoded = ::FFI::Hydrogen.modp_b64_encode(str)
hydrogen_key = ::FFI::Hydrogen.hydro_secretbox_keygen
hydrogen_context = "benched1"
hydrogen_box = ::FFI::Hydrogen::Secretbox.new(hydrogen_context, hydrogen_key)
hydrogen_encrypted = hydrogen_box.encrypt(str)
hydrogen_encrypted_encoded = hydrogen_box.encrypt_encode(str)
rbnacl_key = ::RbNaCl::Random.random_bytes(::RbNaCl::SecretBox.key_bytes)
rbnacl_box = ::RbNaCl::SimpleBox.from_secret_key(rbnacl_key)
rbnacl_encrypted = rbnacl_box.encrypt(str)
rbnacl_encrypted_encoded = ::Base64.encode64(rbnacl_box.encrypt(str))
urlcrypt_key = "h34j4k3l25gh342o5jk46ghjk5nbjkl7nhljk34532j4k5b43hjk5g432jk6v4gjhk6f5j6gv5hj2k3g54hjk321g4hjk25g4hjk35gf4yu6f5jg5b43jk25h4jk325d"
::URLcrypt.key = [urlcrypt_key].pack("H*")
urlcrypt_encrypted = ::URLcrypt.encrypt(str)
Benchmark.ips do |b|
b.time = 2
b.warmup = 1
b.report("Boxed Hydrogen encrypt_encode (#{label})") do |n|
i = 0
while i < n
hydrogen_box.encrypt_encode(str)
i += 1
end
end
b.report("Boxed Hydrogen decode_decrypt (#{label})") do |n|
i = 0
while i < n
hydrogen_box.decode_decrypt(hydrogen_encrypted_encoded)
i += 1
end
end
b.report("Boxed Hydrogen encrypt + Base64 encode (#{label})") do |n|
i = 0
while i < n
hydrogen_box.encrypt(::Base64.encode64(str))
i += 1
end
end
b.report("Boxed Hydrogen decrypt + Base64 decode (#{label})") do |n|
i = 0
while i < n
::Base64.decode64(hydrogen_box.decrypt(hydrogen_encrypted_encoded))
i += 1
end
end
b.report("URLcrypt decrypt (#{label})") do |n|
i = 0
while i < n
::URLcrypt.encrypt(str)
i += 1
end
end
b.report("URLcrypt decrypt (#{label})") do |n|
i = 0
while i < n
::URLcrypt.decrypt(urlcrypt_encrypted)
i += 1
end
end
b.report("Boxed RbNaCl encrypt + Base64 encode (#{label})") do |n|
i = 0
while i < n
::Base64.encode64(rbnacl_box.encrypt(str))
i += 1
end
end
b.report("Boxed RbNaCl decrypt + Base64 decode (#{label})") do |n|
i = 0
while i < n
rbnacl_box.decrypt(::Base64.decode64(rbnacl_encrypted_encoded))
i += 1
end
end
b.report("modp_b64_encode (#{label})") do |n|
i = 0
while i < n
::FFI::Hydrogen.modp_b64_encode(str)
i += 1
end
end
b.report("modp_b64_decode (#{label})") do |n|
i = 0
while i < n
::FFI::Hydrogen.modp_b64_decode(encoded)
i += 1
end
end
b.report("Base64 encode (#{label})") do |n|
i = 0
while i < n
::Base64.encode64(str)
i += 1
end
end
b.report("Base64 decode (#{label})") do |n|
i = 0
while i < n
::Base64.decode64(str)
i += 1
end
end
b.report("hydro_secretbox_encrypt (#{label})") do |n|
i = 0
while i < n
::FFI::Hydrogen.hydro_secretbox_encrypt(str, hydrogen_context, hydrogen_key)
i += 1
end
end
b.report("hydro_secretbox_decrypt (#{label})") do |n|
i = 0
while i < n
::FFI::Hydrogen.hydro_secretbox_decrypt(hydrogen_encrypted, hydrogen_context, hydrogen_key)
i += 1
end
end
b.report("Boxed Hydrogen encrypt (#{label})") do |n|
i = 0
while i < n
hydrogen_box.encrypt(str)
i += 1
end
end
b.report("Boxed Hydrogen decrypt (#{label})") do |n|
i = 0
while i < n
hydrogen_box.decrypt(hydrogen_encrypted)
i += 1
end
end
b.report("Boxed RbNaCl encrypt (#{label})") do |n|
i = 0
while i < n
rbnacl_box.encrypt(str)
i += 1
end
end
b.report("Boxed RbNaCl decrypt (#{label})") do |n|
i = 0
while i < n
rbnacl_box.decrypt(rbnacl_encrypted)
i += 1
end
end
b.compare!
end
end