simple-hpke 0.0.8 → 0.0.11
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 +29 -25
- package/dist/constants.cjs +1768 -0
- package/dist/constants.cjs.map +7 -0
- package/dist/constants.d.ts +14 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +1751 -0
- package/dist/constants.js.map +7 -0
- package/dist/constants.min.cjs +2 -0
- package/dist/constants.min.cjs.map +7 -0
- package/dist/constants.min.js +2 -0
- package/dist/constants.min.js.map +7 -0
- package/dist/index.cjs +212 -207
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +8 -81
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +212 -207
- package/dist/index.js.map +4 -4
- package/dist/index.min.cjs +1 -1
- package/dist/index.min.cjs.map +4 -4
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +4 -4
- package/dist/meta.json +298 -4
- package/dist/util.cjs +1767 -0
- package/dist/util.cjs.map +7 -0
- package/dist/util.d.ts +90 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +1750 -0
- package/dist/util.js.map +7 -0
- package/dist/util.min.cjs +2 -0
- package/dist/util.min.cjs.map +7 -0
- package/dist/util.min.js +2 -0
- package/dist/util.min.js.map +7 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
# Simple HPKE
|
|
2
2
|
[](https://github.com/nichoth/simple-hpke/actions/workflows/nodejs.yml)
|
|
3
|
-
[](README.md)
|
|
4
4
|
[](README.md)
|
|
5
5
|
[](https://semver.org/)
|
|
6
6
|
[](./CHANGELOG.md)
|
|
7
|
-
[](https://packagephobia.com/result?p=simple-hpke)
|
|
8
|
+
[](https://bundlephobia.com/package/simple-hpke)
|
|
9
9
|
[](LICENSE)
|
|
10
10
|
|
|
11
11
|
|
|
@@ -24,7 +24,7 @@ Hybrid Public Key Encryption
|
|
|
24
24
|
* [Hybrid Encryption](#hybrid-encryption)
|
|
25
25
|
+ [Encrypt / Decrypt](#encrypt--decrypt)
|
|
26
26
|
- [`encrypt`](#encrypt)
|
|
27
|
-
- [
|
|
27
|
+
- [`encrypt.asString`](#encryptasstring)
|
|
28
28
|
- [`decrypt`](#decrypt)
|
|
29
29
|
- [`decrypt.asString`](#decryptasstring)
|
|
30
30
|
- [`decrypt.fromString`](#decryptfromstring)
|
|
@@ -42,7 +42,7 @@ Hybrid Public Key Encryption
|
|
|
42
42
|
## Install
|
|
43
43
|
|
|
44
44
|
```sh
|
|
45
|
-
npm i -S
|
|
45
|
+
npm i -S simple-hpke
|
|
46
46
|
```
|
|
47
47
|
|
|
48
48
|
## Example
|
|
@@ -54,10 +54,11 @@ Wrap an AES key, or encrypt a message.
|
|
|
54
54
|
Encrypt an AES key to yourself, then recover it later.
|
|
55
55
|
|
|
56
56
|
```ts
|
|
57
|
-
import { seal, open } from '
|
|
57
|
+
import { seal, open } from 'simple-hpke'
|
|
58
58
|
|
|
59
|
-
// An X25519 keypair.
|
|
60
|
-
//
|
|
59
|
+
// An X25519 keypair. (asymmetric keypair).
|
|
60
|
+
// The private key can be non-extractable.
|
|
61
|
+
// HPKE only needs `deriveBits`.
|
|
61
62
|
const keypair = await crypto.subtle.generateKey(
|
|
62
63
|
{ name: 'X25519' },
|
|
63
64
|
false, // extractable
|
|
@@ -67,31 +68,37 @@ const keypair = await crypto.subtle.generateKey(
|
|
|
67
68
|
// create a new AES key, and encrypt it to your public key.
|
|
68
69
|
const { wrapped, key } = await seal(keypair)
|
|
69
70
|
|
|
70
|
-
// Or wrap an existing AES key. The supplied key must be extractable.
|
|
71
|
+
// Or wrap an existing AES key. The supplied AES key must be extractable.
|
|
71
72
|
const aesKey = await crypto.subtle.generateKey(
|
|
72
73
|
{ name: 'AES-GCM', length: 256 },
|
|
73
74
|
true, // extractable
|
|
74
75
|
['encrypt', 'decrypt']
|
|
75
76
|
)
|
|
76
77
|
|
|
77
|
-
// pass in
|
|
78
|
+
// pass in an existing key. The return value has the wrapped key.
|
|
78
79
|
const { wrapped } = await seal(keypair, aesKey)
|
|
79
80
|
|
|
80
81
|
// Later, recover the same key with your private key.
|
|
81
82
|
const recoveredKey = await open(keypair, wrapped)
|
|
83
|
+
|
|
84
|
+
// `recoveredKey` is equal to `aesKey`
|
|
82
85
|
```
|
|
83
86
|
|
|
84
87
|
See [docs/README.md](./docs/README.md) for the full API and rationale.
|
|
85
88
|
|
|
89
|
+
|
|
90
|
+
------------------------------------------------------
|
|
91
|
+
|
|
92
|
+
|
|
86
93
|
### Hybrid Encryption
|
|
87
94
|
|
|
88
|
-
|
|
89
|
-
The wrapped key is concattenated with the cipher text,
|
|
90
|
-
IV. The recipient uses their private key to open the AES key and
|
|
91
|
-
the message.
|
|
95
|
+
Encrypt a message with AES-GCM, then encrypt the AES key to a given
|
|
96
|
+
public key. The wrapped key is concattenated with the cipher text,
|
|
97
|
+
along with the IV. The recipient uses their private key to open the AES key and
|
|
98
|
+
decrypt the message.
|
|
92
99
|
|
|
93
100
|
```ts
|
|
94
|
-
import { seal, open } from '
|
|
101
|
+
import { seal, open } from 'simple-hpke'
|
|
95
102
|
|
|
96
103
|
const recipient = await crypto.subtle.generateKey(
|
|
97
104
|
{ name: 'X25519' },
|
|
@@ -155,10 +162,7 @@ cipher text). `decrypt` reverses it, returning the plaintext bytes.
|
|
|
155
162
|
|
|
156
163
|
|
|
157
164
|
```ts
|
|
158
|
-
import {
|
|
159
|
-
encrypt,
|
|
160
|
-
decrypt
|
|
161
|
-
} from '@substrate-system/simple-hpke'
|
|
165
|
+
import { encrypt, decrypt } from 'simple-hpke'
|
|
162
166
|
|
|
163
167
|
// need a public key for the recipient
|
|
164
168
|
const recipient = await crypto.subtle.generateKey(
|
|
@@ -214,14 +218,14 @@ async function encrypt (
|
|
|
214
218
|
):Promise<Uint8Array>
|
|
215
219
|
```
|
|
216
220
|
|
|
217
|
-
#####
|
|
221
|
+
##### `encrypt.asString`
|
|
218
222
|
|
|
219
|
-
`encrypt.asString` is `encrypt` with the envelope encoded to a string,
|
|
223
|
+
`encrypt.asString` is `encrypt` with the envelope encoded to a string, useful
|
|
220
224
|
for transports that carry text (JSON, URLs, headers). `opts.encoding` sets the
|
|
221
225
|
string encoding. Default encoding is `base64url`.
|
|
222
226
|
|
|
223
227
|
```ts
|
|
224
|
-
import { encrypt, decrypt } from '
|
|
228
|
+
import { encrypt, decrypt } from 'simple-hpke'
|
|
225
229
|
import { fromString } from 'uint8arrays'
|
|
226
230
|
|
|
227
231
|
// recipient is any RecipientKey; keypair holds the matching private key
|
|
@@ -289,12 +293,12 @@ This exposes ESM and common JS via
|
|
|
289
293
|
|
|
290
294
|
### ESM
|
|
291
295
|
```js
|
|
292
|
-
import { seal, open, encrypt, decrypt } from '
|
|
296
|
+
import { seal, open, encrypt, decrypt } from 'simple-hpke'
|
|
293
297
|
```
|
|
294
298
|
|
|
295
299
|
### Common JS
|
|
296
300
|
```js
|
|
297
|
-
require('
|
|
301
|
+
require('simple-hpke')
|
|
298
302
|
```
|
|
299
303
|
|
|
300
304
|
### pre-built JS
|
|
@@ -303,7 +307,7 @@ accessible to your web server, then link to them in HTML.
|
|
|
303
307
|
|
|
304
308
|
#### copy
|
|
305
309
|
```sh
|
|
306
|
-
cp ./node_modules
|
|
310
|
+
cp ./node_modules/simple-hpke/dist/index.min.js ./public/hpke.min.js
|
|
307
311
|
```
|
|
308
312
|
|
|
309
313
|
#### HTML
|