Class: AliquotPay::Util
- Inherits:
-
Object
- Object
- AliquotPay::Util
- Defined in:
- lib/aliquot-pay/util.rb
Class Method Summary collapse
- .calculate_tag(mac_key, encrypted_message) ⇒ Object
- .derive_keys(ephemeral_public_key, shared_secret, info, protocol_version = :ECv2) ⇒ Object
- .generate_ephemeral_key ⇒ Object
- .generate_shared_secret(private_key, public_key) ⇒ Object
Class Method Details
.calculate_tag(mac_key, encrypted_message) ⇒ Object
38 39 40 41 |
# File 'lib/aliquot-pay/util.rb', line 38 def self.calculate_tag(mac_key, ) digest = OpenSSL::Digest::SHA256.new OpenSSL::HMAC.digest(digest, mac_key, ) end |
.derive_keys(ephemeral_public_key, shared_secret, info, protocol_version = :ECv2) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/aliquot-pay/util.rb', line 14 def self.derive_keys(ephemeral_public_key, shared_secret, info, protocol_version = :ECv2) case protocol_version when :ECv1 key_length = 16 when :ECv2 key_length = 32 else raise StandardError, "invalid protocol_version #{protocol_version}" end = ephemeral_public_key + shared_secret if OpenSSL.const_defined?(:KDF) && OpenSSL::KDF.respond_to?(:hkdf) h = OpenSSL::Digest::SHA256.new hbytes = OpenSSL::KDF.hkdf(, hash: h, salt: '', length: key_length * 2, info: info) else hbytes = HKDF.new(, algorithm: 'SHA256', info: info).next_bytes(key_length * 2) end { aes_key: hbytes[0, key_length], mac_key: hbytes[key_length, key_length], } end |
.generate_ephemeral_key ⇒ Object
6 7 8 |
# File 'lib/aliquot-pay/util.rb', line 6 def self.generate_ephemeral_key OpenSSL::PKey::EC.generate(AliquotPay::EC_CURVE) end |
.generate_shared_secret(private_key, public_key) ⇒ Object
10 11 12 |
# File 'lib/aliquot-pay/util.rb', line 10 def self.generate_shared_secret(private_key, public_key) private_key.dh_compute_key(public_key) end |