A Hybrid Public Key Encryption (HPKE) module extension for ChaCha20/Poly1305
npm install @hpke/chacha20poly1305Documentation: jsr.io |
pages (only for the latest ver.)
- Installation
- Node.js
- Deno
- Web Browsers
- Usage
- Contributing
@hpke/chacha20poly1305 need to be used with
@hpke/core,
which can be installed in the same manner as desribed below.
You can install the package with npm, yarn or pnpm.
``sh`Using npm:
npm install @hpke/chacha20poly1305
yarn add @hpke/chacha20poly1305
pnpm install @hpke/chacha20poly1305Using jsr:
npx jsr add @hpke/chacha20poly1305
yarn dlx jsr add @hpke/chacha20poly1305
pnpm dlx jsr add @hpke/chacha20poly1305
The above manner can be used with other JavaScript runtimes that support npm,
such as Cloudflare Workers and Bun.
Then, you can use the module from code like this:
`ts`
import { CipherSuite, DhkemP256HkdfSha256, HkdfSha256 } from "@hpke/core";
import { Chacha20Poly1305 } from "@hpke/chacha20poly1305";
For Deno, it is recommended to use the jsr.io registry.
`sh`
deno add jsr:@hpke/chacha20poly1305
Followings are how to use this module with typical CDNs. Other CDNs can be used
as well.
Using esm.sh:
`html
`
Using unpkg:
`html`
This section shows some typical usage examples.
`js
import { CipherSuite, DhkemP256HkdfSha256, HkdfSha256 } from "@hpke/core";
import { Chacha20Poly1305 } from "@hpke/chacha20poly1305";
// const { Chacha20Poly1305 } = require("@hpke/chacha20poly1305");
async function doHpke() {
// setup
const suite = new CipherSuite({
kem: new DhkemP256HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Chacha20Poly1305(),
});
const rkp = await suite.kem.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
});
// encrypt
const ct = await sender.seal(new TextEncoder().encode("Hello world!"));
const recipient = await suite.createRecipientContext({
recipientKey: rkp.privateKey,
enc: sender.enc,
});
// decrypt
const pt = await recipient.open(ct);
// Hello world!
console.log(new TextDecoder().decode(pt));
}
try {
doHpke();
} catch (err) {
console.log("failed:", err.message);
}
`
`ts
import { CipherSuite, DhkemP256HkdfSha256, HkdfSha256 } from "@hpke/core";
import { Chacha20Poly1305 } from "@hpke/chacha20poly1305";
async function doHpke() {
// setup
const suite = new CipherSuite({
kem: new DhkemP256HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Chacha20Poly1305(),
});
const rkp = await suite.kem.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
});
// encrypt
const ct = await sender.seal(new TextEncoder().encode("Hello world!"));
const recipient = await suite.createRecipientContext({
recipientKey: rkp.privateKey,
enc: sender.enc,
});
// decrypt
const pt = await recipient.open(ct);
// Hello world!
console.log(new TextDecoder().decode(pt));
}
try {
doHpke();
} catch (_err: unknown) {
console.log("failed.");
}
`
`html``
We welcome all kind of contributions, filing issues, suggesting new features or
sending PRs.