Created
April 9, 2023 11:14
-
-
Save yosiat/372bc468d23c568cd8722af7f56a2b03 to your computer and use it in GitHub Desktop.
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
require "openssl" | |
require "securerandom" | |
require "benchmark/ips" | |
class Signer | |
def initialize | |
@rsa_key = OpenSSL::PKey::RSA.new(2048) | |
end | |
def sign(string_to_sign) | |
key = OpenSSL::PKey::RSA.new(@rsa_key) | |
digest = OpenSSL::Digest::SHA256.new | |
return key.sign(digest, string_to_sign) | |
end | |
def new_sign(string_to_sign) | |
key = @rsa_key | |
key = OpenSSL::PKey::RSA.new(@rsa_key) unless key.respond_to?(:sign) | |
digest = OpenSSL::Digest::SHA256.new | |
return key.sign(digest, string_to_sign) | |
end | |
end | |
signer = Signer.new | |
string_to_sign = SecureRandom.hex(50) | |
Benchmark.ips do |x| | |
# Typical mode, runs the block as many times as it can | |
x.report("sign") { signer.sign(string_to_sign) } | |
x.report("new") { signer.new_sign(string_to_sign) } | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment