@libp2p/crypto 3.0.4 → 4.0.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.
Files changed (81) hide show
  1. package/dist/index.min.js +72 -14
  2. package/dist/src/index.d.ts +0 -2
  3. package/dist/src/index.d.ts.map +1 -1
  4. package/dist/src/index.js +0 -2
  5. package/dist/src/index.js.map +1 -1
  6. package/dist/src/keys/ed25519-browser.d.ts +1 -1
  7. package/dist/src/keys/ed25519-browser.d.ts.map +1 -1
  8. package/dist/src/keys/index.d.ts +4 -2
  9. package/dist/src/keys/index.d.ts.map +1 -1
  10. package/dist/src/keys/index.js +7 -12
  11. package/dist/src/keys/index.js.map +1 -1
  12. package/dist/src/keys/rsa-browser.d.ts +0 -2
  13. package/dist/src/keys/rsa-browser.d.ts.map +1 -1
  14. package/dist/src/keys/rsa-browser.js +0 -25
  15. package/dist/src/keys/rsa-browser.js.map +1 -1
  16. package/dist/src/keys/rsa-class.d.ts +6 -5
  17. package/dist/src/keys/rsa-class.d.ts.map +1 -1
  18. package/dist/src/keys/rsa-class.js +11 -25
  19. package/dist/src/keys/rsa-class.js.map +1 -1
  20. package/dist/src/keys/rsa-utils.d.ts +15 -2
  21. package/dist/src/keys/rsa-utils.d.ts.map +1 -1
  22. package/dist/src/keys/rsa-utils.js +304 -39
  23. package/dist/src/keys/rsa-utils.js.map +1 -1
  24. package/dist/src/keys/rsa.d.ts +0 -2
  25. package/dist/src/keys/rsa.d.ts.map +1 -1
  26. package/dist/src/keys/rsa.js +2 -22
  27. package/dist/src/keys/rsa.js.map +1 -1
  28. package/dist/src/pbkdf2.d.ts +1 -1
  29. package/dist/src/pbkdf2.d.ts.map +1 -1
  30. package/dist/src/pbkdf2.js +14 -10
  31. package/dist/src/pbkdf2.js.map +1 -1
  32. package/dist/src/util.d.ts +0 -7
  33. package/dist/src/util.d.ts.map +1 -1
  34. package/dist/src/util.js +0 -25
  35. package/dist/src/util.js.map +1 -1
  36. package/dist/src/webcrypto-browser.d.ts +5 -0
  37. package/dist/src/webcrypto-browser.d.ts.map +1 -0
  38. package/dist/src/webcrypto-browser.js +17 -0
  39. package/dist/src/webcrypto-browser.js.map +1 -0
  40. package/dist/src/webcrypto.d.ts +3 -1
  41. package/dist/src/webcrypto.d.ts.map +1 -1
  42. package/dist/src/webcrypto.js +4 -11
  43. package/dist/src/webcrypto.js.map +1 -1
  44. package/dist/typedoc-urls.json +9 -4
  45. package/package.json +7 -14
  46. package/src/index.ts +0 -2
  47. package/src/keys/ed25519-browser.ts +1 -1
  48. package/src/keys/index.ts +10 -12
  49. package/src/keys/rsa-browser.ts +0 -29
  50. package/src/keys/rsa-class.ts +11 -28
  51. package/src/keys/rsa-utils.ts +373 -39
  52. package/src/keys/rsa.ts +2 -23
  53. package/src/pbkdf2.ts +17 -15
  54. package/src/util.ts +0 -29
  55. package/src/webcrypto-browser.ts +24 -0
  56. package/src/webcrypto.ts +5 -18
  57. package/dist/src/aes/cipher-mode.d.ts +0 -2
  58. package/dist/src/aes/cipher-mode.d.ts.map +0 -1
  59. package/dist/src/aes/cipher-mode.js +0 -13
  60. package/dist/src/aes/cipher-mode.js.map +0 -1
  61. package/dist/src/aes/ciphers-browser.d.ts +0 -7
  62. package/dist/src/aes/ciphers-browser.d.ts.map +0 -1
  63. package/dist/src/aes/ciphers-browser.js +0 -26
  64. package/dist/src/aes/ciphers-browser.js.map +0 -1
  65. package/dist/src/aes/ciphers.d.ts +0 -5
  66. package/dist/src/aes/ciphers.d.ts.map +0 -1
  67. package/dist/src/aes/ciphers.js +0 -4
  68. package/dist/src/aes/ciphers.js.map +0 -1
  69. package/dist/src/aes/index.d.ts +0 -50
  70. package/dist/src/aes/index.d.ts.map +0 -1
  71. package/dist/src/aes/index.js +0 -61
  72. package/dist/src/aes/index.js.map +0 -1
  73. package/dist/src/keys/jwk2pem.d.ts +0 -8
  74. package/dist/src/keys/jwk2pem.d.ts.map +0 -1
  75. package/dist/src/keys/jwk2pem.js +0 -14
  76. package/dist/src/keys/jwk2pem.js.map +0 -1
  77. package/src/aes/cipher-mode.ts +0 -15
  78. package/src/aes/ciphers-browser.ts +0 -31
  79. package/src/aes/ciphers.ts +0 -4
  80. package/src/aes/index.ts +0 -70
  81. package/src/keys/jwk2pem.ts +0 -21
