@substrate-system/keys 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/LICENSE +99 -0
- package/README.md +688 -0
- package/dist/constants.cjs +64 -0
- package/dist/constants.cjs.map +7 -0
- package/dist/constants.d.ts +24 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +44 -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 +480 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +191 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +493 -0
- package/dist/index.js.map +7 -0
- package/dist/index.min.cjs +2 -0
- package/dist/index.min.cjs.map +7 -0
- package/dist/index.min.js +4 -0
- package/dist/index.min.js.map +7 -0
- package/dist/meta.json +225 -0
- package/dist/types.cjs +57 -0
- package/dist/types.cjs.map +7 -0
- package/dist/types.d.ts +38 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +37 -0
- package/dist/types.js.map +7 -0
- package/dist/types.min.cjs +2 -0
- package/dist/types.min.cjs.map +7 -0
- package/dist/types.min.js +2 -0
- package/dist/types.min.js.map +7 -0
- package/dist/util.cjs +375 -0
- package/dist/util.cjs.map +7 -0
- package/dist/util.d.ts +89 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +373 -0
- package/dist/util.js.map +7 -0
- package/dist/util.min.cjs +4 -0
- package/dist/util.min.cjs.map +7 -0
- package/dist/util.min.js +4 -0
- package/dist/util.min.js.map +7 -0
- package/package.json +86 -0
package/README.md
ADDED
|
@@ -0,0 +1,688 @@
|
|
|
1
|
+
# keys
|
|
2
|
+
[](https://github.com/substrate-system/keys/actions/workflows/nodejs.yml)
|
|
3
|
+
[](README.md)
|
|
4
|
+
[](README.md)
|
|
5
|
+
[](https://semver.org/)
|
|
6
|
+
[](./CHANGELOG.md)
|
|
7
|
+
[](https://packagephobia.com/result?p=@substrate-system/keys)
|
|
8
|
+
[](https://bundlephobia.com/package/@substrate-system/keys)
|
|
9
|
+
[](LICENSE)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
Create and store keypairs in the browser with the [web crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API).
|
|
13
|
+
|
|
14
|
+
Use [indexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API)
|
|
15
|
+
to store [non-extractable keypairs](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey#extractable)
|
|
16
|
+
in the browser. "Non-extractable" means that the browser prevents you from ever
|
|
17
|
+
reading the private key, but the keys can be persisted and re-used indefinitely.
|
|
18
|
+
|
|
19
|
+
>
|
|
20
|
+
> [!TIP]
|
|
21
|
+
> Use the [persist method](https://developer.mozilla.org/en-US/docs/Web/API/StorageManager/persist)
|
|
22
|
+
> to tell the browser not to delete from `indexedDB`.
|
|
23
|
+
>
|
|
24
|
+
|
|
25
|
+
Each instance of `Keys` has two keypairs -- one for signing, and another for
|
|
26
|
+
encrypting. We are **using RSA keys only** right now, because we are
|
|
27
|
+
[waiting for all browsers to support ECC](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey#browser_compatibility).
|
|
28
|
+
|
|
29
|
+
See also, [the API docs generated from typescript](https://bicycle-codes.github.io/keys/).
|
|
30
|
+
|
|
31
|
+
<details><summary><h2>Contents</h2></summary>
|
|
32
|
+
|
|
33
|
+
<!-- toc -->
|
|
34
|
+
|
|
35
|
+
- [install](#install)
|
|
36
|
+
- [Asymmetric Encryption](#asymmetric-encryption)
|
|
37
|
+
- [Modules](#modules)
|
|
38
|
+
* [`exports`](#exports)
|
|
39
|
+
* [ESM](#esm)
|
|
40
|
+
* [Common JS](#common-js)
|
|
41
|
+
* [pre-built JS](#pre-built-js)
|
|
42
|
+
- [get started](#get-started)
|
|
43
|
+
* [Create a keypair](#create-a-keypair)
|
|
44
|
+
* [some notes about the `keys` instance](#some-notes-about-the-keys-instance)
|
|
45
|
+
* [Delete a keypair](#delete-a-keypair)
|
|
46
|
+
* [sign and verify something](#sign-and-verify-something)
|
|
47
|
+
* [encrypt something](#encrypt-something)
|
|
48
|
+
* [decrypt something](#decrypt-something)
|
|
49
|
+
- [examples](#examples)
|
|
50
|
+
* [Create a new `Keys` instance](#create-a-new-keys-instance)
|
|
51
|
+
* [Get a hash of the DID](#get-a-hash-of-the-did)
|
|
52
|
+
* [Persist the keys](#persist-the-keys)
|
|
53
|
+
* [Restore from indexedDB](#restore-from-indexeddb)
|
|
54
|
+
* [Sign something](#sign-something)
|
|
55
|
+
* [Get a signature as a string](#get-a-signature-as-a-string)
|
|
56
|
+
* [Verify a signature](#verify-a-signature)
|
|
57
|
+
* [Encrypt a key](#encrypt-a-key)
|
|
58
|
+
* [Asymmetrically encrypt some arbitrary data](#asymmetrically-encrypt-some-arbitrary-data)
|
|
59
|
+
* [Asymmetrically encrypt a string, return a new string](#asymmetrically-encrypt-a-string-return-a-new-string)
|
|
60
|
+
* [Decrypt a message](#decrypt-a-message)
|
|
61
|
+
* [`.decrypt.asString`](#decryptasstring)
|
|
62
|
+
* [In memory only](#in-memory-only)
|
|
63
|
+
- [AES](#aes)
|
|
64
|
+
* [`create`](#create)
|
|
65
|
+
* [`export`](#export)
|
|
66
|
+
* [`exportAsString`](#exportasstring)
|
|
67
|
+
* [`AES.encrypt`](#aesencrypt)
|
|
68
|
+
* [`AES.decrypt`](#aesdecrypt)
|
|
69
|
+
|
|
70
|
+
<!-- tocstop -->
|
|
71
|
+
|
|
72
|
+
</details>
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
## install
|
|
76
|
+
|
|
77
|
+
```sh
|
|
78
|
+
npm i -S @bicycle-codes/keys
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Asymmetric Encryption
|
|
82
|
+
|
|
83
|
+
Asymmetric encryption means using an AES key to encrypt the content, then
|
|
84
|
+
encrypting the AES key to a public RSA key. If you encrypt something to Bob's
|
|
85
|
+
public key, that means we create a new buffer of encrypted AES key +
|
|
86
|
+
encrypted content, where the encrypted AES key can only be decrypted by Bob's
|
|
87
|
+
private key.
|
|
88
|
+
|
|
89
|
+
## Modules
|
|
90
|
+
|
|
91
|
+
### `exports`
|
|
92
|
+
|
|
93
|
+
This exposes ESM and common JS via [package.json `exports` field](https://nodejs.org/api/packages.html#exports).
|
|
94
|
+
|
|
95
|
+
### ESM
|
|
96
|
+
```js
|
|
97
|
+
import { Keys } from '@bicycle-codes/keys'
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Common JS
|
|
101
|
+
```js
|
|
102
|
+
const { Keys } = require('@bicycle-codes/keys')
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### pre-built JS
|
|
106
|
+
This package exposes minified JS files too. Copy them to a location that is
|
|
107
|
+
accessible to your web server, then link to them in HTML.
|
|
108
|
+
|
|
109
|
+
#### copy
|
|
110
|
+
```sh
|
|
111
|
+
cp ./node_modules/@bicycle-codes/keys/dist/index.min.js ./public/keys.min.js
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### HTML
|
|
115
|
+
```html
|
|
116
|
+
<script type="module" src="./keys.min.js"></script>
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
------------------------------------------------------
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
## get started
|
|
123
|
+
|
|
124
|
+
### Create a keypair
|
|
125
|
+
Create a new keypair, then save it in `indexedDB`.
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
import { Keys } from '@bicycle-codes/keys'
|
|
129
|
+
|
|
130
|
+
const keys = await Keys.create()
|
|
131
|
+
|
|
132
|
+
// save the keys to indexedDB
|
|
133
|
+
await keys.persist()
|
|
134
|
+
|
|
135
|
+
// ... sometime in the future ...
|
|
136
|
+
// get our keys from indexedDB
|
|
137
|
+
const keysAgain = await Keys.load()
|
|
138
|
+
|
|
139
|
+
console.assert(keys.DID === keysAgain.DID) // true
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### some notes about the `keys` instance
|
|
143
|
+
|
|
144
|
+
#### `keys.DID`
|
|
145
|
+
|
|
146
|
+
This is the [DID string](https://www.w3.org/TR/did-1.0/) for the signing key for
|
|
147
|
+
this instance. The DID looks like this:
|
|
148
|
+
|
|
149
|
+
```js
|
|
150
|
+
'did:key:z13V3Sog2YaUKhdGCmgx9UZuW...'
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
#### `keys.getDeviceName` / `keys.deviceName`
|
|
154
|
+
|
|
155
|
+
Return the 32 character, DNS friendly hash of the signing public key.
|
|
156
|
+
|
|
157
|
+
```js
|
|
158
|
+
const name = await keys.getDeviceName()
|
|
159
|
+
|
|
160
|
+
// a promise is exposed as property `deviceName`
|
|
161
|
+
const name = await keys.deviceName
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
#### `keys.persisted`
|
|
165
|
+
|
|
166
|
+
A flag indicating whether `.persist` has been called, meaning that these keys
|
|
167
|
+
are saved in `indexedDB`.
|
|
168
|
+
|
|
169
|
+
#### `keys.publicEncryptKey`
|
|
170
|
+
|
|
171
|
+
The public encryption `CryptoKey`.
|
|
172
|
+
|
|
173
|
+
#### `keys.getPublicEncryptKey()`
|
|
174
|
+
|
|
175
|
+
Get the public encryption key, as a `base64` string. For other formats,
|
|
176
|
+
[see below](#keysgetpublicencryptkeyformat).
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
{
|
|
180
|
+
async getPublicEncryptKey ():Promise<string>
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
##### `keys.getPublicEncryptKey(format)`
|
|
185
|
+
|
|
186
|
+
Get the public encryption key. The given format should be a
|
|
187
|
+
[supported encoding](https://github.com/achingbrain/uint8arrays/blob/26684d4fa1a78f3e5c16e74bf13192e881db4fcf/src/util/bases.ts#L46) in
|
|
188
|
+
[uint8arrays](https://github.com/achingbrain/uint8arrays).
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
{
|
|
192
|
+
async getPublicEncryptKey (
|
|
193
|
+
format?:SupportedEncodings
|
|
194
|
+
):Promise<string>
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
#### keys.getPublicEncryptKey.uint8Array
|
|
199
|
+
|
|
200
|
+
Get the public encryption key as a `Uint8Array`.
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
{
|
|
204
|
+
function uint8Array:()=>Promise<Uint8Array<ArrayBufferLike>>
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Delete a keypair
|
|
209
|
+
|
|
210
|
+
Delete the keys from `indexedDB`.
|
|
211
|
+
|
|
212
|
+
```js
|
|
213
|
+
await keys.delete()
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
--------------------------------------------------------------------------
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
### sign and verify something
|
|
220
|
+
`.verify` takes the content, the signature, and the DID for the public key
|
|
221
|
+
used to sign. The DID is exposed as the property `.DID` on a `Keys` instance.
|
|
222
|
+
|
|
223
|
+
>
|
|
224
|
+
> [!NOTE]
|
|
225
|
+
> `verify` is exposed as a separate function, so you don't
|
|
226
|
+
> have to include all of `Keys` just to verify a signature.
|
|
227
|
+
>
|
|
228
|
+
|
|
229
|
+
```js
|
|
230
|
+
import { Keys, verify } from '@bicycle-codes/keys'
|
|
231
|
+
|
|
232
|
+
const keys = await Keys.create()
|
|
233
|
+
|
|
234
|
+
// sign something
|
|
235
|
+
const sig = await keys.sign.asString('hello string')
|
|
236
|
+
|
|
237
|
+
// verify the signature
|
|
238
|
+
const isOk = await verify('hello string', sig, keys.DID)
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### encrypt something
|
|
242
|
+
Take the public key we are encrypting to, return an `ArrayBuffer`, containing
|
|
243
|
+
the encrypted AES key concattenated with the `iv` and encrypted content.
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
```js
|
|
247
|
+
import { encryptTo } from '@bicycle-codes/keys'
|
|
248
|
+
|
|
249
|
+
// need to know the public key we are encrypting for
|
|
250
|
+
const publicKey = await keys.getPublicEncryptKey()
|
|
251
|
+
|
|
252
|
+
const encrypted = await encryptTo({
|
|
253
|
+
content: 'hello public key',
|
|
254
|
+
publicKey
|
|
255
|
+
}) // => ArrayBuffer
|
|
256
|
+
|
|
257
|
+
const encrypted = await encryptTo.asString({
|
|
258
|
+
content: 'hello public key',
|
|
259
|
+
publicKey
|
|
260
|
+
})
|
|
261
|
+
// => <encrypted text>
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
### decrypt something
|
|
265
|
+
A `Keys` instance has a method `decrypt`. The `encryptedMessage` argument is
|
|
266
|
+
an `ArrayBuffer` as returned from `encryptTo`, above.
|
|
267
|
+
|
|
268
|
+
```js
|
|
269
|
+
import { Keys } from '@bicycle-codes/keys'
|
|
270
|
+
|
|
271
|
+
const keys = await Keys.create()
|
|
272
|
+
// ...
|
|
273
|
+
const decrypted = await keys.decrypt(encryptedMsg)
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
----------------------------------------------------------------------
|
|
277
|
+
|
|
278
|
+
## examples
|
|
279
|
+
|
|
280
|
+
### Create a new `Keys` instance
|
|
281
|
+
|
|
282
|
+
Use the factory function `Keys.create`. The optional parameters,
|
|
283
|
+
`encryptionKeyName` and `signingKeyName`, are added as properties to the
|
|
284
|
+
`keys` instance -- `ENCRYPTION_KEY_NAME` and `SIGNING_KEY_NAME`. These are
|
|
285
|
+
used as indexes for saving the keys in `indexedDB`.
|
|
286
|
+
|
|
287
|
+
```ts
|
|
288
|
+
class Keys {
|
|
289
|
+
ENCRYPTION_KEY_NAME:string = 'encryption-key'
|
|
290
|
+
SIGNING_KEY_NAME:string = 'signing-key'
|
|
291
|
+
|
|
292
|
+
static async create (opts?:{
|
|
293
|
+
encryptionKeyName:string,
|
|
294
|
+
signingKeyName:string
|
|
295
|
+
}):Promise<Keys>
|
|
296
|
+
}
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
#### `.create()` example
|
|
300
|
+
|
|
301
|
+
Use the factory function b/c async.
|
|
302
|
+
|
|
303
|
+
```js
|
|
304
|
+
import { Keys } from '@bicycle-codes/keys'
|
|
305
|
+
|
|
306
|
+
const keys = await Keys.create()
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### Get a hash of the DID
|
|
310
|
+
Get a 32-character, DNS-friendly string of the hash of the given `DID`.
|
|
311
|
+
Available as static or instance method. If called as an instance method,
|
|
312
|
+
this will use the `DID` assigned to the given `Keys` instance.
|
|
313
|
+
|
|
314
|
+
The static method requires a `DID` string to be passed in.
|
|
315
|
+
|
|
316
|
+
#### static method
|
|
317
|
+
|
|
318
|
+
```ts
|
|
319
|
+
class Keys {
|
|
320
|
+
static async deviceName (did:DID):Promise<string>
|
|
321
|
+
}
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
#### instance method
|
|
325
|
+
|
|
326
|
+
If used as an instance method, this will use the `DID` assigned to the instance.
|
|
327
|
+
|
|
328
|
+
```ts
|
|
329
|
+
class Keys {
|
|
330
|
+
async getDeviceName ():Promise<string>
|
|
331
|
+
}
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
### Persist the keys
|
|
335
|
+
Save the keys to `indexedDB`. This depends on the values of class properties
|
|
336
|
+
`ENCRYPTION_KEY_NAME` and `SIGNING_KEY_NAME`. Set them if you want to change the
|
|
337
|
+
indexes under which the keys are saved to `indexedDB`.
|
|
338
|
+
|
|
339
|
+
By default we use these:
|
|
340
|
+
```js
|
|
341
|
+
const DEFAULT_ENC_NAME = 'encryption-key'
|
|
342
|
+
const DEFAULT_SIG_NAME = 'signing-key'
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
#### `.persist`
|
|
346
|
+
|
|
347
|
+
```ts
|
|
348
|
+
class Keys {
|
|
349
|
+
async persist ():Promise<void>
|
|
350
|
+
}
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
#### `.persist` example
|
|
354
|
+
```js
|
|
355
|
+
import { Keys } from '@bicycle-codes/keys'
|
|
356
|
+
|
|
357
|
+
const keys = await Keys.create()
|
|
358
|
+
keys.ENCRYPTION_KEY_NAME = 'encryption-key-custom-name'
|
|
359
|
+
keys.SIGNING_KEY_NAME = 'signing-key-custom-name'
|
|
360
|
+
await keys.persist()
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
### Restore from indexedDB
|
|
364
|
+
Create a `Keys` instance from data saved to `indexedDB`. Pass in different
|
|
365
|
+
`indexedDB` key names for the keys if you need to.
|
|
366
|
+
|
|
367
|
+
#### `static .load`
|
|
368
|
+
```ts
|
|
369
|
+
class Keys {
|
|
370
|
+
static async load (opts:{
|
|
371
|
+
encryptionKeyName,
|
|
372
|
+
signingKeyName
|
|
373
|
+
} = {
|
|
374
|
+
encryptionKeyName: DEFAULT_ENC_NAME,
|
|
375
|
+
signingKeyName: DEFAULT_SIG_NAME
|
|
376
|
+
}):Promise<Keys>
|
|
377
|
+
}
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
#### example
|
|
381
|
+
```js
|
|
382
|
+
import { Keys } from '@bicycle-codes/keys'
|
|
383
|
+
|
|
384
|
+
const newKeys = await Keys.load()
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
### Sign something
|
|
389
|
+
Create a new signature for the given input.
|
|
390
|
+
|
|
391
|
+
```ts
|
|
392
|
+
class Keys {
|
|
393
|
+
async sign (
|
|
394
|
+
msg:ArrayBuffer|string|Uint8Array,
|
|
395
|
+
charsize?:CharSize,
|
|
396
|
+
):Promise<Uint8Array>
|
|
397
|
+
}
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
#### example
|
|
401
|
+
```js
|
|
402
|
+
const sig = await keys.sign('hello signatures')
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
### Get a signature as a string
|
|
406
|
+
|
|
407
|
+
#### `keys.sign.asString(msg)`
|
|
408
|
+
|
|
409
|
+
```ts
|
|
410
|
+
{
|
|
411
|
+
/**
|
|
412
|
+
* Sign a message, return the signature as a base64 encoded string.
|
|
413
|
+
*
|
|
414
|
+
* @param {Msg} msg The message to sign
|
|
415
|
+
* @param {CharSize} [charsize] Character size
|
|
416
|
+
* @returns {Promise<string>}
|
|
417
|
+
*/
|
|
418
|
+
asString: async (msg:Msg, charsize?:CharSize):Promise<string>
|
|
419
|
+
}
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
```js
|
|
423
|
+
const sig = await keys.sign.asString('hello string')
|
|
424
|
+
// => ubW9PIjb360v...
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
### Verify a signature
|
|
428
|
+
Check if a given signature is valid. This is exposed as a stateless function so
|
|
429
|
+
that it can be used independently from any keypairs. You need to pass in the
|
|
430
|
+
data that was signed, the signature, and the `DID` string of the public key used
|
|
431
|
+
to create the signature.
|
|
432
|
+
|
|
433
|
+
```ts
|
|
434
|
+
async function verify (
|
|
435
|
+
msg:string|Uint8Array,
|
|
436
|
+
sig:string|Uint8Array,
|
|
437
|
+
signingDid:DID
|
|
438
|
+
):Promise<boolean>
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
```js
|
|
442
|
+
import { verify } from '@bicycle-codes/keys'
|
|
443
|
+
|
|
444
|
+
const isOk = await verify('hello string', sig, keys.DID)
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
### Encrypt a key
|
|
448
|
+
|
|
449
|
+
Use asynchronous (RSA) encryption to encrypt an AES key to the given public key.
|
|
450
|
+
|
|
451
|
+
```ts
|
|
452
|
+
async function encryptKeyTo ({ key, publicKey }:{
|
|
453
|
+
key:string|Uint8Array|CryptoKey;
|
|
454
|
+
publicKey:CryptoKey|Uint8Array|string;
|
|
455
|
+
}, format?:'uint8array'|'arraybuffer'):Promise<Uint8Array|ArrayBuffer>
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
#### example
|
|
459
|
+
```js
|
|
460
|
+
import { encryptKeyTo } from '@bicycle-codes/keys'
|
|
461
|
+
|
|
462
|
+
// pass in a CryptoKey
|
|
463
|
+
const encrypted = await encryptKeyTo({
|
|
464
|
+
key: myAesKey,
|
|
465
|
+
publicKey: keys.publicEncryptKey
|
|
466
|
+
})
|
|
467
|
+
|
|
468
|
+
// pass in a base64 string
|
|
469
|
+
const encryptedTwo = await encryptKeyTo({
|
|
470
|
+
key: aesKey,
|
|
471
|
+
publicKey: await keys.getPublicEncryptKey()
|
|
472
|
+
}) // => Uint8Array
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
#### encrypt a key, return a string
|
|
476
|
+
|
|
477
|
+
Encrypt the given key to the public key, and return the result as a
|
|
478
|
+
base64 string.
|
|
479
|
+
|
|
480
|
+
```ts
|
|
481
|
+
import { encryptKeyTo } from '@bicycle-codes/keys'
|
|
482
|
+
|
|
483
|
+
encryptKeyTo.asString = async function ({ key, publicKey }:{
|
|
484
|
+
key:string|Uint8Array|CryptoKey;
|
|
485
|
+
publicKey:CryptoKey|string|Uint8Array;
|
|
486
|
+
}, format?:SupportedEncodings):Promise<string> {
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
#### format
|
|
490
|
+
`encryptKeyTo.asString` takes an optional second argument for [the format](https://github.com/achingbrain/uint8arrays/blob/26684d4fa1a78f3e5c16e74bf13192e881db4fcf/src/util/bases.ts#L46)
|
|
491
|
+
of the returned string. Format is anything supported by [uint8arrays](https://github.com/achingbrain/uint8arrays). By default, if omitted, it is `base64`.
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
### Asymmetrically encrypt some arbitrary data
|
|
495
|
+
|
|
496
|
+
Encrypt the given message to the given public key. If an AES key is not
|
|
497
|
+
provided, one will be created. Use the AES key to encrypt the given
|
|
498
|
+
content, then encrypt the AES key to the given public key.
|
|
499
|
+
|
|
500
|
+
The return value is an ArrayBuffer containing the encrypted AES key +
|
|
501
|
+
the `iv` + the encrypted content.
|
|
502
|
+
|
|
503
|
+
To decrypt, pass the returned value to `keys.decrypt`, where `keys` is an
|
|
504
|
+
instance with the corresponding private key.
|
|
505
|
+
|
|
506
|
+
```ts
|
|
507
|
+
async function encryptTo (
|
|
508
|
+
opts:{
|
|
509
|
+
content:string|Uint8Array;
|
|
510
|
+
publicKey:CryptoKey|string;
|
|
511
|
+
},
|
|
512
|
+
aesKey?:SymmKey|Uint8Array|string,
|
|
513
|
+
):Promise<ArrayBuffer>
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
#### example
|
|
517
|
+
```js
|
|
518
|
+
import { encryptTo } from '@bicycle-codes/keys'
|
|
519
|
+
|
|
520
|
+
const encrypted = await encryptTo({
|
|
521
|
+
content: 'hello encryption',
|
|
522
|
+
publicKey: keys.publicEncryptKey
|
|
523
|
+
})
|
|
524
|
+
|
|
525
|
+
// => ArrayBuffer
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
### Asymmetrically encrypt a string, return a new string
|
|
529
|
+
|
|
530
|
+
Encrypt the given string, and return a new string that is the (encrypted) AES
|
|
531
|
+
key concattenated with the `iv` and cipher text. The
|
|
532
|
+
corresponding method `keys.decrypt.asString` will know how to parse and
|
|
533
|
+
decrypt the resulting text.
|
|
534
|
+
|
|
535
|
+
Use the functions `encryptTo.asString` and `keys.decrypt.asString`.
|
|
536
|
+
|
|
537
|
+
#### `keys.decrypt.asString`
|
|
538
|
+
```ts
|
|
539
|
+
async function asString (msg:string, keysize?:SymmKeyLength):Promise<string>
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
```js
|
|
543
|
+
import { Keys, encryptTo } from '@bicycle-codes/keys'
|
|
544
|
+
|
|
545
|
+
const keys = await Keys.create()
|
|
546
|
+
const pubKey = await keys.getPublicEncryptKey()
|
|
547
|
+
const msg = { type: 'test', content: 'hello' }
|
|
548
|
+
const cipherText = await encryptTo.asString({
|
|
549
|
+
content: JSON.stringify(msg),
|
|
550
|
+
// pass in a string public key or crypto key or Uint8Array
|
|
551
|
+
publicKey: pubKey
|
|
552
|
+
}) // => string
|
|
553
|
+
|
|
554
|
+
const text = await keys.decrypt.asString(cipherText)
|
|
555
|
+
const data = JSON.parse(text)
|
|
556
|
+
// => { type: 'test', content: 'hello' }
|
|
557
|
+
```
|
|
558
|
+
|
|
559
|
+
### Decrypt a message
|
|
560
|
+
```ts
|
|
561
|
+
class Keys {
|
|
562
|
+
async decrypt (
|
|
563
|
+
msg:string|Uint8Array|ArrayBuffer,
|
|
564
|
+
keysize?:SymmKeyLength
|
|
565
|
+
):Promise<Uint8Array>
|
|
566
|
+
}
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
```js
|
|
570
|
+
const decrypted = await keys.decrypt(encrypted)
|
|
571
|
+
// => Uint8Array
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
### `.decrypt.asString`
|
|
575
|
+
Decrypt a message, and stringify the result.
|
|
576
|
+
|
|
577
|
+
```ts
|
|
578
|
+
{
|
|
579
|
+
async function asString (msg:EncryptedMessage):Promise<string>
|
|
580
|
+
}
|
|
581
|
+
```
|
|
582
|
+
|
|
583
|
+
```js
|
|
584
|
+
await keys.decrypt.asString(encryptedString)
|
|
585
|
+
// => 'hello encryption'
|
|
586
|
+
```
|
|
587
|
+
|
|
588
|
+
### In memory only
|
|
589
|
+
Create a keypair, but do not save it in `indexedDB`, even if you call `persist`.
|
|
590
|
+
Pass an option `.session` to `.create` or `.load`.
|
|
591
|
+
|
|
592
|
+
```js
|
|
593
|
+
import { Keys } from '@bicycle-codes/keys'
|
|
594
|
+
|
|
595
|
+
const keys = await Keys.create({ session: true })
|
|
596
|
+
|
|
597
|
+
// or pass it to `.load`
|
|
598
|
+
const keysTwo = await Keys.load({ session: true })
|
|
599
|
+
```
|
|
600
|
+
|
|
601
|
+
## AES
|
|
602
|
+
Expose several AES functions with nice defaults.
|
|
603
|
+
|
|
604
|
+
* algorithm: `AES-GCM`
|
|
605
|
+
* key size: `256`
|
|
606
|
+
* `iv` size: [`12` bytes](https://crypto.stackexchange.com/questions/41601/aes-gcm-recommended-iv-size-why-12-bytes) (96 bits)
|
|
607
|
+
|
|
608
|
+
```js
|
|
609
|
+
import { AES } from '@bicycle-codes/keys'
|
|
610
|
+
|
|
611
|
+
const key = await AES.create(/* ... optional arguments ... */)
|
|
612
|
+
```
|
|
613
|
+
|
|
614
|
+
### `create`
|
|
615
|
+
Create a new AES key. By default uses 256 bits & GCM algorithm.
|
|
616
|
+
|
|
617
|
+
```ts
|
|
618
|
+
function create (opts:{ alg:string, length:number } = {
|
|
619
|
+
alg: DEFAULT_SYMM_ALGORITHM, // AES-GCM
|
|
620
|
+
length: DEFAULT_SYMM_LENGTH // 256
|
|
621
|
+
}):Promise<CryptoKey>
|
|
622
|
+
```
|
|
623
|
+
|
|
624
|
+
```ts
|
|
625
|
+
import { AES } from '@bicycle-codes/keys'
|
|
626
|
+
const aesKey = await AES.create()
|
|
627
|
+
```
|
|
628
|
+
|
|
629
|
+
### `export`
|
|
630
|
+
Get the AES key as a `Uint8Array`.
|
|
631
|
+
|
|
632
|
+
```ts
|
|
633
|
+
async function export (key:CryptoKey):Promise<Uint8Array>
|
|
634
|
+
```
|
|
635
|
+
|
|
636
|
+
```js
|
|
637
|
+
import { AES } from '@bicycle-codes/keys'
|
|
638
|
+
const exported = await AES.export(aesKey)
|
|
639
|
+
```
|
|
640
|
+
|
|
641
|
+
### `exportAsString`
|
|
642
|
+
Get the key as a string, `base64` encoded.
|
|
643
|
+
|
|
644
|
+
```ts
|
|
645
|
+
async function asString (
|
|
646
|
+
key:CryptoKey,
|
|
647
|
+
format?:SupportedEncoding
|
|
648
|
+
):Promise<string>
|
|
649
|
+
```
|
|
650
|
+
|
|
651
|
+
```js
|
|
652
|
+
import { AES } from '@bicycle-codes/keys'
|
|
653
|
+
const exported = await AES.export.asString(aesKey)
|
|
654
|
+
```
|
|
655
|
+
|
|
656
|
+
### `AES.encrypt`
|
|
657
|
+
|
|
658
|
+
Take a `Uint8Array`, return an encrypted `Uint8Array`.
|
|
659
|
+
|
|
660
|
+
```ts
|
|
661
|
+
async function encrypt (
|
|
662
|
+
data:Uint8Array,
|
|
663
|
+
cryptoKey:CryptoKey|Uint8Array,
|
|
664
|
+
iv?:Uint8Array
|
|
665
|
+
):Promise<Uint8Array>
|
|
666
|
+
```
|
|
667
|
+
|
|
668
|
+
```js
|
|
669
|
+
import { AES } from '@bicycle-codes/keys'
|
|
670
|
+
import { fromString } from 'uint8arrays'
|
|
671
|
+
|
|
672
|
+
const encryptedText = await AES.encrypt(fromString('hello AES'), aesKey)
|
|
673
|
+
```
|
|
674
|
+
|
|
675
|
+
### `AES.decrypt`
|
|
676
|
+
```ts
|
|
677
|
+
async function decrypt (
|
|
678
|
+
encryptedData:Uint8Array|string,
|
|
679
|
+
cryptoKey:CryptoKey|Uint8Array|ArrayBuffer,
|
|
680
|
+
iv?:Uint8Array
|
|
681
|
+
):Promise<Uint8Array>
|
|
682
|
+
```
|
|
683
|
+
|
|
684
|
+
```js
|
|
685
|
+
import { AES } from '@bicycle-codes/keys'
|
|
686
|
+
|
|
687
|
+
const decryptedText = await AES.decrypt(encryptedText, aesKey)
|
|
688
|
+
```
|