Created
June 16, 2015 09:12
-
-
Save jurriaan/8a62636a8dedb5a38a5a to your computer and use it in GitHub Desktop.
benchmark_jwt.rb
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
$LOAD_PATH.unshift('./lib/') | |
require 'jwt' | |
require 'benchmark/ips' | |
@private_key = {} | |
@public_key = {} | |
@jwt = {} | |
@payload = { :hello => 'world' } | |
def init_jwt(type, key_type) | |
klass = type.start_with?('ES') ? OpenSSL::PKey::EC : OpenSSL::PKey::RSA | |
if type.start_with?('ES') | |
@private_key[key_type] ||= klass.new(key_type).tap(&:generate_key) | |
else | |
@private_key[key_type] ||= klass.generate(key_type) | |
end | |
@public_key[key_type] ||= klass.new(@private_key[key_type]).tap { |key| key.private_key = nil if key.respond_to? :private_key= } | |
@jwt[[type, key_type]] = JWT.encode(@payload, @private_key[key_type], type) | |
end | |
# ECDSA JWT | |
puts 'Generating ECDSA keys..' | |
init_jwt 'ES256', 'prime256v1' | |
init_jwt 'ES384', 'secp384r1' | |
init_jwt 'ES512', 'secp521r1' | |
# RSA JWT for various key lengths | |
[1024, 2048, 4096, 8192].each do |key_bits| | |
puts "Generating #{key_bits} RSA keys.." | |
init_jwt 'RS256', key_bits | |
init_jwt 'RS384', key_bits | |
init_jwt 'RS512', key_bits | |
end | |
Benchmark.ips do |x| | |
@jwt.each do |key, jwt| | |
type, key_type = key | |
x.report("verify using #{key}") { JWT.decode(jwt, @public_key[key_type]) } | |
x.report("sign using #{key}") { JWT.encode(@payload, @private_key[key_type], type) } | |
end | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Full output: