simple-hpke 0.1.2 → 0.1.3

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.
Files changed (2) hide show
  1. package/README.md +44 -13
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -68,14 +68,17 @@ const keypair = await crypto.subtle.generateKey(
68
68
  // Create a new AES key and encrypt it to your public key.
69
69
  const { enc, key } = await create(keypair)
70
70
 
71
+ //
71
72
  // Or wrap an existing AES key. The supplied AES key must be extractable.
73
+ //
74
+
72
75
  const aesKey = await crypto.subtle.generateKey(
73
76
  { name: 'AES-GCM', length: 256 },
74
77
  true, // extractable
75
78
  ['encrypt', 'decrypt']
76
79
  )
77
80
 
78
- // Wrap an existing key.
81
+ // Wrap the existing key.
79
82
  const { enc: sealedKey } = await seal(keypair, aesKey)
80
83
 
81
84
  // or pass in just a public key
@@ -86,8 +89,6 @@ const recoveredKey = await open(keypair, sealedKey)
86
89
  // `recoveredKey` is equal to `aesKey`
87
90
  ```
88
91
 
89
- See [docs/README.md](./docs/README.md) for the full API and rationale.
90
-
91
92
 
92
93
  ------------------------------------------------------
93
94
 
@@ -148,11 +149,11 @@ new TextDecoder().decode(plaintext) // => 'attack at dawn'
148
149
  #### Encrypt / Decrypt
149
150
 
150
151
  So that was a lot of code to encrypt and decrypt a message...
151
- This package exposes functions `encrypt` and `decrypt` which do the same thing.
152
+ This package exposes functions `encrypt` and `decrypt` that do the same thing.
152
153
 
153
154
  `encrypt` seals an AES key to the recipient, encrypts the message under
154
155
  that key, and returns a single envelope:
155
- `wrappedLen + wrapped + iv + ciphertext`
156
+ `wrappedLen + wrappedKey + iv + ciphertext`
156
157
  (a 2-byte length prefix, the wrapped key, the 12-byte AES-GCM IV, and the
157
158
  cipher text). `decrypt` reverses it, returning the plaintext bytes.
158
159
 
@@ -168,27 +169,42 @@ cipher text). `decrypt` reverses it, returning the plaintext bytes.
168
169
 
169
170
 
170
171
  ```ts
172
+ import { toString } from 'uint8arrays'
171
173
  import { encrypt, decrypt } from 'simple-hpke'
172
174
 
175
+ // ----------------------
176
+ // Encrypt
177
+ // ----------------------
178
+
173
179
  // need a public key for the recipient
174
180
  const recipient = await crypto.subtle.generateKey(
175
181
  { name: 'X25519' },
176
- false,
182
+ false, // extractable
177
183
  ['deriveBits']
178
184
  )
179
185
 
180
- // create a new AES key, encrypt a message, and get back one envelope
181
- const encryptedMessage = await encrypt(recipient, 'hello encryption')
186
+ // create a new AES key, encrypt a message, and get back an "envelope"
187
+ const encryptedMessage = await encrypt(recipient.publicKey, 'hello encryption')
182
188
 
183
- // the recipient recovers the message with their private key
184
- const text = await decrypt.asString(recipient, encryptedMessage)
189
+ // encrypt to a public key as Uint8Array
190
+ const publicBytes = new Uint8Array(
191
+ await subtle.exportKey('raw', recipient.publicKey)
192
+ )
193
+ const envelope = await encrypt(publicBytes, 'hello again')
185
194
 
186
- // use `decrypt` to get a Uint8Array
187
- const bytes = await decrypt(recipient, encryptedMessage)
195
+ // encrypt to a stringified public key
196
+ // create a string with `uint8arrays.toString`
197
+ const publicString = toString(publicBytes, 'base64url')
198
+ const envelopeAgain = await encrypt({
199
+ publicKey: publicString,
200
+ encoding: 'base64url' // <-- this is the default
201
+ }, 'hello again, string version')
188
202
 
189
203
  //
190
- // use an existing AES key
204
+ // encrypt with an existing AES key
205
+ // (a key not generated by this module)
191
206
  //
207
+
192
208
  const existingKey = await crypto.subtle.generateKey(
193
209
  { name: 'AES-GCM', length: 256 },
194
210
  true, // extractable
@@ -200,8 +216,23 @@ const anotherEncryptedMsg = await encrypt(
200
216
  'hello again',
201
217
  existingKey
202
218
  )
219
+
220
+ // ----------------------
221
+ // Decrypt
222
+ // ----------------------
223
+
224
+ // the recipient recovers the message with their private key
225
+ const text = await decrypt.asString(recipient, encryptedMessage)
226
+
227
+ // use `decrypt` to get a Uint8Array
228
+ const bytes = await decrypt(recipient, encryptedMessage)
229
+
230
+ // get a string
231
+ const plaintext = await decrypt.asString(recipient, envelope)
203
232
  ```
204
233
 
234
+ ---
235
+
205
236
  ##### `encrypt`
206
237
 
207
238
  The recipient can be a crypto key, a Uint8Array, or a string public key.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "simple-hpke",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Hybrid Public Key Encryption",
5
5
  "type": "module",
6
6
  "files": [