dajiaji / hpke-js

A Hybrid Public Key Encryption (HPKE) module built on top of Web Cryptography API.
https://dajiaji.github.io/hpke-js/docs
MIT License
58 stars 9 forks source link

suite.kemContext is not a function #278

Closed cherimo closed 9 months ago

cherimo commented 10 months ago

I am trying to export/serialize the public key with the following code:

import { Aes256Gcm, CipherSuite, HkdfSha512 } from "@hpke/core";
import { DhkemX448HkdfSha512 } from "@hpke/dhkem-x448";

async function doHpke() {
  const suite = new CipherSuite({
    kem: new DhkemX448HkdfSha512(),
    kdf: new HkdfSha512(),
    aead: new Aes256Gcm(),
  });
  const kemContext = await suite.kemContext();

  // A recipient generates a key pair.
  const rkp = await suite.kem.generateKeyPair();

  const rawPubKey = await kemContext.serializePublicKey(rkp.publicKey);

  console.log(rawPubKey)

  // A sender encrypts a message with the recipient public key.
  const sender = await suite.createSenderContext({
    recipientPublicKey: rkp.publicKey,
  });

  const ct = await sender.seal(new TextEncoder().encode("Hello world!"));

I get the following error: TypeError: suite.kemContext is not a function

Is there something I've overlooked?

Thank you

dajiaji commented 10 months ago

Hi @cherimo,

suite.kemContext() has already been removed after v1.0.0 release, but you can use a getter function kem instead of it. As you already used in your sample code above, could you try the following code?

// before
const kemContext = await suite.kemContext();
const rawPubKey = await kemContext.serializePublicKey(rkp.publicKey);
// after (try this)
const rawPubKey = await suite.kem.serializePublicKey(rkp.publicKey);
cherimo commented 9 months ago

@dajiaji many thanks, that worked perfectly!!