Skip to content

Instantly share code, notes, and snippets.

@yosiat
Created April 9, 2023 11:14
Show Gist options
  • Save yosiat/372bc468d23c568cd8722af7f56a2b03 to your computer and use it in GitHub Desktop.
Save yosiat/372bc468d23c568cd8722af7f56a2b03 to your computer and use it in GitHub Desktop.
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