Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Base | PSK | Auth | AuthPSK |
---|---|---|---|
✅ | ✅ | ✅ | ✅ |
KEMs | Browser | Node.js | Deno |
---|---|---|---|
DHKEM (P-256, HKDF-SHA256) | ✅ | ✅ | |
DHKEM (P-384, HKDF-SHA384) | ✅ | ✅ | |
DHKEM (P-521, HKDF-SHA512) | ✅ | ✅ | |
DHKEM (X25519, HKDF-SHA256) | |||
DHKEM (X448, HKDF-SHA512) |
KDFs | Browser | Node.js | Deno |
---|---|---|---|
HKDF-SHA256 | ✅ | ✅ | |
HKDF-SHA384 | ✅ | ✅ | |
HKDF-SHA512 | ✅ | ✅ |
AEADs | Browser | Node.js | Deno |
---|---|---|---|
AES-128-GCM | ✅ | ✅ | |
AES-256-GCM | ✅ | ✅ | |
ChaCha20Poly1305 | |||
Export Only | ✅ | ✅ |
Using npm:
npm install hpke-js
Using unpkg CDN:
<!-- use the latest stable version -->
<script src="https://unpkg.com/hpke-js/dist/hpke.min.js"></script>
<!-- use a specific version -->
<script src="https://unpkg.com/hpke-js@0.4.1/dist/hpke.min.js"></script>
Using jsDelivr CDN:
<!-- use the latest stable version -->
<script src="https://cdn.jsdelivr.net/npm/hpke-js/dist/hpke.min.js"></script>
<!-- use a specific version -->
<script src="https://cdn.jsdelivr.net/npm/hpke-js@0.4.1/dist/hpke.min.js"></script>
This section shows some typical usage examples.
Node.js environment:
const { Kem, Kdf, Aead, CipherSuite } = require("hpke-js");
async function doHpke() {
// setup
const suite = new CipherSuite({
kem: Kem.DhkemP256HkdfSha256,
kdf: Kdf.HkdfSha256,
aead: Aead.Aes128Gcm
});
const rkp = await suite.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
});
// encrypt
const ct = await sender.seal(new TextEncoder().encode("my-secret-message"));
// decrypt
const pt = await recipient.open(ct);
console.log("decrypted: ", new TextDecoder().decode(pt));
// decripted: my-secret-message
}
doHpke();
Browser environment:
<html>
<head></head>
<body>
<script type="text/javascript" src="https://unpkg.com/hpke-js/dist/browser/hpke.min.js"></script>
<script type="text/javascript">
async function doHpke() {
// the global name is 'hkpe'
const suite = new hpke.CipherSuite({
kem: hpke.Kem.DhkemP256HkdfSha256,
kdf: hpke.Kdf.HkdfSha256,
aead: hpke.Aead.Aes128Gcm
});
const rkp = await suite.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
});
// encrypt
const ct = await sender.seal(new TextEncoder().encode('hello world!'));
// decrypt
const pt = await recipient.open(ct);
// hello world!
alert(new TextDecoder().decode(pt));
}
</script>
<button type="button" onclick="doHpke()">do HPKE</button>
</body>
</html>
Node.js environment:
const { Kem, Kdf, Aead, CipherSuite } = require("hpke-js");
async function doHpke() {
// setup
const suite = new CipherSuite({
kem: Kem.DhkemP256HkdfSha256,
kdf: Kdf.HkdfSha256,
aead: Aead.Aes128Gcm
});
const rkp = await suite.generateKeyPair();
const pt = new TextEncoder().encode('my-secret-message'),
// encrypt
const { ct, enc } = await suite.seal({ recipientPublicKey: rkp.publicKey }, pt);
// decrypt
const pt = await suite.open({ recipientKey: rkp, enc: enc }, ct);
console.log("decrypted: ", new TextDecoder().decode(pt));
// decripted: my-secret-message
}
doHpke();
Node.js environment:
const te = new TextEncoder();
const td = new TextDecoder();
// setup
const suite = new CipherSuite({
kem: Kem.DhkemP256HkdfSha256,
kdf: Kdf.HkdfSha256,
aead: Aead.Aes128Gcm,
});
const rkp = await suite.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
});
// setup bidirectional encryption
await sender.setupBidirectional(te.encode('seed-for-key'), te.encode('seed-for-nonce'));
await recipient.setupBidirectional(te.encode('seed-for-key'), te.encode('seed-for-nonce'));
// encrypt
const ct = await sender.seal(te.encode('my-secret-message-s'));
// decrypt
const pt = await recipient.open(ct);
// assert
console.log("recipient decrypted: ", td.decode(pt));
// decripted: my-secret-message-s
// encrypt reversely
const rct = await recipient.seal(te.encode('my-secret-message-r'));
// decrypt reversely
const rpt = await sender.open(rct);
console.log("sender decrypted: ", td.decode(rpt));
// decripted: my-secret-message-r
}
doHpke();
Node.js environment:
const { Kem, Kdf, Aead, CipherSuite } = require("hpke-js");
async function doHpke() {
// setup
const suite = new CipherSuite({
kem: Kem.DhkemP256HkdfSha256,
kdf: Kdf.HkdfSha256,
aead: Aead.Aes128Gcm
});
const rkp = await suite.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
psk: {
id: new TextEncoder().encode("our-pre-shared-key-id"),
key: new TextEncoder().encode("our-pre-shared-key"),
}
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
psk: {
id: new TextEncoder().encode("our-pre-shared-key-id"),
key: new TextEncoder().encode("our-pre-shared-key"),
}
});
// encrypt
const ct = await sender.seal(new TextEncoder().encode("my-secret-message"));
// decrypt
const pt = await recipient.open(ct);
console.log("decrypted: ", new TextDecoder().decode(pt));
// decripted: my-secret-message
}
doHpke();
Node.js environment:
const { Kem, Kdf, Aead, CipherSuite } = require("hpke-js");
async function doHpke() {
// setup
const suite = new CipherSuite({
kem: Kem.DhkemP256HkdfSha256,
kdf: Kdf.HkdfSha256,
aead: Aead.Aes128Gcm
});
const rkp = await suite.generateKeyPair();
const skp = await suite.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
senderKey: skp
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
senderPublicKey: skp.publicKey
});
// encrypt
const ct = await sender.seal(new TextEncoder().encode("my-secret-message"));
// decrypt
const pt = await recipient.open(ct);
console.log("decrypted: ", new TextDecoder().decode(pt));
// decripted: my-secret-message
}
doHpke();
Node.js environment:
const { Kem, Kdf, Aead, CipherSuite } = require("hpke-js");
async function doHpke() {
// setup
const suite = new CipherSuite({
kem: Kem.DhkemP256HkdfSha256,
kdf: Kdf.HkdfSha256,
aead: Aead.Aes128Gcm
});
const rkp = await suite.generateKeyPair();
const skp = await suite.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
senderKey: skp,
psk: {
id: new TextEncoder().encode("our-pre-shared-key-id"),
key: new TextEncoder().encode("our-pre-shared-key"),
}
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
senderPublicKey: skp.publicKey,
psk: {
id: new TextEncoder().encode("our-pre-shared-key-id"),
key: new TextEncoder().encode("our-pre-shared-key"),
}
});
// encrypt
const ct = await sender.seal(new TextEncoder().encode("my-secret-message"));
// decrypt
const pt = await recipient.open(ct);
console.log("decrypted: ", new TextDecoder().decode(pt));
// decripted: my-secret-message
}
doHpke();
We welcome all kind of contributions, filing issues, suggesting new features or sending PRs.
FAQs
A Hybrid Public Key Encryption (HPKE) module for various JavaScript runtimes
The npm package hpke-js receives a total of 15,808 weekly downloads. As such, hpke-js popularity was classified as popular.
We found that hpke-js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.