simple-hpke 0.1.5 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -8
- package/dist/constants.cjs +2 -22
- package/dist/constants.cjs.map +3 -3
- package/dist/constants.js +2 -22
- package/dist/constants.js.map +3 -3
- package/dist/constants.min.cjs +1 -1
- package/dist/constants.min.cjs.map +3 -3
- package/dist/constants.min.js +1 -1
- package/dist/constants.min.js.map +3 -3
- package/dist/index.cjs +6 -21
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +33 -22
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -21
- package/dist/index.js.map +3 -3
- package/dist/index.min.cjs +1 -1
- package/dist/index.min.cjs.map +3 -3
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +3 -3
- package/dist/meta.json +16 -16
- package/dist/util.cjs +2 -22
- package/dist/util.cjs.map +3 -3
- package/dist/util.d.ts +1 -1
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +2 -22
- package/dist/util.js.map +3 -3
- package/dist/util.min.cjs +1 -1
- package/dist/util.min.cjs.map +3 -3
- package/dist/util.min.js +1 -1
- package/dist/util.min.js.map +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ Hybrid Public Key Encryption
|
|
|
14
14
|
|
|
15
15
|
1 dependency -- `uint8arrays`.
|
|
16
16
|
|
|
17
|
-
On every `create`/`
|
|
17
|
+
On every `create`/`encrypt` call, we generate a fresh ephemeral X25519 keypair,
|
|
18
18
|
do Diffie-Hellman against the recipient public key, and the key schedule derives
|
|
19
19
|
an AES-256-GCM key from that shared secret.
|
|
20
20
|
|
|
@@ -64,7 +64,7 @@ Create an AES key, or encrypt a message.
|
|
|
64
64
|
Encrypt an AES key, then recover it later.
|
|
65
65
|
|
|
66
66
|
```ts
|
|
67
|
-
import { create,
|
|
67
|
+
import { create, encryptKey, open } from 'simple-hpke'
|
|
68
68
|
|
|
69
69
|
// An X25519 keypair. (asymmetric keypair).
|
|
70
70
|
// The private key can be non-extractable.
|
|
@@ -76,7 +76,9 @@ const keypair = await crypto.subtle.generateKey(
|
|
|
76
76
|
)
|
|
77
77
|
|
|
78
78
|
// Create a new AES key and encrypt it to your public key.
|
|
79
|
-
|
|
79
|
+
// `key` is the unencrypted new AES key
|
|
80
|
+
// `enc` is the AES key encrypted to the given public key
|
|
81
|
+
const { enc, key } = await create(keypair.publicKey) // or a buffer or string
|
|
80
82
|
|
|
81
83
|
//
|
|
82
84
|
// Or wrap an existing AES key. The supplied AES key must be extractable.
|
|
@@ -88,12 +90,12 @@ const aesKey = await crypto.subtle.generateKey(
|
|
|
88
90
|
)
|
|
89
91
|
|
|
90
92
|
// Wrap the existing key.
|
|
91
|
-
const { enc:
|
|
93
|
+
const { enc: wrappedKey } = await encryptKey(keypair, aesKey)
|
|
92
94
|
|
|
93
95
|
// or pass in just a public key
|
|
94
96
|
|
|
95
97
|
// Later, recover the same key with your private key.
|
|
96
|
-
const recoveredKey = await open(keypair,
|
|
98
|
+
const recoveredKey = await open(keypair, wrappedKey)
|
|
97
99
|
|
|
98
100
|
// `recoveredKey` is equal to `aesKey`
|
|
99
101
|
```
|
|
@@ -105,7 +107,7 @@ const recoveredKey = await open(keypair, sealedKey)
|
|
|
105
107
|
### Hybrid Encryption
|
|
106
108
|
|
|
107
109
|
Encrypt a message with AES-GCM, then encrypt the AES key to a given
|
|
108
|
-
public key. The
|
|
110
|
+
public key. The encrypted key is concattenated with the cipher text,
|
|
109
111
|
along with the IV. The recipient uses their private key to open the AES key and
|
|
110
112
|
decrypt the message.
|
|
111
113
|
|
|
@@ -160,7 +162,7 @@ new TextDecoder().decode(plaintext) // => 'attack at dawn'
|
|
|
160
162
|
So that was a lot of code to encrypt and decrypt a message...
|
|
161
163
|
This package exposes functions `encrypt` and `decrypt` that do the same thing.
|
|
162
164
|
|
|
163
|
-
`encrypt`
|
|
165
|
+
`encrypt` wraps an AES key to the recipient, encrypts the message under
|
|
164
166
|
that key, and returns a single envelope:
|
|
165
167
|
`wrappedLen + wrappedKey + iv + ciphertext`
|
|
166
168
|
(a 2-byte length prefix, the wrapped key, the 12-byte AES-GCM IV, and the
|
|
@@ -339,7 +341,7 @@ This exposes ESM and common JS via
|
|
|
339
341
|
|
|
340
342
|
### ESM
|
|
341
343
|
```js
|
|
342
|
-
import { create,
|
|
344
|
+
import { create, encryptKey, open, encrypt, decrypt } from 'simple-hpke'
|
|
343
345
|
```
|
|
344
346
|
|
|
345
347
|
### Common JS
|
package/dist/constants.cjs
CHANGED
|
@@ -1317,16 +1317,6 @@ __name(toString2, "toString");
|
|
|
1317
1317
|
|
|
1318
1318
|
// src/index.ts
|
|
1319
1319
|
var subtle = globalThis.crypto.subtle;
|
|
1320
|
-
async function create2(recipient, opts) {
|
|
1321
|
-
const { wrapped, key } = await encryptKey(recipient, null, opts);
|
|
1322
|
-
return { enc: wrapped, key };
|
|
1323
|
-
}
|
|
1324
|
-
__name(create2, "create");
|
|
1325
|
-
async function seal(recipient, aesKey, opts) {
|
|
1326
|
-
const { wrapped, key } = await encryptKey(recipient, aesKey, opts);
|
|
1327
|
-
return { enc: wrapped, key };
|
|
1328
|
-
}
|
|
1329
|
-
__name(seal, "seal");
|
|
1330
1320
|
var open = Object.assign(openBytes, {
|
|
1331
1321
|
raw: openRawBytes
|
|
1332
1322
|
});
|
|
@@ -1507,7 +1497,7 @@ async function encryptKey(recipient, aesKey, opts) {
|
|
|
1507
1497
|
const ciphertext = await aeadSeal(key, baseNonce, keyBytes);
|
|
1508
1498
|
const wrapped = concat2(enc, ciphertext);
|
|
1509
1499
|
const aesGcmKey = await importAesKey(keyBytes);
|
|
1510
|
-
return { wrapped, key: aesGcmKey };
|
|
1500
|
+
return { enc: wrapped, key: aesGcmKey };
|
|
1511
1501
|
}
|
|
1512
1502
|
__name(encryptKey, "encryptKey");
|
|
1513
1503
|
|
|
@@ -1547,17 +1537,7 @@ async function aeadOpen(key, nonce, ciphertext) {
|
|
|
1547
1537
|
__name(aeadOpen, "aeadOpen");
|
|
1548
1538
|
async function encryptBytes(recipient, message, aesKey, opts) {
|
|
1549
1539
|
const plaintext = typeof message === "string" ? new TextEncoder().encode(message) : message;
|
|
1550
|
-
|
|
1551
|
-
let key;
|
|
1552
|
-
if (!aesKey) {
|
|
1553
|
-
const keys = await create2(recipient, opts);
|
|
1554
|
-
enc = keys.enc;
|
|
1555
|
-
key = keys.key;
|
|
1556
|
-
} else {
|
|
1557
|
-
const keys = await seal(recipient, aesKey, opts);
|
|
1558
|
-
enc = keys.enc;
|
|
1559
|
-
key = keys.key;
|
|
1560
|
-
}
|
|
1540
|
+
const { enc, key } = await encryptKey(recipient, aesKey, opts);
|
|
1561
1541
|
const iv = globalThis.crypto.getRandomValues(new Uint8Array(NN));
|
|
1562
1542
|
const ct = new Uint8Array(await subtle2.encrypt(
|
|
1563
1543
|
{ name: "AES-GCM", iv },
|