Skip to content

Commit 43e6e71

Browse files
authored
Merge pull request #329 from dajiaji/add-samples-deno-for-chacha20poly1305
Add samples/deno to x/chacha20poly1305.
2 parents 20abb89 + 1f967fe commit 43e6e71

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"tasks": {
3+
"dev": "deno run --watch main.ts"
4+
},
5+
"imports": {
6+
"@hpke/chacha20poly1305": "jsr:@hpke/chacha20poly1305@^1.2.9",
7+
"@hpke/core": "jsr:@hpke/core@^1.2.9",
8+
"@std/assert": "jsr:@std/assert@1"
9+
}
10+
}

x/chacha20poly1305/samples/deno/deno.lock

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { CipherSuite, DhkemP256HkdfSha256, HkdfSha256 } from "@hpke/core";
2+
import { Chacha20Poly1305 } from "@hpke/chacha20poly1305";
3+
4+
async function doHpke() {
5+
// setup
6+
const suite = new CipherSuite({
7+
kem: new DhkemP256HkdfSha256(),
8+
kdf: new HkdfSha256(),
9+
aead: new Chacha20Poly1305(),
10+
});
11+
12+
const rkp = await suite.kem.generateKeyPair();
13+
14+
const sender = await suite.createSenderContext({
15+
recipientPublicKey: rkp.publicKey,
16+
});
17+
18+
const recipient = await suite.createRecipientContext({
19+
recipientKey: rkp.privateKey,
20+
enc: sender.enc,
21+
});
22+
23+
// encrypt
24+
const ct = await sender.seal(new TextEncoder().encode("Hello world!"));
25+
26+
// decrypt
27+
const pt = await recipient.open(ct);
28+
29+
// Hello world!
30+
console.log(new TextDecoder().decode(pt));
31+
}
32+
33+
try {
34+
doHpke();
35+
} catch (_err: unknown) {
36+
console.log("doHPKE() failed.");
37+
}

0 commit comments

Comments
 (0)