@libp2p/crypto 2.0.5 → 2.0.6-50442d7a

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.
@@ -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":"AAGA,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,wBAAsB,MAAM,CAAE,GAAG,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,GAAG,OAAO,CAAC,SAAS,CAAC,CAgBjF"}
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"}
@@ -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,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
+ /**
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":"AAGA,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"}
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"}
@@ -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"}
@@ -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';
@@ -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';
@@ -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"}
@@ -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"}
@@ -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,OAAO,EAAE,WAAW;IACpB,OAAO,EAAE,WAAW;CACrB,CAAA;AAED,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;AACtC,MAAM,KAAK,GAAG,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;AAEpC,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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AO,CAAC,CAAA;aACzC;YAED,OAAO,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAA;QACrC,CAAC;KACF,CAAA;AACH,CAAC"}
@@ -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
- * @param {string} encryptedKey
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;AAkBD,wBAAsB,eAAe,CAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAEzF;AAID,wBAAsB,uBAAuB,CAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAMnH;AAID,wBAAgB,kBAAkB,CAAE,GAAG,EAAE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CAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAEzF;AAED;;;;GAIG;AACH,wBAAsB,uBAAuB,CAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAMnH;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAE,GAAG,EAAE,UAAU,GAAG,SAAS,CAc9D;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAE,GAAG,EAAE;IAAE,KAAK,EAAE,UAAU,CAAA;CAAE,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,UAAU,CAIvF;AAED;;GAEG;AACH,wBAAsB,mBAAmB,CAAE,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAc/E;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAE,GAAG,EAAE;IAAE,KAAK,EAAE,UAAU,CAAA;CAAE,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,UAAU,CAIxF;AAED;;;;GAIG;AACH,wBAAsB,SAAS,CAAE,YAAY,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAgB5F"}
@@ -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 { CodeError } from '@libp2p/interface/errors';
@@ -30,20 +41,29 @@ function typeToKey(type) {
30
41
  }
31
42
  throw unsupportedKey(type);
32
43
  }
33
- // Generates a keypair of the given type and bitsize
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
- // Generates a keypair of the given type and bitsize
38
- // seed is a 32 byte uint8array
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
- // Converts a protobuf serialized public key into its
46
- // representative object
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
- // Converts a public key object into a protobuf serialized public key
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
- // Converts a protobuf serialized private key into its
68
- // representative object
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
- // Converts a private key object into a protobuf serialized private key
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
- * @param {string} encryptedKey
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,OAAO,uBAAuB,CAAA;AAC9B,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,qCAAqC;AACrC,OAAO,KAAK,MAAM,yBAAyB,CAAA;AAC3C,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,KAAK,OAAO,MAAM,oBAAoB,CAAA;AAC7C,OAAO,wBAAwB,MAAM,qBAAqB,CAAA;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AACxC,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;AAGjD,OAAO,EAAE,YAAY,EAAE,CAAA;AACvB,OAAO,EAAE,wBAAwB,EAAE,CAAA;AACnC,OAAO,EAAE,OAAO,EAAE,CAAA;AAIlB,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,GAAG,EAAE,GAAG;IACR,OAAO,EAAE,OAAO;IAChB,SAAS,EAAE,SAAS;CACrB,CAAA;AAED,SAAS,cAAc,CAAE,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AAO,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,2 +1,5 @@
1
+ /**
2
+ * Generates a Uint8Array with length `number` populated by random bytes
3
+ */
1
4
  export default function randomBytes(length: number): Uint8Array;
2
5
  //# sourceMappingURL=random-bytes.d.ts.map
@@ -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"}
@@ -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.5",
3
+ "version": "2.0.6-50442d7a",
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.3",
89
+ "@libp2p/interface": "0.1.4-50442d7a",
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')