Skip to content

Commit

Permalink
Merge pull request #366 from dajiaji/add-node-samples
Browse files Browse the repository at this point in the history
Add samples for node.
  • Loading branch information
dajiaji authored Sep 1, 2024
2 parents 7637723 + 9fb87fd commit a8d93a0
Show file tree
Hide file tree
Showing 19 changed files with 607 additions and 1 deletion.
34 changes: 34 additions & 0 deletions x/chacha20poly1305/samples/node/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { CipherSuite, DhkemP256HkdfSha256, HkdfSha256 } from "@hpke/core";
import { Chacha20Poly1305 } from "@hpke/chacha20poly1305";

async function doHpke() {
const suite = new CipherSuite({
kem: new DhkemP256HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Chacha20Poly1305(),
});

const rkp = await suite.kem.generateKeyPair();

// 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 decrypts it.
const recipient = await suite.createRecipientContext({
recipientKey: rkp.privateKey,
enc: sender.enc,
});
const pt = await recipient.open(ct);

// Hello world!
console.log(new TextDecoder().decode(pt));
}

try {
doHpke();
} catch (e) {
console.log("Error:", e.message);
}
48 changes: 48 additions & 0 deletions x/chacha20poly1305/samples/node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions x/chacha20poly1305/samples/node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "node-hpke-core",
"version": "1.0.0",
"description": "A sample code of @hpke/core.",
"type": "module",
"main": "app.js",
"author": "Ajitomi Daisuke <ajitomi@gmail.com> (https://github.com/dajiaji)",
"license": "MIT",
"dependencies": {
"@hpke/chacha20poly1305": "^1.3.0",
"@hpke/core": "^1.3.0"
}
}
33 changes: 33 additions & 0 deletions x/core/samples/node/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Aes128Gcm, CipherSuite, DhkemP256HkdfSha256, HkdfSha256 } from "@hpke/core";

async function doHpke() {
const suite = new CipherSuite({
kem: new DhkemP256HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Aes128Gcm(),
});

const rkp = await suite.kem.generateKeyPair();

// 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 decrypts it.
const recipient = await suite.createRecipientContext({
recipientKey: rkp.privateKey,
enc: sender.enc,
});
const pt = await recipient.open(ct);

// Hello world!
console.log(new TextDecoder().decode(pt));
}

try {
doHpke();
} catch (e) {
console.log("Error:", e.message);
}
25 changes: 25 additions & 0 deletions x/core/samples/node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions x/core/samples/node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "node-hpke-core",
"version": "1.0.0",
"description": "A sample code of @hpke/core.",
"type": "module",
"main": "app.js",
"author": "Ajitomi Daisuke <ajitomi@gmail.com> (https://github.com/dajiaji)",
"license": "MIT",
"dependencies": {
"@hpke/core": "^1.3.0"
}
}
34 changes: 34 additions & 0 deletions x/dhkem-secp256k1/samples/node/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Aes128Gcm, CipherSuite, HkdfSha256 } from "@hpke/core";
import { DhkemSecp256k1HkdfSha256 } from "@hpke/dhkem-secp256k1";

async function doHpke() {
const suite = new CipherSuite({
kem: new DhkemSecp256k1HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Aes128Gcm(),
});

const rkp = await suite.kem.generateKeyPair();

// 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 decrypts it.
const recipient = await suite.createRecipientContext({
recipientKey: rkp.privateKey,
enc: sender.enc,
});
const pt = await recipient.open(ct);

// Hello world!
console.log(new TextDecoder().decode(pt));
}

try {
doHpke();
} catch (e) {
console.log("Error:", e.message);
}
63 changes: 63 additions & 0 deletions x/dhkem-secp256k1/samples/node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions x/dhkem-secp256k1/samples/node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "node-hpke-core",
"version": "1.0.0",
"description": "A sample code of @hpke/core.",
"type": "module",
"main": "app.js",
"author": "Ajitomi Daisuke <ajitomi@gmail.com> (https://github.com/dajiaji)",
"license": "MIT",
"dependencies": {
"@hpke/core": "^1.3.0",
"@hpke/dhkem-secp256k1": "^1.3.0"
}
}
34 changes: 34 additions & 0 deletions x/dhkem-x25519/samples/node/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Aes128Gcm, CipherSuite, HkdfSha256 } from "@hpke/core";
import { DhkemX25519HkdfSha256 } from "@hpke/dhkem-x25519";

async function doHpke() {
const suite = new CipherSuite({
kem: new DhkemX25519HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Aes128Gcm(),
});

const rkp = await suite.kem.generateKeyPair();

// 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 decrypts it.
const recipient = await suite.createRecipientContext({
recipientKey: rkp.privateKey,
enc: sender.enc,
});
const pt = await recipient.open(ct);

// Hello world!
console.log(new TextDecoder().decode(pt));
}

try {
doHpke();
} catch (e) {
console.log("Error:", e.message);
}
64 changes: 64 additions & 0 deletions x/dhkem-x25519/samples/node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions x/dhkem-x25519/samples/node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "node-hpke-core",
"version": "1.0.0",
"description": "A sample code of @hpke/core.",
"type": "module",
"main": "app.js",
"author": "Ajitomi Daisuke <ajitomi@gmail.com> (https://github.com/dajiaji)",
"license": "MIT",
"dependencies": {
"@hpke/core": "^1.3.0",
"@hpke/dhkem-x25519": "^1.3.0"
}
}
Loading

0 comments on commit a8d93a0

Please sign in to comment.