Skip to content

Commit

Permalink
Merge pull request #301 from OR13/debug-webpack-of-ts-of-hpke
Browse files Browse the repository at this point in the history
Add webpack sample
  • Loading branch information
dajiaji authored Feb 25, 2024
2 parents f4bf056 + 16aa4ed commit c62fb99
Show file tree
Hide file tree
Showing 8 changed files with 3,312 additions and 0 deletions.
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

0 comments on commit c62fb99

Please sign in to comment.