Skip to content

Commit 5784ab0

Browse files
committed
use native hkdf
1 parent 7e8c631 commit 5784ab0

File tree

4 files changed

+7
-8
lines changed

4 files changed

+7
-8
lines changed

lib/web_push.rb

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
require 'openssl'
44
require 'base64'
5-
require 'hkdf'
65
require 'jwt'
76
require 'uri'
87
require 'net/http'

lib/web_push/encryption.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def encrypt(message, p256dh, auth)
88
assert_arguments(message, p256dh, auth)
99

1010
group_name = 'prime256v1'
11+
hash = 'SHA256'
1112
salt = Random.new.bytes(16)
1213

1314
server = OpenSSL::PKey::EC.generate(group_name)
@@ -25,11 +26,11 @@ def encrypt(message, p256dh, auth)
2526
content_encryption_key_info = "Content-Encoding: aes128gcm\0"
2627
nonce_info = "Content-Encoding: nonce\0"
2728

28-
prk = HKDF.new(shared_secret, salt: client_auth_token, algorithm: 'SHA256', info: info).read(32)
29+
prk = OpenSSL::KDF.hkdf(shared_secret, salt: client_auth_token, info: info, hash: hash, length: 32)
2930

30-
content_encryption_key = HKDF.new(prk, salt: salt, info: content_encryption_key_info).read(16)
31+
content_encryption_key = OpenSSL::KDF.hkdf(prk, salt: salt, info: content_encryption_key_info, hash: hash, length: 16)
3132

32-
nonce = HKDF.new(prk, salt: salt, info: nonce_info).read(12)
33+
nonce = OpenSSL::KDF.hkdf(prk, salt: salt, info: nonce_info, hash: hash, length: 12)
3334

3435
ciphertext = encrypt_payload(message, content_encryption_key, nonce)
3536

spec/web_push/encryption_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ def decrypt payload
6565
content_encryption_key_info = "Content-Encoding: aes128gcm\0"
6666
nonce_info = "Content-Encoding: nonce\0"
6767

68-
prk = HKDF.new(shared_secret, salt: client_auth_token, algorithm: 'SHA256', info: info).read(32)
68+
prk = OpenSSL::KDF.hkdf(shared_secret, salt: client_auth_token, info: info, hash: 'SHA256', length: 32)
6969

70-
content_encryption_key = HKDF.new(prk, salt: salt, info: content_encryption_key_info).read(16)
71-
nonce = HKDF.new(prk, salt: salt, info: nonce_info).read(12)
70+
content_encryption_key = OpenSSL::KDF.hkdf(prk, salt: salt, info: content_encryption_key_info, hash: 'SHA256', length: 16)
71+
nonce = OpenSSL::KDF.hkdf(prk, salt: salt, info: nonce_info, hash: 'SHA256', length: 12)
7272

7373
decrypt_ciphertext(ciphertext, content_encryption_key, nonce)
7474
end

web-push.gemspec

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ Gem::Specification.new do |spec|
1414

1515
spec.required_ruby_version = '>= 3.0'
1616

17-
spec.add_dependency 'hkdf', '~> 1.0'
1817
spec.add_dependency 'jwt', '~> 2.0'
1918
spec.add_dependency 'openssl', '~> 3.0'
2019

0 commit comments

Comments
 (0)