@libp2p/crypto 2.0.6-d5ef1c91 → 2.0.7-051154dd
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 +6 -293
- package/dist/src/aes/index.d.ts +44 -0
- package/dist/src/aes/index.d.ts.map +1 -1
- package/dist/src/aes/index.js +44 -0
- package/dist/src/aes/index.js.map +1 -1
- package/dist/src/hmac/index.d.ts +16 -0
- package/dist/src/hmac/index.d.ts.map +1 -1
- package/dist/src/hmac/index.js +16 -0
- package/dist/src/hmac/index.js.map +1 -1
- package/dist/src/index.d.ts +9 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +9 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/keys/ecdh.d.ts +5 -0
- package/dist/src/keys/ecdh.d.ts.map +1 -1
- package/dist/src/keys/ecdh.js +5 -0
- package/dist/src/keys/ecdh.js.map +1 -1
- package/dist/src/keys/index.d.ts +36 -2
- package/dist/src/keys/index.d.ts.map +1 -1
- package/dist/src/keys/index.js +36 -11
- package/dist/src/keys/index.js.map +1 -1
- package/dist/src/random-bytes.d.ts +3 -0
- package/dist/src/random-bytes.d.ts.map +1 -1
- package/dist/src/random-bytes.js +3 -0
- package/dist/src/random-bytes.js.map +1 -1
- package/package.json +2 -2
- package/src/aes/index.ts +45 -0
- package/src/hmac/index.ts +17 -0
- package/src/index.ts +10 -0
- package/src/keys/ecdh.ts +5 -0
- package/src/keys/index.ts +37 -11
- package/src/random-bytes.ts +3 -0
package/README.md
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
# @libp2p/crypto <!-- omit in toc -->
|
|
2
|
-
|
|
3
1
|
[](http://libp2p.io/)
|
|
4
2
|
[](https://discuss.libp2p.io)
|
|
5
3
|
[](https://codecov.io/gh/libp2p/js-libp2p)
|
|
@@ -7,45 +5,13 @@
|
|
|
7
5
|
|
|
8
6
|
> Crypto primitives for libp2p
|
|
9
7
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
- [Install](#install)
|
|
13
|
-
- [Browser `<script>` tag](#browser-script-tag)
|
|
14
|
-
- [Lead Maintainer](#lead-maintainer)
|
|
15
|
-
- [Usage](#usage)
|
|
16
|
-
- [Web Crypto API](#web-crypto-api)
|
|
17
|
-
- [API](#api)
|
|
18
|
-
- [`crypto.aes`](#cryptoaes)
|
|
19
|
-
- [`crypto.aes.create(key, iv)`](#cryptoaescreatekey-iv)
|
|
20
|
-
- [`decrypt(data)`](#decryptdata)
|
|
21
|
-
- [`encrypt(data)`](#encryptdata)
|
|
22
|
-
- [`crypto.hmac`](#cryptohmac)
|
|
23
|
-
- [`crypto.hmac.create(hash, secret)`](#cryptohmaccreatehash-secret)
|
|
24
|
-
- [`digest(data)`](#digestdata)
|
|
25
|
-
- [`crypto.keys`](#cryptokeys)
|
|
26
|
-
- [`crypto.keys.generateKeyPair(type, bits)`](#cryptokeysgeneratekeypairtype-bits)
|
|
27
|
-
- [`crypto.keys.generateEphemeralKeyPair(curve)`](#cryptokeysgenerateephemeralkeypaircurve)
|
|
28
|
-
- [`crypto.keys.keyStretcher(cipherType, hashType, secret)`](#cryptokeyskeystretcherciphertype-hashtype-secret)
|
|
29
|
-
- [`crypto.keys.marshalPublicKey(key, [type])`](#cryptokeysmarshalpublickeykey-type)
|
|
30
|
-
- [`crypto.keys.unmarshalPublicKey(buf)`](#cryptokeysunmarshalpublickeybuf)
|
|
31
|
-
- [`crypto.keys.marshalPrivateKey(key, [type])`](#cryptokeysmarshalprivatekeykey-type)
|
|
32
|
-
- [`crypto.keys.unmarshalPrivateKey(buf)`](#cryptokeysunmarshalprivatekeybuf)
|
|
33
|
-
- [`crypto.keys.import(encryptedKey, password)`](#cryptokeysimportencryptedkey-password)
|
|
34
|
-
- [`privateKey.export(password, format)`](#privatekeyexportpassword-format)
|
|
35
|
-
- [`crypto.randomBytes(number)`](#cryptorandombytesnumber)
|
|
36
|
-
- [`crypto.pbkdf2(password, salt, iterations, keySize, hash)`](#cryptopbkdf2password-salt-iterations-keysize-hash)
|
|
37
|
-
- [Contribute](#contribute)
|
|
38
|
-
- [API Docs](#api-docs)
|
|
39
|
-
- [License](#license)
|
|
40
|
-
- [Contribution](#contribution)
|
|
41
|
-
|
|
42
|
-
## Install
|
|
8
|
+
# Install
|
|
43
9
|
|
|
44
10
|
```console
|
|
45
11
|
$ npm i @libp2p/crypto
|
|
46
12
|
```
|
|
47
13
|
|
|
48
|
-
|
|
14
|
+
## Browser `<script>` tag
|
|
49
15
|
|
|
50
16
|
Loading this module through a script tag will make it's exports available as `Libp2pCrypto` in the global namespace.
|
|
51
17
|
|
|
@@ -53,260 +19,7 @@ Loading this module through a script tag will make it's exports available as `Li
|
|
|
53
19
|
<script src="https://unpkg.com/@libp2p/crypto/dist/index.min.js"></script>
|
|
54
20
|
```
|
|
55
21
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
## Lead Maintainer
|
|
59
|
-
|
|
60
|
-
[Jacob Heun](https://github.com/jacobheun/)
|
|
61
|
-
|
|
62
|
-
## Usage
|
|
63
|
-
|
|
64
|
-
```js
|
|
65
|
-
const crypto = require('libp2p-crypto')
|
|
66
|
-
|
|
67
|
-
// Now available to you:
|
|
68
|
-
//
|
|
69
|
-
// crypto.aes
|
|
70
|
-
// crypto.hmac
|
|
71
|
-
// crypto.keys
|
|
72
|
-
// etc.
|
|
73
|
-
//
|
|
74
|
-
// See full API details below...
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
### Web Crypto API
|
|
78
|
-
|
|
79
|
-
The `libp2p-crypto` library depends on the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) in the browser. Web Crypto is available in all modern browsers, however browsers restrict its usage to [Secure Contexts](https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts).
|
|
80
|
-
|
|
81
|
-
**This means you will not be able to use some `libp2p-crypto` functions in the browser when the page is served over HTTP.** To enable the Web Crypto API and allow `libp2p-crypto` to work fully, please serve your page over HTTPS.
|
|
82
|
-
|
|
83
|
-
## API
|
|
84
|
-
|
|
85
|
-
### `crypto.aes`
|
|
86
|
-
|
|
87
|
-
Exposes an interface to AES encryption (formerly Rijndael), as defined in U.S. Federal Information Processing Standards Publication 197.
|
|
88
|
-
|
|
89
|
-
This uses `CTR` mode.
|
|
90
|
-
|
|
91
|
-
#### `crypto.aes.create(key, iv)`
|
|
92
|
-
|
|
93
|
-
- `key: Uint8Array` The key, if length `16` then `AES 128` is used. For length `32`, `AES 256` is used.
|
|
94
|
-
- `iv: Uint8Array` Must have length `16`.
|
|
95
|
-
|
|
96
|
-
Returns `Promise<{decrypt<Function>, encrypt<Function>}>`
|
|
97
|
-
|
|
98
|
-
##### `decrypt(data)`
|
|
99
|
-
|
|
100
|
-
- `data: Uint8Array`
|
|
101
|
-
|
|
102
|
-
Returns `Promise<Uint8Array>`
|
|
103
|
-
|
|
104
|
-
##### `encrypt(data)`
|
|
105
|
-
|
|
106
|
-
- `data: Uint8Array`
|
|
107
|
-
|
|
108
|
-
Returns `Promise<Uint8Array>`
|
|
109
|
-
|
|
110
|
-
```js
|
|
111
|
-
const crypto = require('libp2p-crypto')
|
|
112
|
-
|
|
113
|
-
// Setting up Key and IV
|
|
114
|
-
|
|
115
|
-
// A 16 bytes array, 128 Bits, AES-128 is chosen
|
|
116
|
-
const key128 = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
|
|
117
|
-
|
|
118
|
-
// A 16 bytes array, 128 Bits,
|
|
119
|
-
const IV = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
|
|
120
|
-
|
|
121
|
-
async function main () {
|
|
122
|
-
const decryptedMessage = 'Hello, world!'
|
|
123
|
-
|
|
124
|
-
// Encrypting
|
|
125
|
-
const cipher = await crypto.aes.create(key128, IV)
|
|
126
|
-
const encryptedBuffer = await cipher.encrypt(Uint8Array.from(decryptedMessage))
|
|
127
|
-
console.log(encryptedBuffer)
|
|
128
|
-
// prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>
|
|
129
|
-
|
|
130
|
-
// Decrypting
|
|
131
|
-
const decipher = await crypto.aes.create(key128, IV)
|
|
132
|
-
const decryptedBuffer = await cipher.decrypt(encryptedBuffer)
|
|
133
|
-
|
|
134
|
-
console.log(decryptedBuffer)
|
|
135
|
-
// prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>
|
|
136
|
-
|
|
137
|
-
console.log(decryptedBuffer.toString('utf-8'))
|
|
138
|
-
// prints: Hello, world!
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
main()
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
### `crypto.hmac`
|
|
145
|
-
|
|
146
|
-
Exposes an interface to the Keyed-Hash Message Authentication Code (HMAC) as defined in U.S. Federal Information Processing Standards Publication 198. An HMAC is a cryptographic hash that uses a key to sign a message. The receiver verifies the hash by recomputing it using the same key.
|
|
147
|
-
|
|
148
|
-
#### `crypto.hmac.create(hash, secret)`
|
|
149
|
-
|
|
150
|
-
- `hash: String`
|
|
151
|
-
- `secret: Uint8Array`
|
|
152
|
-
|
|
153
|
-
Returns `Promise<{digest<Function>}>`
|
|
154
|
-
|
|
155
|
-
##### `digest(data)`
|
|
156
|
-
|
|
157
|
-
- `data: Uint8Array`
|
|
158
|
-
|
|
159
|
-
Returns `Promise<Uint8Array>`
|
|
160
|
-
|
|
161
|
-
Example:
|
|
162
|
-
|
|
163
|
-
```js
|
|
164
|
-
const crypto = require('libp2p-crypto')
|
|
165
|
-
|
|
166
|
-
async function main () {
|
|
167
|
-
const hash = 'SHA1' // 'SHA256' || 'SHA512'
|
|
168
|
-
const hmac = await crypto.hmac.create(hash, uint8ArrayFromString('secret'))
|
|
169
|
-
const sig = await hmac.digest(uint8ArrayFromString('hello world'))
|
|
170
|
-
console.log(sig)
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
main()
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
### `crypto.keys`
|
|
177
|
-
|
|
178
|
-
**Supported Key Types**
|
|
179
|
-
|
|
180
|
-
The [`generateKeyPair`](#generatekeypairtype-bits), [`marshalPublicKey`](#marshalpublickeykey-type), and [`marshalPrivateKey`](#marshalprivatekeykey-type) functions accept a string `type` argument.
|
|
181
|
-
|
|
182
|
-
Currently the `'RSA'`, `'ed25519'`, and `secp256k1` types are supported, although ed25519 and secp256k1 keys support only signing and verification of messages. For encryption / decryption support, RSA keys should be used.
|
|
183
|
-
|
|
184
|
-
### `crypto.keys.generateKeyPair(type, bits)`
|
|
185
|
-
|
|
186
|
-
- `type: String`, see [Supported Key Types](#supported-key-types) above.
|
|
187
|
-
- `bits: Number` Minimum of 1024
|
|
188
|
-
|
|
189
|
-
Returns `Promise<{privateKey<Uint8Array>, publicKey<Uint8Array>}>`
|
|
190
|
-
|
|
191
|
-
Generates a keypair of the given type and bitsize.
|
|
192
|
-
|
|
193
|
-
### `crypto.keys.generateEphemeralKeyPair(curve)`
|
|
194
|
-
|
|
195
|
-
- `curve: String`, one of `'P-256'`, `'P-384'`, `'P-521'` is currently supported
|
|
196
|
-
|
|
197
|
-
Returns `Promise`
|
|
198
|
-
|
|
199
|
-
Generates an ephemeral public key and returns a function that will compute the shared secret key.
|
|
200
|
-
|
|
201
|
-
Focuses only on ECDH now, but can be made more general in the future.
|
|
202
|
-
|
|
203
|
-
Resolves to an object of the form:
|
|
204
|
-
|
|
205
|
-
```js
|
|
206
|
-
{
|
|
207
|
-
key: Uint8Array,
|
|
208
|
-
genSharedKey: Function
|
|
209
|
-
}
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
### `crypto.keys.keyStretcher(cipherType, hashType, secret)`
|
|
213
|
-
|
|
214
|
-
- `cipherType: String`, one of `'AES-128'`, `'AES-256'`, `'Blowfish'`
|
|
215
|
-
- `hashType: String`, one of `'SHA1'`, `SHA256`, `SHA512`
|
|
216
|
-
- `secret: Uint8Array`
|
|
217
|
-
|
|
218
|
-
Returns `Promise`
|
|
219
|
-
|
|
220
|
-
Generates a set of keys for each party by stretching the shared key.
|
|
221
|
-
|
|
222
|
-
Resolves to an object of the form:
|
|
223
|
-
|
|
224
|
-
```js
|
|
225
|
-
{
|
|
226
|
-
k1: {
|
|
227
|
-
iv: Uint8Array,
|
|
228
|
-
cipherKey: Uint8Array,
|
|
229
|
-
macKey: Uint8Array
|
|
230
|
-
},
|
|
231
|
-
k2: {
|
|
232
|
-
iv: Uint8Array,
|
|
233
|
-
cipherKey: Uint8Array,
|
|
234
|
-
macKey: Uint8Array
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
### `crypto.keys.marshalPublicKey(key, [type])`
|
|
240
|
-
|
|
241
|
-
- `key: keys.rsa.RsaPublicKey | keys.ed25519.Ed25519PublicKey | keys.secp256k1.Secp256k1PublicKey`
|
|
242
|
-
- `type: String`, see [Supported Key Types](#supported-key-types) above. Defaults to 'rsa'.
|
|
243
|
-
|
|
244
|
-
Returns `Uint8Array`
|
|
245
|
-
|
|
246
|
-
Converts a public key object into a protobuf serialized public key.
|
|
247
|
-
|
|
248
|
-
### `crypto.keys.unmarshalPublicKey(buf)`
|
|
249
|
-
|
|
250
|
-
- `buf: Uint8Array`
|
|
251
|
-
|
|
252
|
-
Returns `RsaPublicKey|Ed25519PublicKey|Secp256k1PublicKey`
|
|
253
|
-
|
|
254
|
-
Converts a protobuf serialized public key into its representative object.
|
|
255
|
-
|
|
256
|
-
### `crypto.keys.marshalPrivateKey(key, [type])`
|
|
257
|
-
|
|
258
|
-
- `key: keys.rsa.RsaPrivateKey | keys.ed25519.Ed25519PrivateKey | keys.secp256k1.Secp256k1PrivateKey`
|
|
259
|
-
- `type: String`, see [Supported Key Types](#supported-key-types) above.
|
|
260
|
-
|
|
261
|
-
Returns `Uint8Array`
|
|
262
|
-
|
|
263
|
-
Converts a private key object into a protobuf serialized private key.
|
|
264
|
-
|
|
265
|
-
### `crypto.keys.unmarshalPrivateKey(buf)`
|
|
266
|
-
|
|
267
|
-
- `buf: Uint8Array`
|
|
268
|
-
|
|
269
|
-
Returns `Promise<RsaPrivateKey|Ed25519PrivateKey|Secp256k1PrivateKey>`
|
|
270
|
-
|
|
271
|
-
Converts a protobuf serialized private key into its representative object.
|
|
272
|
-
|
|
273
|
-
### `crypto.keys.import(encryptedKey, password)`
|
|
274
|
-
|
|
275
|
-
- `encryptedKey: string`
|
|
276
|
-
- `password: string`
|
|
277
|
-
|
|
278
|
-
Returns `Promise<PrivateKey>`
|
|
279
|
-
|
|
280
|
-
Converts an exported private key into its representative object. Supported formats are 'pem' (RSA only) and 'libp2p-key'.
|
|
281
|
-
|
|
282
|
-
### `privateKey.export(password, format)`
|
|
283
|
-
|
|
284
|
-
- `password: string`
|
|
285
|
-
- `format: string` the format to export to: 'pem' (rsa only), 'libp2p-key'
|
|
286
|
-
|
|
287
|
-
Returns `string`
|
|
288
|
-
|
|
289
|
-
Exports the password protected `PrivateKey`. RSA keys will be exported as password protected PEM by default. Ed25519 and Secp256k1 keys will be exported as password protected AES-GCM base64 encoded strings ('libp2p-key' format).
|
|
290
|
-
|
|
291
|
-
### `crypto.randomBytes(number)`
|
|
292
|
-
|
|
293
|
-
- `number: Number`
|
|
294
|
-
|
|
295
|
-
Returns `Uint8Array`
|
|
296
|
-
|
|
297
|
-
Generates a Uint8Array with length `number` populated by random bytes.
|
|
298
|
-
|
|
299
|
-
### `crypto.pbkdf2(password, salt, iterations, keySize, hash)`
|
|
300
|
-
|
|
301
|
-
- `password: String`
|
|
302
|
-
- `salt: String`
|
|
303
|
-
- `iterations: Number`
|
|
304
|
-
- `keySize: Number` in bytes
|
|
305
|
-
- `hash: String` the hashing algorithm ('sha1', 'sha2-512', ...)
|
|
306
|
-
|
|
307
|
-
Computes the Password Based Key Derivation Function 2; returning a new password.
|
|
308
|
-
|
|
309
|
-
## Contribute
|
|
22
|
+
# Contribute
|
|
310
23
|
|
|
311
24
|
Feel free to join in. All welcome. Open an [issue](https://github.com/libp2p/js-libp2p-crypto/issues)!
|
|
312
25
|
|
|
@@ -314,17 +27,17 @@ This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/c
|
|
|
314
27
|
|
|
315
28
|
[](https://github.com/ipfs/community/blob/master/contributing.md)
|
|
316
29
|
|
|
317
|
-
|
|
30
|
+
# API Docs
|
|
318
31
|
|
|
319
32
|
- <https://libp2p.github.io/js-libp2p/modules/_libp2p_crypto.html>
|
|
320
33
|
|
|
321
|
-
|
|
34
|
+
# License
|
|
322
35
|
|
|
323
36
|
Licensed under either of
|
|
324
37
|
|
|
325
38
|
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
|
|
326
39
|
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
|
|
327
40
|
|
|
328
|
-
|
|
41
|
+
# Contribution
|
|
329
42
|
|
|
330
43
|
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
|
package/dist/src/aes/index.d.ts
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* Exposes an interface to AES encryption (formerly Rijndael), as defined in U.S. Federal Information Processing Standards Publication 197.
|
|
5
|
+
*
|
|
6
|
+
* This uses `CTR` mode.
|
|
7
|
+
*
|
|
8
|
+
* /**
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```js
|
|
12
|
+
* import { create } from '@libp2p/crypto/aes'
|
|
13
|
+
*
|
|
14
|
+
* // Setting up Key and IV
|
|
15
|
+
*
|
|
16
|
+
* // A 16 bytes array, 128 Bits, AES-128 is chosen
|
|
17
|
+
* const key128 = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
|
|
18
|
+
*
|
|
19
|
+
* // A 16 bytes array, 128 Bits,
|
|
20
|
+
* const IV = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
|
|
21
|
+
*
|
|
22
|
+
* const decryptedMessage = 'Hello, world!'
|
|
23
|
+
*
|
|
24
|
+
* // Encrypting
|
|
25
|
+
* const cipher = await crypto.aes.create(key128, IV)
|
|
26
|
+
* const encryptedBuffer = await encrypt(Uint8Array.from(decryptedMessage))
|
|
27
|
+
* console.log(encryptedBuffer)
|
|
28
|
+
* // prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>
|
|
29
|
+
*
|
|
30
|
+
* // Decrypting
|
|
31
|
+
* const decipher = await crypto.aes.create(key128, IV)
|
|
32
|
+
* const decryptedBuffer = await decrypt(encryptedBuffer)
|
|
33
|
+
*
|
|
34
|
+
* console.log(decryptedBuffer)
|
|
35
|
+
* // prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>
|
|
36
|
+
*
|
|
37
|
+
* console.log(decryptedBuffer.toString('utf-8'))
|
|
38
|
+
* // prints: Hello, world!
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
1
41
|
export interface AESCipher {
|
|
2
42
|
encrypt(data: Uint8Array): Promise<Uint8Array>;
|
|
3
43
|
decrypt(data: Uint8Array): Promise<Uint8Array>;
|
|
4
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* @param key - The key, if length `16` then `AES 128` is used. For length `32`, `AES 256` is used
|
|
47
|
+
* @param iv - Must have length `16`
|
|
48
|
+
*/
|
|
5
49
|
export declare function create(key: Uint8Array, iv: Uint8Array): Promise<AESCipher>;
|
|
6
50
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/aes/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/aes/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAKH,MAAM,WAAW,SAAS;IACxB,OAAO,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAAA;IAC9C,OAAO,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAAA;CAC/C;AAED;;;GAGG;AACH,wBAAsB,MAAM,CAAE,GAAG,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,GAAG,OAAO,CAAC,SAAS,CAAC,CAgBjF"}
|
package/dist/src/aes/index.js
CHANGED
|
@@ -1,5 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* Exposes an interface to AES encryption (formerly Rijndael), as defined in U.S. Federal Information Processing Standards Publication 197.
|
|
5
|
+
*
|
|
6
|
+
* This uses `CTR` mode.
|
|
7
|
+
*
|
|
8
|
+
* /**
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```js
|
|
12
|
+
* import { create } from '@libp2p/crypto/aes'
|
|
13
|
+
*
|
|
14
|
+
* // Setting up Key and IV
|
|
15
|
+
*
|
|
16
|
+
* // A 16 bytes array, 128 Bits, AES-128 is chosen
|
|
17
|
+
* const key128 = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
|
|
18
|
+
*
|
|
19
|
+
* // A 16 bytes array, 128 Bits,
|
|
20
|
+
* const IV = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
|
|
21
|
+
*
|
|
22
|
+
* const decryptedMessage = 'Hello, world!'
|
|
23
|
+
*
|
|
24
|
+
* // Encrypting
|
|
25
|
+
* const cipher = await crypto.aes.create(key128, IV)
|
|
26
|
+
* const encryptedBuffer = await encrypt(Uint8Array.from(decryptedMessage))
|
|
27
|
+
* console.log(encryptedBuffer)
|
|
28
|
+
* // prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>
|
|
29
|
+
*
|
|
30
|
+
* // Decrypting
|
|
31
|
+
* const decipher = await crypto.aes.create(key128, IV)
|
|
32
|
+
* const decryptedBuffer = await decrypt(encryptedBuffer)
|
|
33
|
+
*
|
|
34
|
+
* console.log(decryptedBuffer)
|
|
35
|
+
* // prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>
|
|
36
|
+
*
|
|
37
|
+
* console.log(decryptedBuffer.toString('utf-8'))
|
|
38
|
+
* // prints: Hello, world!
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
1
41
|
import { cipherMode } from './cipher-mode.js';
|
|
2
42
|
import * as ciphers from './ciphers.js';
|
|
43
|
+
/**
|
|
44
|
+
* @param key - The key, if length `16` then `AES 128` is used. For length `32`, `AES 256` is used
|
|
45
|
+
* @param iv - Must have length `16`
|
|
46
|
+
*/
|
|
3
47
|
export async function create(key, iv) {
|
|
4
48
|
const mode = cipherMode(key);
|
|
5
49
|
const cipher = ciphers.createCipheriv(mode, key, iv);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/aes/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAC7C,OAAO,KAAK,OAAO,MAAM,cAAc,CAAA;AAOvC,MAAM,CAAC,KAAK,UAAU,MAAM,CAAE,GAAe,EAAE,EAAc;IAC3D,MAAM,IAAI,GAAG,UAAU,CAAC,GAAG,CAAC,CAAA;IAC5B,MAAM,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE,CAAC,CAAA;IACpD,MAAM,QAAQ,GAAG,OAAO,CAAC,gBAAgB,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE,CAAC,CAAA;IAExD,MAAM,GAAG,GAAc;QACrB,KAAK,CAAC,OAAO,CAAE,IAAI;YACjB,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QAC5B,CAAC;QAED,KAAK,CAAC,OAAO,CAAE,IAAI;YACjB,OAAO,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QAC9B,CAAC;KACF,CAAA;IAED,OAAO,GAAG,CAAA;AACZ,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/aes/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAC7C,OAAO,KAAK,OAAO,MAAM,cAAc,CAAA;AAOvC;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM,CAAE,GAAe,EAAE,EAAc;IAC3D,MAAM,IAAI,GAAG,UAAU,CAAC,GAAG,CAAC,CAAA;IAC5B,MAAM,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE,CAAC,CAAA;IACpD,MAAM,QAAQ,GAAG,OAAO,CAAC,gBAAgB,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE,CAAC,CAAA;IAExD,MAAM,GAAG,GAAc;QACrB,KAAK,CAAC,OAAO,CAAE,IAAI;YACjB,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QAC5B,CAAC;QAED,KAAK,CAAC,OAAO,CAAE,IAAI;YACjB,OAAO,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QAC9B,CAAC;KACF,CAAA;IAED,OAAO,GAAG,CAAA;AACZ,CAAC"}
|
package/dist/src/hmac/index.d.ts
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* Exposes an interface to the Keyed-Hash Message Authentication Code (HMAC) as defined in U.S. Federal Information Processing Standards Publication 198. An HMAC is a cryptographic hash that uses a key to sign a message. The receiver verifies the hash by recomputing it using the same key.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```js
|
|
9
|
+
* import { create } from '@libp2p/hmac'
|
|
10
|
+
*
|
|
11
|
+
* const hash = 'SHA1' // 'SHA256' || 'SHA512'
|
|
12
|
+
* const hmac = await crypto.hmac.create(hash, uint8ArrayFromString('secret'))
|
|
13
|
+
* const sig = await hmac.digest(uint8ArrayFromString('hello world'))
|
|
14
|
+
* console.log(sig)
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
1
17
|
export interface HMAC {
|
|
2
18
|
digest(data: Uint8Array): Promise<Uint8Array>;
|
|
3
19
|
length: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hmac/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hmac/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAKH,MAAM,WAAW,IAAI;IACnB,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAAA;IAC7C,MAAM,EAAE,MAAM,CAAA;CACf;AAED,wBAAsB,MAAM,CAAE,IAAI,EAAE,MAAM,GAAG,QAAQ,GAAG,QAAQ,EAAE,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAWnG"}
|
package/dist/src/hmac/index.js
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* Exposes an interface to the Keyed-Hash Message Authentication Code (HMAC) as defined in U.S. Federal Information Processing Standards Publication 198. An HMAC is a cryptographic hash that uses a key to sign a message. The receiver verifies the hash by recomputing it using the same key.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```js
|
|
9
|
+
* import { create } from '@libp2p/hmac'
|
|
10
|
+
*
|
|
11
|
+
* const hash = 'SHA1' // 'SHA256' || 'SHA512'
|
|
12
|
+
* const hmac = await crypto.hmac.create(hash, uint8ArrayFromString('secret'))
|
|
13
|
+
* const sig = await hmac.digest(uint8ArrayFromString('hello world'))
|
|
14
|
+
* console.log(sig)
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
1
17
|
import crypto from 'crypto';
|
|
2
18
|
import lengths from './lengths.js';
|
|
3
19
|
export async function create(hash, secret) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/hmac/index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAA;AAC3B,OAAO,OAAO,MAAM,cAAc,CAAA;AAOlC,MAAM,CAAC,KAAK,UAAU,MAAM,CAAE,IAAkC,EAAE,MAAkB;IAClF,MAAM,GAAG,GAAG;QACV,KAAK,CAAC,MAAM,CAAE,IAAgB;YAC5B,MAAM,IAAI,GAAG,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE,MAAM,CAAC,CAAA;YAC1D,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;YACjB,OAAO,IAAI,CAAC,MAAM,EAAE,CAAA;QACtB,CAAC;QACD,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC;KACtB,CAAA;IAED,OAAO,GAAG,CAAA;AACZ,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/hmac/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,MAAM,MAAM,QAAQ,CAAA;AAC3B,OAAO,OAAO,MAAM,cAAc,CAAA;AAOlC,MAAM,CAAC,KAAK,UAAU,MAAM,CAAE,IAAkC,EAAE,MAAkB;IAClF,MAAM,GAAG,GAAG;QACV,KAAK,CAAC,MAAM,CAAE,IAAgB;YAC5B,MAAM,IAAI,GAAG,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE,MAAM,CAAC,CAAA;YAC1D,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;YACjB,OAAO,IAAI,CAAC,MAAM,EAAE,CAAA;QACtB,CAAC;QACD,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC;KACtB,CAAA;IAED,OAAO,GAAG,CAAA;AACZ,CAAC"}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* The `libp2p-crypto` library depends on the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) in the browser. Web Crypto is available in all modern browsers, however browsers restrict its usage to [Secure Contexts](https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts).
|
|
5
|
+
*
|
|
6
|
+
* *This means you will not be able to use some `@libp2p/crypto` functions in the browser when the page is served over HTTP.*
|
|
7
|
+
*
|
|
8
|
+
* To enable the Web Crypto API and allow `@libp2p/crypto` to work fully, please serve your page over HTTPS.
|
|
9
|
+
*/
|
|
1
10
|
import * as aes from './aes/index.js';
|
|
2
11
|
import * as hmac from './hmac/index.js';
|
|
3
12
|
import * as keys from './keys/index.js';
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAA;AACrC,OAAO,KAAK,IAAI,MAAM,iBAAiB,CAAA;AACvC,OAAO,KAAK,IAAI,MAAM,iBAAiB,CAAA;AACvC,OAAO,MAAM,MAAM,aAAa,CAAA;AAChC,OAAO,WAAW,MAAM,mBAAmB,CAAA;AAE3C,OAAO,EAAE,GAAG,EAAE,CAAA;AACd,OAAO,EAAE,IAAI,EAAE,CAAA;AACf,OAAO,EAAE,IAAI,EAAE,CAAA;AACf,OAAO,EAAE,WAAW,EAAE,CAAA;AACtB,OAAO,EAAE,MAAM,EAAE,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAA;AACrC,OAAO,KAAK,IAAI,MAAM,iBAAiB,CAAA;AACvC,OAAO,KAAK,IAAI,MAAM,iBAAiB,CAAA;AACvC,OAAO,MAAM,MAAM,aAAa,CAAA;AAChC,OAAO,WAAW,MAAM,mBAAmB,CAAA;AAE3C,OAAO,EAAE,GAAG,EAAE,CAAA;AACd,OAAO,EAAE,IAAI,EAAE,CAAA;AACf,OAAO,EAAE,IAAI,EAAE,CAAA;AACf,OAAO,EAAE,WAAW,EAAE,CAAA;AACtB,OAAO,EAAE,MAAM,EAAE,CAAA"}
|
package/dist/src/index.js
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* The `libp2p-crypto` library depends on the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) in the browser. Web Crypto is available in all modern browsers, however browsers restrict its usage to [Secure Contexts](https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts).
|
|
5
|
+
*
|
|
6
|
+
* *This means you will not be able to use some `@libp2p/crypto` functions in the browser when the page is served over HTTP.*
|
|
7
|
+
*
|
|
8
|
+
* To enable the Web Crypto API and allow `@libp2p/crypto` to work fully, please serve your page over HTTPS.
|
|
9
|
+
*/
|
|
1
10
|
import * as aes from './aes/index.js';
|
|
2
11
|
import * as hmac from './hmac/index.js';
|
|
3
12
|
import * as keys from './keys/index.js';
|
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAA;AACrC,OAAO,KAAK,IAAI,MAAM,iBAAiB,CAAA;AACvC,OAAO,KAAK,IAAI,MAAM,iBAAiB,CAAA;AACvC,OAAO,MAAM,MAAM,aAAa,CAAA;AAChC,OAAO,WAAW,MAAM,mBAAmB,CAAA;AAE3C,OAAO,EAAE,GAAG,EAAE,CAAA;AACd,OAAO,EAAE,IAAI,EAAE,CAAA;AACf,OAAO,EAAE,IAAI,EAAE,CAAA;AACf,OAAO,EAAE,WAAW,EAAE,CAAA;AACtB,OAAO,EAAE,MAAM,EAAE,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAA;AACrC,OAAO,KAAK,IAAI,MAAM,iBAAiB,CAAA;AACvC,OAAO,KAAK,IAAI,MAAM,iBAAiB,CAAA;AACvC,OAAO,MAAM,MAAM,aAAa,CAAA;AAChC,OAAO,WAAW,MAAM,mBAAmB,CAAA;AAE3C,OAAO,EAAE,GAAG,EAAE,CAAA;AACd,OAAO,EAAE,IAAI,EAAE,CAAA;AACf,OAAO,EAAE,IAAI,EAAE,CAAA;AACf,OAAO,EAAE,WAAW,EAAE,CAAA;AACtB,OAAO,EAAE,MAAM,EAAE,CAAA"}
|
package/dist/src/keys/ecdh.d.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
1
|
import type { ECDHKey } from './interface.js';
|
|
2
|
+
/**
|
|
3
|
+
* Generates an ephemeral public key and returns a function that will compute the shared secret key.
|
|
4
|
+
*
|
|
5
|
+
* Focuses only on ECDH now, but can be made more general in the future.
|
|
6
|
+
*/
|
|
2
7
|
export declare function generateEphmeralKeyPair(curve: string): Promise<ECDHKey>;
|
|
3
8
|
//# sourceMappingURL=ecdh.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ecdh.d.ts","sourceRoot":"","sources":["../../../src/keys/ecdh.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAe,MAAM,gBAAgB,CAAA;AAW1D,wBAAsB,uBAAuB,CAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAmB9E"}
|
|
1
|
+
{"version":3,"file":"ecdh.d.ts","sourceRoot":"","sources":["../../../src/keys/ecdh.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAe,MAAM,gBAAgB,CAAA;AAW1D;;;;GAIG;AACH,wBAAsB,uBAAuB,CAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAmB9E"}
|
package/dist/src/keys/ecdh.js
CHANGED
|
@@ -7,6 +7,11 @@ const curves = {
|
|
|
7
7
|
};
|
|
8
8
|
const curveTypes = Object.keys(curves);
|
|
9
9
|
const names = curveTypes.join(' / ');
|
|
10
|
+
/**
|
|
11
|
+
* Generates an ephemeral public key and returns a function that will compute the shared secret key.
|
|
12
|
+
*
|
|
13
|
+
* Focuses only on ECDH now, but can be made more general in the future.
|
|
14
|
+
*/
|
|
10
15
|
export async function generateEphmeralKeyPair(curve) {
|
|
11
16
|
if (curve !== 'P-256' && curve !== 'P-384' && curve !== 'P-521') {
|
|
12
17
|
throw new CodeError(`Unknown curve: ${curve}. Must be ${names}`, 'ERR_INVALID_CURVE');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ecdh.js","sourceRoot":"","sources":["../../../src/keys/ecdh.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAA;AAC3B,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AAGpD,MAAM,MAAM,GAAG;IACb,OAAO,EAAE,YAAY;IACrB,OA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AK,CAAC,CAAA;AAEpC;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAAE,KAAa;IAC1D,IAAI,KAAK,KAAK,OAAO,IAAI,KAAK,KAAK,OAAO,IAAI,KAAK,KAAK,OAAO,EAAE;QAC/D,MAAM,IAAI,SAAS,CAAC,kBAAkB,KAAK,aAAa,KAAK,EAAE,EAAE,mBAAmB,CAAC,CAAA;KACtF;IAED,MAAM,IAAI,GAAG,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAA;IAC7C,IAAI,CAAC,YAAY,EAAE,CAAA;IAEnB,OAAO;QACL,GAAG,EAAE,IAAI,CAAC,YAAY,EAAgB;QAEtC,KAAK,CAAC,YAAY,CAAE,QAAoB,EAAE,YAA0B;YAClE,IAAI,YAAY,IAAI,IAAI,EAAE;gBACxB,IAAI,CAAC,aAAa,CAAC,YAAY,CAAC,OAAO,CAAC,CAAA;aACzC;YAED,OAAO,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAA;QACrC,CAAC;KACF,CAAA;AACH,CAAC"}
|
package/dist/src/keys/index.d.ts
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* **Supported Key Types**
|
|
5
|
+
*
|
|
6
|
+
* The {@link generateKeyPair}, {@link marshalPublicKey}, and {@link marshalPrivateKey} functions accept a string `type` argument.
|
|
7
|
+
*
|
|
8
|
+
* Currently the `'RSA'`, `'ed25519'`, and `secp256k1` types are supported, although ed25519 and secp256k1 keys support only signing and verification of messages.
|
|
9
|
+
*
|
|
10
|
+
* For encryption / decryption support, RSA keys should be used.
|
|
11
|
+
*/
|
|
1
12
|
import 'node-forge/lib/asn1.js';
|
|
2
13
|
import 'node-forge/lib/pbe.js';
|
|
3
14
|
import * as Ed25519 from './ed25519-class.js';
|
|
@@ -16,20 +27,43 @@ export declare const supportedKeys: {
|
|
|
16
27
|
ed25519: typeof Ed25519;
|
|
17
28
|
secp256k1: typeof Secp256k1;
|
|
18
29
|
};
|
|
30
|
+
/**
|
|
31
|
+
* Generates a keypair of the given type and bitsize
|
|
32
|
+
*
|
|
33
|
+
* @param type
|
|
34
|
+
* @param bits - Minimum of 1024
|
|
35
|
+
*/
|
|
19
36
|
export declare function generateKeyPair(type: KeyTypes, bits?: number): Promise<PrivateKey>;
|
|
37
|
+
/**
|
|
38
|
+
* Generates a keypair of the given type and bitsize.
|
|
39
|
+
*
|
|
40
|
+
* Seed is a 32 byte uint8array
|
|
41
|
+
*/
|
|
20
42
|
export declare function generateKeyPairFromSeed(type: KeyTypes, seed: Uint8Array, bits?: number): Promise<PrivateKey>;
|
|
43
|
+
/**
|
|
44
|
+
* Converts a protobuf serialized public key into its representative object
|
|
45
|
+
*/
|
|
21
46
|
export declare function unmarshalPublicKey(buf: Uint8Array): PublicKey;
|
|
47
|
+
/**
|
|
48
|
+
* Converts a public key object into a protobuf serialized public key
|
|
49
|
+
*/
|
|
22
50
|
export declare function marshalPublicKey(key: {
|
|
23
51
|
bytes: Uint8Array;
|
|
24
52
|
}, type?: string): Uint8Array;
|
|
53
|
+
/**
|
|
54
|
+
* Converts a protobuf serialized private key into its representative object
|
|
55
|
+
*/
|
|
25
56
|
export declare function unmarshalPrivateKey(buf: Uint8Array): Promise<PrivateKey>;
|
|
57
|
+
/**
|
|
58
|
+
* Converts a private key object into a protobuf serialized private key
|
|
59
|
+
*/
|
|
26
60
|
export declare function marshalPrivateKey(key: {
|
|
27
61
|
bytes: Uint8Array;
|
|
28
62
|
}, type?: string): Uint8Array;
|
|
29
63
|
/**
|
|
64
|
+
* Converts an exported private key into its representative object.
|
|
30
65
|
*
|
|
31
|
-
*
|
|
32
|
-
* @param {string} password
|
|
66
|
+
* Supported formats are 'pem' (RSA only) and 'libp2p-key'.
|
|
33
67
|
*/
|
|
34
68
|
export declare function importKey(encryptedKey: string, password: string): Promise<PrivateKey>;
|
|
35
69
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/keys/index.ts"],"names":[],"mappings":"AAAA,OAAO,wBAAwB,CAAA;AAC/B,OAAO,uBAAuB,CAAA;AAK9B,OAAO,KAAK,OAAO,MAAM,oBAAoB,CAAA;AAC7C,OAAO,wBAAwB,MAAM,qBAAqB,CAAA;AAE1D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACjD,OAAO,KAAK,OAAO,MAAM,WAAW,CAAA;AACpC,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAA;AACrC,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAA;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAA;AAEnE,OAAO,EAAE,YAAY,EAAE,CAAA;AACvB,OAAO,EAAE,wBAAwB,EAAE,CAAA;AACnC,OAAO,EAAE,OAAO,EAAE,CAAA;AAElB,MAAM,MAAM,QAAQ,GAAG,KAAK,GAAG,SAAS,GAAG,WAAW,CAAA;AAEtD,eAAO,MAAM,aAAa;;;;CAIzB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/keys/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,wBAAwB,CAAA;AAC/B,OAAO,uBAAuB,CAAA;AAK9B,OAAO,KAAK,OAAO,MAAM,oBAAoB,CAAA;AAC7C,OAAO,wBAAwB,MAAM,qBAAqB,CAAA;AAE1D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACjD,OAAO,KAAK,OAAO,MAAM,WAAW,CAAA;AACpC,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAA;AACrC,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAA;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAA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
* The {@link generateKeyPair}, {@link marshalPublicKey}, and {@link marshalPrivateKey} functions accept a string `type` argument.
|
|
7
|
+
*
|
|
8
|
+
* Currently the `'RSA'`, `'ed25519'`, and `secp256k1` types are supported, although ed25519 and secp256k1 keys support only signing and verification of messages.
|
|
9
|
+
*
|
|
10
|
+
* For encryption / decryption support, RSA keys should be used.
|
|
11
|
+
*/
|
|
1
12
|
import 'node-forge/lib/asn1.js';
|
|
2
13
|
import 'node-forge/lib/pbe.js';
|
|
3
14
|
import { CodeError } from '@libp2p/interface/errors';
|
|
@@ -30,20 +41,29 @@ function typeToKey(type) {
|
|
|
30
41
|
}
|
|
31
42
|
throw unsupportedKey(type);
|
|
32
43
|
}
|
|
33
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Generates a keypair of the given type and bitsize
|
|
46
|
+
*
|
|
47
|
+
* @param type
|
|
48
|
+
* @param bits - Minimum of 1024
|
|
49
|
+
*/
|
|
34
50
|
export async function generateKeyPair(type, bits) {
|
|
35
51
|
return typeToKey(type).generateKeyPair(bits ?? 2048);
|
|
36
52
|
}
|
|
37
|
-
|
|
38
|
-
|
|
53
|
+
/**
|
|
54
|
+
* Generates a keypair of the given type and bitsize.
|
|
55
|
+
*
|
|
56
|
+
* Seed is a 32 byte uint8array
|
|
57
|
+
*/
|
|
39
58
|
export async function generateKeyPairFromSeed(type, seed, bits) {
|
|
40
59
|
if (type.toLowerCase() !== 'ed25519') {
|
|
41
60
|
throw new CodeError('Seed key derivation is unimplemented for RSA or secp256k1', 'ERR_UNSUPPORTED_KEY_DERIVATION_TYPE');
|
|
42
61
|
}
|
|
43
62
|
return Ed25519.generateKeyPairFromSeed(seed);
|
|
44
63
|
}
|
|
45
|
-
|
|
46
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Converts a protobuf serialized public key into its representative object
|
|
66
|
+
*/
|
|
47
67
|
export function unmarshalPublicKey(buf) {
|
|
48
68
|
const decoded = keysPBM.PublicKey.decode(buf);
|
|
49
69
|
const data = decoded.Data ?? new Uint8Array();
|
|
@@ -58,14 +78,17 @@ export function unmarshalPublicKey(buf) {
|
|
|
58
78
|
throw unsupportedKey(decoded.Type ?? 'unknown');
|
|
59
79
|
}
|
|
60
80
|
}
|
|
61
|
-
|
|
81
|
+
/**
|
|
82
|
+
* Converts a public key object into a protobuf serialized public key
|
|
83
|
+
*/
|
|
62
84
|
export function marshalPublicKey(key, type) {
|
|
63
85
|
type = (type ?? 'rsa').toLowerCase();
|
|
64
86
|
typeToKey(type); // check type
|
|
65
87
|
return key.bytes;
|
|
66
88
|
}
|
|
67
|
-
|
|
68
|
-
|
|
89
|
+
/**
|
|
90
|
+
* Converts a protobuf serialized private key into its representative object
|
|
91
|
+
*/
|
|
69
92
|
export async function unmarshalPrivateKey(buf) {
|
|
70
93
|
const decoded = keysPBM.PrivateKey.decode(buf);
|
|
71
94
|
const data = decoded.Data ?? new Uint8Array();
|
|
@@ -80,16 +103,18 @@ export async function unmarshalPrivateKey(buf) {
|
|
|
80
103
|
throw unsupportedKey(decoded.Type ?? 'RSA');
|
|
81
104
|
}
|
|
82
105
|
}
|
|
83
|
-
|
|
106
|
+
/**
|
|
107
|
+
* Converts a private key object into a protobuf serialized private key
|
|
108
|
+
*/
|
|
84
109
|
export function marshalPrivateKey(key, type) {
|
|
85
110
|
type = (type ?? 'rsa').toLowerCase();
|
|
86
111
|
typeToKey(type); // check type
|
|
87
112
|
return key.bytes;
|
|
88
113
|
}
|
|
89
114
|
/**
|
|
115
|
+
* Converts an exported private key into its representative object.
|
|
90
116
|
*
|
|
91
|
-
*
|
|
92
|
-
* @param {string} password
|
|
117
|
+
* Supported formats are 'pem' (RSA only) and 'libp2p-key'.
|
|
93
118
|
*/
|
|
94
119
|
export async function importKey(encryptedKey, password) {
|
|
95
120
|
try {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/keys/index.ts"],"names":[],"mappings":"AAAA,OAAO,wBAAwB,CAAA;AAC/B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YAAoB,EAAE,QAAgB;IACrE,IAAI;QACF,MAAM,GAAG,GAAG,MAAM,QAAQ,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAA;QAClD,OAAO,MAAM,mBAAmB,CAAC,GAAG,CAAC,CAAA;KACtC;IAAC,OAAO,CAAC,EAAE;QACV,qCAAqC;KACtC;IAED,kCAAkC;IAClC,MAAM,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC,oBAAoB,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAA;IAClE,IAAI,GAAG,KAAK,IAAI,EAAE;QAChB,MAAM,IAAI,SAAS,CAAC,yEAAyE,EAAE,wBAAwB,CAAC,CAAA;KACzH;IACD,IAAI,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAA;IAC3D,GAAG,GAAG,oBAAoB,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,OAAO,CAAC,CAAA;IACnD,OAAO,aAAa,CAAC,GAAG,CAAC,sBAAsB,CAAC,GAAG,CAAC,CAAA;AACtD,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"random-bytes.d.ts","sourceRoot":"","sources":["../../src/random-bytes.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,OAAO,UAAU,WAAW,CAAE,MAAM,EAAE,MAAM,GAAG,UAAU,CAK/D"}
|
|
1
|
+
{"version":3,"file":"random-bytes.d.ts","sourceRoot":"","sources":["../../src/random-bytes.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,WAAW,CAAE,MAAM,EAAE,MAAM,GAAG,UAAU,CAK/D"}
|
package/dist/src/random-bytes.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface/errors';
|
|
2
2
|
import { randomBytes as randB } from '@noble/hashes/utils';
|
|
3
|
+
/**
|
|
4
|
+
* Generates a Uint8Array with length `number` populated by random bytes
|
|
5
|
+
*/
|
|
3
6
|
export default function randomBytes(length) {
|
|
4
7
|
if (isNaN(length) || length <= 0) {
|
|
5
8
|
throw new CodeError('random bytes length must be a Number bigger than 0', 'ERR_INVALID_LENGTH');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"random-bytes.js","sourceRoot":"","sources":["../../src/random-bytes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAE,WAAW,IAAI,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAE1D,MAAM,CAAC,OAAO,UAAU,WAAW,CAAE,MAAc;IACjD,IAAI,KAAK,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,CAAC,EAAE;QAChC,MAAM,IAAI,SAAS,CAAC,oDAAoD,EAAE,oBAAoB,CAAC,CAAA;KAChG;IACD,OAAO,KAAK,CAAC,MAAM,CAAC,CAAA;AACtB,CAAC"}
|
|
1
|
+
{"version":3,"file":"random-bytes.js","sourceRoot":"","sources":["../../src/random-bytes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAE,WAAW,IAAI,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAE1D;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,WAAW,CAAE,MAAc;IACjD,IAAI,KAAK,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,CAAC,EAAE;QAChC,MAAM,IAAI,SAAS,CAAC,oDAAoD,EAAE,oBAAoB,CAAC,CAAA;KAChG;IACD,OAAO,KAAK,CAAC,MAAM,CAAC,CAAA;AACtB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/crypto",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.7-051154dd",
|
|
4
4
|
"description": "Crypto primitives for libp2p",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/master/packages/crypto#readme",
|
|
@@ -86,7 +86,7 @@
|
|
|
86
86
|
"generate": "protons ./src/keys/keys.proto"
|
|
87
87
|
},
|
|
88
88
|
"dependencies": {
|
|
89
|
-
"@libp2p/interface": "0.1.
|
|
89
|
+
"@libp2p/interface": "0.1.5-051154dd",
|
|
90
90
|
"@noble/curves": "^1.1.0",
|
|
91
91
|
"@noble/hashes": "^1.3.1",
|
|
92
92
|
"multiformats": "^12.0.1",
|
package/src/aes/index.ts
CHANGED
|
@@ -1,3 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* Exposes an interface to AES encryption (formerly Rijndael), as defined in U.S. Federal Information Processing Standards Publication 197.
|
|
5
|
+
*
|
|
6
|
+
* This uses `CTR` mode.
|
|
7
|
+
*
|
|
8
|
+
* /**
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```js
|
|
12
|
+
* import { create } from '@libp2p/crypto/aes'
|
|
13
|
+
*
|
|
14
|
+
* // Setting up Key and IV
|
|
15
|
+
*
|
|
16
|
+
* // A 16 bytes array, 128 Bits, AES-128 is chosen
|
|
17
|
+
* const key128 = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
|
|
18
|
+
*
|
|
19
|
+
* // A 16 bytes array, 128 Bits,
|
|
20
|
+
* const IV = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
|
|
21
|
+
*
|
|
22
|
+
* const decryptedMessage = 'Hello, world!'
|
|
23
|
+
*
|
|
24
|
+
* // Encrypting
|
|
25
|
+
* const cipher = await crypto.aes.create(key128, IV)
|
|
26
|
+
* const encryptedBuffer = await encrypt(Uint8Array.from(decryptedMessage))
|
|
27
|
+
* console.log(encryptedBuffer)
|
|
28
|
+
* // prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>
|
|
29
|
+
*
|
|
30
|
+
* // Decrypting
|
|
31
|
+
* const decipher = await crypto.aes.create(key128, IV)
|
|
32
|
+
* const decryptedBuffer = await decrypt(encryptedBuffer)
|
|
33
|
+
*
|
|
34
|
+
* console.log(decryptedBuffer)
|
|
35
|
+
* // prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>
|
|
36
|
+
*
|
|
37
|
+
* console.log(decryptedBuffer.toString('utf-8'))
|
|
38
|
+
* // prints: Hello, world!
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
|
|
1
42
|
import { cipherMode } from './cipher-mode.js'
|
|
2
43
|
import * as ciphers from './ciphers.js'
|
|
3
44
|
|
|
@@ -6,6 +47,10 @@ export interface AESCipher {
|
|
|
6
47
|
decrypt(data: Uint8Array): Promise<Uint8Array>
|
|
7
48
|
}
|
|
8
49
|
|
|
50
|
+
/**
|
|
51
|
+
* @param key - The key, if length `16` then `AES 128` is used. For length `32`, `AES 256` is used
|
|
52
|
+
* @param iv - Must have length `16`
|
|
53
|
+
*/
|
|
9
54
|
export async function create (key: Uint8Array, iv: Uint8Array): Promise<AESCipher> {
|
|
10
55
|
const mode = cipherMode(key)
|
|
11
56
|
const cipher = ciphers.createCipheriv(mode, key, iv)
|
package/src/hmac/index.ts
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* Exposes an interface to the Keyed-Hash Message Authentication Code (HMAC) as defined in U.S. Federal Information Processing Standards Publication 198. An HMAC is a cryptographic hash that uses a key to sign a message. The receiver verifies the hash by recomputing it using the same key.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```js
|
|
9
|
+
* import { create } from '@libp2p/hmac'
|
|
10
|
+
*
|
|
11
|
+
* const hash = 'SHA1' // 'SHA256' || 'SHA512'
|
|
12
|
+
* const hmac = await crypto.hmac.create(hash, uint8ArrayFromString('secret'))
|
|
13
|
+
* const sig = await hmac.digest(uint8ArrayFromString('hello world'))
|
|
14
|
+
* console.log(sig)
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
|
|
1
18
|
import crypto from 'crypto'
|
|
2
19
|
import lengths from './lengths.js'
|
|
3
20
|
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* The `libp2p-crypto` library depends on the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) in the browser. Web Crypto is available in all modern browsers, however browsers restrict its usage to [Secure Contexts](https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts).
|
|
5
|
+
*
|
|
6
|
+
* *This means you will not be able to use some `@libp2p/crypto` functions in the browser when the page is served over HTTP.*
|
|
7
|
+
*
|
|
8
|
+
* To enable the Web Crypto API and allow `@libp2p/crypto` to work fully, please serve your page over HTTPS.
|
|
9
|
+
*/
|
|
10
|
+
|
|
1
11
|
import * as aes from './aes/index.js'
|
|
2
12
|
import * as hmac from './hmac/index.js'
|
|
3
13
|
import * as keys from './keys/index.js'
|
package/src/keys/ecdh.ts
CHANGED
|
@@ -11,6 +11,11 @@ const curves = {
|
|
|
11
11
|
const curveTypes = Object.keys(curves)
|
|
12
12
|
const names = curveTypes.join(' / ')
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Generates an ephemeral public key and returns a function that will compute the shared secret key.
|
|
16
|
+
*
|
|
17
|
+
* Focuses only on ECDH now, but can be made more general in the future.
|
|
18
|
+
*/
|
|
14
19
|
export async function generateEphmeralKeyPair (curve: string): Promise<ECDHKey> {
|
|
15
20
|
if (curve !== 'P-256' && curve !== 'P-384' && curve !== 'P-521') {
|
|
16
21
|
throw new CodeError(`Unknown curve: ${curve}. Must be ${names}`, 'ERR_INVALID_CURVE')
|
package/src/keys/index.ts
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* **Supported Key Types**
|
|
5
|
+
*
|
|
6
|
+
* The {@link generateKeyPair}, {@link marshalPublicKey}, and {@link marshalPrivateKey} functions accept a string `type` argument.
|
|
7
|
+
*
|
|
8
|
+
* Currently the `'RSA'`, `'ed25519'`, and `secp256k1` types are supported, although ed25519 and secp256k1 keys support only signing and verification of messages.
|
|
9
|
+
*
|
|
10
|
+
* For encryption / decryption support, RSA keys should be used.
|
|
11
|
+
*/
|
|
12
|
+
|
|
1
13
|
import 'node-forge/lib/asn1.js'
|
|
2
14
|
import 'node-forge/lib/pbe.js'
|
|
3
15
|
import { CodeError } from '@libp2p/interface/errors'
|
|
@@ -40,13 +52,21 @@ function typeToKey (type: string): typeof RSA | typeof Ed25519 | typeof Secp256k
|
|
|
40
52
|
throw unsupportedKey(type)
|
|
41
53
|
}
|
|
42
54
|
|
|
43
|
-
|
|
55
|
+
/**
|
|
56
|
+
* Generates a keypair of the given type and bitsize
|
|
57
|
+
*
|
|
58
|
+
* @param type
|
|
59
|
+
* @param bits - Minimum of 1024
|
|
60
|
+
*/
|
|
44
61
|
export async function generateKeyPair (type: KeyTypes, bits?: number): Promise<PrivateKey> {
|
|
45
62
|
return typeToKey(type).generateKeyPair(bits ?? 2048)
|
|
46
63
|
}
|
|
47
64
|
|
|
48
|
-
|
|
49
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Generates a keypair of the given type and bitsize.
|
|
67
|
+
*
|
|
68
|
+
* Seed is a 32 byte uint8array
|
|
69
|
+
*/
|
|
50
70
|
export async function generateKeyPairFromSeed (type: KeyTypes, seed: Uint8Array, bits?: number): Promise<PrivateKey> {
|
|
51
71
|
if (type.toLowerCase() !== 'ed25519') {
|
|
52
72
|
throw new CodeError('Seed key derivation is unimplemented for RSA or secp256k1', 'ERR_UNSUPPORTED_KEY_DERIVATION_TYPE')
|
|
@@ -55,8 +75,9 @@ export async function generateKeyPairFromSeed (type: KeyTypes, seed: Uint8Array,
|
|
|
55
75
|
return Ed25519.generateKeyPairFromSeed(seed)
|
|
56
76
|
}
|
|
57
77
|
|
|
58
|
-
|
|
59
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Converts a protobuf serialized public key into its representative object
|
|
80
|
+
*/
|
|
60
81
|
export function unmarshalPublicKey (buf: Uint8Array): PublicKey {
|
|
61
82
|
const decoded = keysPBM.PublicKey.decode(buf)
|
|
62
83
|
const data = decoded.Data ?? new Uint8Array()
|
|
@@ -73,15 +94,18 @@ export function unmarshalPublicKey (buf: Uint8Array): PublicKey {
|
|
|
73
94
|
}
|
|
74
95
|
}
|
|
75
96
|
|
|
76
|
-
|
|
97
|
+
/**
|
|
98
|
+
* Converts a public key object into a protobuf serialized public key
|
|
99
|
+
*/
|
|
77
100
|
export function marshalPublicKey (key: { bytes: Uint8Array }, type?: string): Uint8Array {
|
|
78
101
|
type = (type ?? 'rsa').toLowerCase()
|
|
79
102
|
typeToKey(type) // check type
|
|
80
103
|
return key.bytes
|
|
81
104
|
}
|
|
82
105
|
|
|
83
|
-
|
|
84
|
-
|
|
106
|
+
/**
|
|
107
|
+
* Converts a protobuf serialized private key into its representative object
|
|
108
|
+
*/
|
|
85
109
|
export async function unmarshalPrivateKey (buf: Uint8Array): Promise<PrivateKey> {
|
|
86
110
|
const decoded = keysPBM.PrivateKey.decode(buf)
|
|
87
111
|
const data = decoded.Data ?? new Uint8Array()
|
|
@@ -98,7 +122,9 @@ export async function unmarshalPrivateKey (buf: Uint8Array): Promise<PrivateKey>
|
|
|
98
122
|
}
|
|
99
123
|
}
|
|
100
124
|
|
|
101
|
-
|
|
125
|
+
/**
|
|
126
|
+
* Converts a private key object into a protobuf serialized private key
|
|
127
|
+
*/
|
|
102
128
|
export function marshalPrivateKey (key: { bytes: Uint8Array }, type?: string): Uint8Array {
|
|
103
129
|
type = (type ?? 'rsa').toLowerCase()
|
|
104
130
|
typeToKey(type) // check type
|
|
@@ -106,9 +132,9 @@ export function marshalPrivateKey (key: { bytes: Uint8Array }, type?: string): U
|
|
|
106
132
|
}
|
|
107
133
|
|
|
108
134
|
/**
|
|
135
|
+
* Converts an exported private key into its representative object.
|
|
109
136
|
*
|
|
110
|
-
*
|
|
111
|
-
* @param {string} password
|
|
137
|
+
* Supported formats are 'pem' (RSA only) and 'libp2p-key'.
|
|
112
138
|
*/
|
|
113
139
|
export async function importKey (encryptedKey: string, password: string): Promise<PrivateKey> {
|
|
114
140
|
try {
|
package/src/random-bytes.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface/errors'
|
|
2
2
|
import { randomBytes as randB } from '@noble/hashes/utils'
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Generates a Uint8Array with length `number` populated by random bytes
|
|
6
|
+
*/
|
|
4
7
|
export default function randomBytes (length: number): Uint8Array {
|
|
5
8
|
if (isNaN(length) || length <= 0) {
|
|
6
9
|
throw new CodeError('random bytes length must be a Number bigger than 0', 'ERR_INVALID_LENGTH')
|