Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add webpack sample #301

Merged
merged 2 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions samples/ts-webpack/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
app.js
36 changes: 36 additions & 0 deletions samples/ts-webpack/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Aes128Gcm, CipherSuite, DhkemP256HkdfSha256, HkdfSha256 } from "@hpke/core";

export const test = async () => {
const suite = new CipherSuite({
kem: new DhkemP256HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Aes128Gcm(),
});

// I expected to be able to do:
// const rkp = await suite.kem.generateKeyPair();
// instead I needed to do:
const extractable = true
const rkp = await window.crypto.subtle.generateKey({
name: "ECDH",
namedCurve: "P-256",
}, extractable, ['deriveBits'])

// A sender encrypts a message.
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey
});
const ct = await sender.seal(new TextEncoder().encode("✨ hello world! ✨"));
// A recipient decripts it.
const recipient = await suite.createRecipientContext({
recipientKey: rkp.privateKey,
enc: sender.enc,
});
try {
const pt = await recipient.open(ct);
// hello world!
alert(new TextDecoder().decode(pt));
} catch (err) {
alert("failed to decrypt.");
}
}
14 changes: 14 additions & 0 deletions samples/ts-webpack/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>

<head>
<title>Test</title>
<script src="./dist/main.js" class="remove"></script>
<script>
window.test.test();
</script>
</head>
<body>
<pre>See developer console.</pre>
</body>
</html>
11 changes: 11 additions & 0 deletions samples/ts-webpack/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


import * as app from './app.js'

const test = async () => {
app.test()
}
// setup exports on window
window.test = {
test
}
Loading
Loading