Class: Privy::Cryptography::HpkeRecipient

Inherits:
Object
  • Object
show all
Defined in:
lib/privy/cryptography.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHpkeRecipient

Returns a new instance of HpkeRecipient.



29
30
31
32
33
# File 'lib/privy/cryptography.rb', line 29

def initialize
  @ec_key = OpenSSL::PKey::EC.generate("prime256v1")
  @public_key_spki = @ec_key.public_to_der
  @hpke = HPKE.new(HPKE::DHKEM_P256_HKDF_SHA256, HPKE::HKDF_SHA256, HPKE::CHACHA20_POLY1305)
end

Instance Attribute Details

#public_key_spkiString (readonly)

Returns Raw DER bytes of the SPKI-encoded public key.

Returns:

  • (String)

    Raw DER bytes of the SPKI-encoded public key



27
28
29
# File 'lib/privy/cryptography.rb', line 27

def public_key_spki
  @public_key_spki
end

Instance Method Details

#decrypt(encapsulated_key, ciphertext) ⇒ String

Decrypts an HPKE-encrypted payload.

Parameters:

  • encapsulated_key (String)

    Raw bytes of the encapsulated key from the sender

  • ciphertext (String)

    Raw bytes of the ciphertext to decrypt

Returns:

  • (String)

    Decrypted plaintext bytes



40
41
42
43
# File 'lib/privy/cryptography.rb', line 40

def decrypt(encapsulated_key, ciphertext)
  ctx = @hpke.setup_base_r(encapsulated_key, @ec_key, "")
  ctx.open("", ciphertext)
end

#public_key_pkeyOpenSSL::PKey::PKey

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (OpenSSL::PKey::PKey)


47
48
49
# File 'lib/privy/cryptography.rb', line 47

def public_key_pkey
  OpenSSL::PKey.read(@public_key_spki)
end