Closed vsespb closed 6 years ago
It seems that decryption is faster, but I googled that encryption should be faster (i.e. https://crypto.stackexchange.com/questions/6378/why-is-rsa-encryption-significantly-faster-than-decryption and several other places) Maybe there is performance bug and something could be improved?
OS: linux
use strict; use warnings; use Benchmarks qw/all/; use Crypt::JWT qw(encode_jwt decode_jwt); use File::Temp qw/tempdir/; use File::Slurp qw/read_file/; my $tmp = tempdir(CLEANUP => 1); my $payload = { x => "x" x 1000 }; for my $keylen (1024, 2048) { my $private = "$tmp/private${keylen}.pem"; my $public = "$tmp/public${keylen}.pem"; system "openssl genpkey -algorithm RSA -out $private -pkeyopt rsa_keygen_bits:$keylen"; system "openssl rsa -pubout -in $private -out $public"; my $priv_key = read_file($private); my $pub_key = read_file($public); my $priv_o = Crypt::PK::RSA->new(\$priv_key); my $pub_o = Crypt::PK::RSA->new(\$pub_key); my $jwt_token = encode_jwt(payload => $payload, alg => 'RS256', key => \$priv_key); timethese(-1, { "encode ( $keylen )" => sub { encode_jwt(payload => $payload, alg=>'RS256', key=>$priv_o); }, "decode ( $keylen )" => sub { decode_jwt(token => $jwt_token, key => $pub_o); }, }); } __END__ Intel Core i7-2600, 16GB RAM, SSD. $perl jwt.pl ...................................++++++ ....++++++ writing RSA key Benchmark: running decode ( 1024 ), encode ( 1024 ) for at least 1 CPU seconds... decode ( 1024 ): 1.08815 wallclock secs ( 1.08 usr + 0.00 sys = 1.08 CPU) @ 6636.11/s (n=7167) encode ( 1024 ): 1.07056 wallclock secs ( 1.06 usr + 0.00 sys = 1.06 CPU) @ 1408.49/s (n=1493) .........+++ ...................................................................................................................................................................................................................................................+++ writing RSA key Benchmark: running decode ( 2048 ), encode ( 2048 ) for at least 1 CPU seconds... decode ( 2048 ): 1.04012 wallclock secs ( 1.04 usr + 0.00 sys = 1.04 CPU) @ 4306.73/s (n=4479) encode ( 2048 ): 1.05303 wallclock secs ( 1.05 usr + 0.00 sys = 1.05 CPU) @ 336.19/s (n=353)
oh, i was wrong. it's not about encryption vs decryption, but about signing vs verification (which are opposite too, probably). sorry for noise.
It seems that decryption is faster, but I googled that encryption should be faster (i.e. https://crypto.stackexchange.com/questions/6378/why-is-rsa-encryption-significantly-faster-than-decryption and several other places) Maybe there is performance bug and something could be improved?
OS: linux