@@ -1,26 +0,0 @@
1
- import 'node-forge/lib/aes.js';
2
- // @ts-expect-error types are missing
3
- import forge from 'node-forge/lib/forge.js';
4
- import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
5
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
6
- export function createCipheriv(mode, key, iv) {
7
- const cipher2 = forge.cipher.createCipher('AES-CTR', uint8ArrayToString(key, 'ascii'));
8
- cipher2.start({ iv: uint8ArrayToString(iv, 'ascii') });
9
- return {
10
- update: (data) => {
11
- cipher2.update(forge.util.createBuffer(uint8ArrayToString(data, 'ascii')));
12
- return uint8ArrayFromString(cipher2.output.getBytes(), 'ascii');
13
- }
14
- };
15
- }
16
- export function createDecipheriv(mode, key, iv) {
17
- const cipher2 = forge.cipher.createDecipher('AES-CTR', uint8ArrayToString(key, 'ascii'));
18
- cipher2.start({ iv: uint8ArrayToString(iv, 'ascii') });
19
- return {
20
- update: (data) => {
21
- cipher2.update(forge.util.createBuffer(uint8ArrayToString(data, 'ascii')));
22
- return uint8ArrayFromString(cipher2.output.getBytes(), 'ascii');
23
- }
24
- };
25
- }
26
- //# sourceMappingURL=ciphers-browser.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ciphers-browser.js","sourceRoot":"","sources":["../../../src/aes/ciphers-browser.ts"],"names":[],"mappings":"AAAA,OAAO,uBAAuB,CAAA;AAC9B,qCAAqC;AACrC,OAAO,KAAK,MAAM,yBAAyB,CAAA;AAC3C,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAMtE,MAAM,UAAU,cAAc,CAAE,IAAS,EAAE,GAAe,EAAE,EAAc;IACxE,MAAM,OAAO,GAAG,KAAK,CAAC,MAAM,CAAC,YAAY,CAAC,SAAS,EAAE,kBAAkB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAA;IACtF,OAAO,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,kBAAkB,CAAC,EAAE,EAAE,OAAO,CAAC,EAAE,CAAC,CAAA;IACtD,OAAO;QACL,MAAM,EAAE,CAAC,IAAgB,EAAE,EAAE;YAC3B,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,kBAAkB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,CAAA;YAC1E,OAAO,oBAAoB,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,OAAO,CAAC,CAAA;QACjE,CAAC;KACF,CAAA;AACH,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAE,IAAS,EAAE,GAAe,EAAE,EAAc;IAC1E,MAAM,OAAO,GAAG,KAAK,CAAC,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,kBAAkB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAA;IACxF,OAAO,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,kBAAkB,CAAC,EAAE,EAAE,OAAO,CAAC,EAAE,CAAC,CAAA;IACtD,OAAO;QACL,MAAM,EAAE,CAAC,IAAgB,EAAE,EAAE;YAC3B,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,kBAAkB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,CAAA;YAC1E,OAAO,oBAAoB,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,OAAO,CAAC,CAAA;QACjE,CAAC;KACF,CAAA;AACH,CAAC"}
@@ -1,5 +0,0 @@
1
- /// <reference types="node" />
2
- import crypto from 'crypto';
3
- export declare const createCipheriv: typeof crypto.createCipheriv;
4
- export declare const createDecipheriv: typeof crypto.createDecipheriv;
5
- //# sourceMappingURL=ciphers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ciphers.d.ts","sourceRoot":"","sources":["../../../src/aes/ciphers.ts"],"names":[],"mappings":";AAAA,OAAO,MAAM,MAAM,QAAQ,CAAA;AAE3B,eAAO,MAAM,cAAc,8BAAwB,CAAA;AACnD,eAAO,MAAM,gBAAgB,gCAA0B,CAAA"}
@@ -1,4 +0,0 @@
1
- import crypto from 'crypto';
2
- export const createCipheriv = crypto.createCipheriv;
3
- export const createDecipheriv = crypto.createDecipheriv;
4
- //# sourceMappingURL=ciphers.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ciphers.js","sourceRoot":"","sources":["../../../src/aes/ciphers.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAA;AAE3B,MAAM,CAAC,MAAM,cAAc,GAAG,MAAM,CAAC,cAAc,CAAA;AACnD,MAAM,CAAC,MAAM,gBAAgB,GAAG,MAAM,CAAC,gBAAgB,CAAA"}
@@ -1,50 +0,0 @@
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
- export interface AESCipher {
42
- encrypt(data: Uint8Array): Promise<Uint8Array>;
43
- decrypt(data: Uint8Array): Promise<Uint8Array>;
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
- */
49
- export declare function create(key: Uint8Array, iv: Uint8Array): AESCipher;
50
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
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,wBAAgB,MAAM,CAAE,GAAG,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,GAAG,SAAS,CAgBlE"}
@@ -1,61 +0,0 @@
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
- import { cipherMode } from './cipher-mode.js';
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
- */
47
- export function create(key, iv) {
48
- const mode = cipherMode(key);
49
- const cipher = ciphers.createCipheriv(mode, key, iv);
50
- const decipher = ciphers.createDecipheriv(mode, key, iv);
51
- const res = {
52
- async encrypt(data) {
53
- return cipher.update(data);
54
- },
55
- async decrypt(data) {
56
- return decipher.update(data);
57
- }
58
- };
59
- return res;
60
- }
61
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
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,UAAU,MAAM,CAAE,GAAe,EAAE,EAAc;IACrD,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,8 +0,0 @@
1
- import 'node-forge/lib/rsa.js';
2
- export interface JWK {
3
- encrypt(msg: string): string;
4
- decrypt(msg: string): string;
5
- }
6
- export declare function jwk2priv(key: JsonWebKey): JWK;
7
- export declare function jwk2pub(key: JsonWebKey): JWK;
8
- //# sourceMappingURL=jwk2pem.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"jwk2pem.d.ts","sourceRoot":"","sources":["../../../src/keys/jwk2pem.ts"],"names":[],"mappings":"AAAA,OAAO,uBAAuB,CAAA;AAK9B,MAAM,WAAW,GAAG;IAClB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;IAC5B,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAC7B;AAMD,wBAAgB,QAAQ,CAAE,GAAG,EAAE,UAAU,GAAG,GAAG,CAE9C;AAED,wBAAgB,OAAO,CAAE,GAAG,EAAE,UAAU,GAAG,GAAG,CAE7C"}
@@ -1,14 +0,0 @@
1
- import 'node-forge/lib/rsa.js';
2
- // @ts-expect-error types are missing
3
- import forge from 'node-forge/lib/forge.js';
4
- import { base64urlToBigInteger } from '../util.js';
5
- function convert(key, types) {
6
- return types.map(t => base64urlToBigInteger(key[t]));
7
- }
8
- export function jwk2priv(key) {
9
- return forge.pki.setRsaPrivateKey(...convert(key, ['n', 'e', 'd', 'p', 'q', 'dp', 'dq', 'qi']));
10
- }
11
- export function jwk2pub(key) {
12
- return forge.pki.setRsaPublicKey(...convert(key, ['n', 'e']));
13
- }
14
- //# sourceMappingURL=jwk2pem.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"jwk2pem.js","sourceRoot":"","sources":["../../../src/keys/jwk2pem.ts"],"names":[],"mappings":"AAAA,OAAO,uBAAuB,CAAA;AAC9B,qCAAqC;AACrC,OAAO,KAAK,MAAM,yBAAyB,CAAA;AAC3C,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAA;AAOlD,SAAS,OAAO,CAAE,GAAQ,EAAE,KAAe;IACzC,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,qBAAqB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AACtD,CAAC;AAED,MAAM,UAAU,QAAQ,CAAE,GAAe;IACvC,OAAO,KAAK,CAAC,GAAG,CAAC,gBAAgB,CAAC,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAA;AACjG,CAAC;AAED,MAAM,UAAU,OAAO,CAAE,GAAe;IACtC,OAAO,KAAK,CAAC,GAAG,CAAC,eAAe,CAAC,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,CAAA;AAC/D,CAAC"}
@@ -1,15 +0,0 @@
1
- import { CodeError } from '@libp2p/interface'
2
-
3
- const CIPHER_MODES = {
4
- 16: 'aes-128-ctr',
5
- 32: 'aes-256-ctr'
6
- }
7
-
8
- export function cipherMode (key: Uint8Array): string {
9
- if (key.length === 16 || key.length === 32) {
10
- return CIPHER_MODES[key.length]
11
- }
12
-
13
- const modes = Object.entries(CIPHER_MODES).map(([k, v]) => `${k} (${v})`).join(' / ')
14
- throw new CodeError(`Invalid key length ${key.length} bytes. Must be ${modes}`, 'ERR_INVALID_KEY_LENGTH')
15
- }
@@ -1,31 +0,0 @@
1
- import 'node-forge/lib/aes.js'
2
- // @ts-expect-error types are missing
3
- import forge from 'node-forge/lib/forge.js'
4
- import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
5
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
6
-
7
- export interface Cipher {
8
- update(data: Uint8Array): Uint8Array
9
- }
10
-
11
- export function createCipheriv (mode: any, key: Uint8Array, iv: Uint8Array): Cipher {
12
- const cipher2 = forge.cipher.createCipher('AES-CTR', uint8ArrayToString(key, 'ascii'))
13
- cipher2.start({ iv: uint8ArrayToString(iv, 'ascii') })
14
- return {
15
- update: (data: Uint8Array) => {
16
- cipher2.update(forge.util.createBuffer(uint8ArrayToString(data, 'ascii')))
17
- return uint8ArrayFromString(cipher2.output.getBytes(), 'ascii')
18
- }
19
- }
20
- }
21
-
22
- export function createDecipheriv (mode: any, key: Uint8Array, iv: Uint8Array): Cipher {
23
- const cipher2 = forge.cipher.createDecipher('AES-CTR', uint8ArrayToString(key, 'ascii'))
24
- cipher2.start({ iv: uint8ArrayToString(iv, 'ascii') })
25
- return {
26
- update: (data: Uint8Array) => {
27
- cipher2.update(forge.util.createBuffer(uint8ArrayToString(data, 'ascii')))
28
- return uint8ArrayFromString(cipher2.output.getBytes(), 'ascii')
29
- }
30
- }
31
- }
@@ -1,4 +0,0 @@
1
- import crypto from 'crypto'
2
-
3
- export const createCipheriv = crypto.createCipheriv
4
- export const createDecipheriv = crypto.createDecipheriv
package/src/aes/index.ts DELETED
@@ -1,70 +0,0 @@
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
-
42
- import { cipherMode } from './cipher-mode.js'
43
- import * as ciphers from './ciphers.js'
44
-
45
- export interface AESCipher {
46
- encrypt(data: Uint8Array): Promise<Uint8Array>
47
- decrypt(data: Uint8Array): Promise<Uint8Array>
48
- }
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
- */
54
- export function create (key: Uint8Array, iv: Uint8Array): AESCipher {
55
- const mode = cipherMode(key)
56
- const cipher = ciphers.createCipheriv(mode, key, iv)
57
- const decipher = ciphers.createDecipheriv(mode, key, iv)
58
-
59
- const res: AESCipher = {
60
- async encrypt (data) {
61
- return cipher.update(data)
62
- },
63
-
64
- async decrypt (data) {
65
- return decipher.update(data)
66
- }
67
- }
68
-
69
- return res
70
- }
@@ -1,21 +0,0 @@
1
- import 'node-forge/lib/rsa.js'
2
- // @ts-expect-error types are missing
3
- import forge from 'node-forge/lib/forge.js'
4
- import { base64urlToBigInteger } from '../util.js'
5
-
6
- export interface JWK {
7
- encrypt(msg: string): string
8
- decrypt(msg: string): string
9
- }
10
-
11
- function convert (key: any, types: string[]): Array<typeof forge.jsbn.BigInteger> {
12
- return types.map(t => base64urlToBigInteger(key[t]))
13
- }
14
-
15
- export function jwk2priv (key: JsonWebKey): JWK {
16
- return forge.pki.setRsaPrivateKey(...convert(key, ['n', 'e', 'd', 'p', 'q', 'dp', 'dq', 'qi']))
17
- }
18
-
19
- export function jwk2pub (key: JsonWebKey): JWK {
20
- return forge.pki.setRsaPublicKey(...convert(key, ['n', 'e']))
21
- }