Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use native hkdf #3

Merged
merged 1 commit into from
Feb 2, 2023
Merged

use native hkdf #3

merged 1 commit into from
Feb 2, 2023

Conversation

firien
Copy link
Contributor

@firien firien commented Feb 1, 2023

OpenSSL::KDF.hkdf has been around since ruby 2.5

require 'benchmark'
require 'openssl'
require 'hkdf'

salt = Random.new.bytes(16)
shared_secret = Random.new.bytes(32)
info = 'helloworld'

Benchmark.bm do |benchmark|
  n = 50_000
  benchmark.report('gem') do
    n.times do
      HKDF.new(shared_secret, salt: salt, algorithm: 'SHA256', info: info).next_bytes(32)
    end
  end
  benchmark.report('native') do
    n.times do
      OpenSSL::KDF.hkdf(shared_secret, salt: salt, info: info, hash: 'SHA256', length: 32)
    end
  end
end

@collimarco
Copy link
Contributor

Thanks!

Here's my results:

       user     system      total        real
gem  2.643673   0.015704   2.659377 (  2.660243)
native  1.091457   0.001120   1.092577 (  1.092618)

Native is faster and we can also drop an external dependency.

All tests are passing.

@collimarco collimarco merged commit 5326a5f into pushpad:master Feb 2, 2023
@firien firien deleted the native-hkdf branch February 2, 2023 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants