@libp2p/crypto 0.0.0 → 0.22.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 (170) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +314 -0
  3. package/dist/src/aes/cipher-mode.d.ts +2 -0
  4. package/dist/src/aes/cipher-mode.d.ts.map +1 -0
  5. package/dist/src/aes/cipher-mode.js +13 -0
  6. package/dist/src/aes/cipher-mode.js.map +1 -0
  7. package/dist/src/aes/ciphers-browser.d.ts +8 -0
  8. package/dist/src/aes/ciphers-browser.d.ts.map +1 -0
  9. package/dist/src/aes/ciphers-browser.js +26 -0
  10. package/dist/src/aes/ciphers-browser.js.map +1 -0
  11. package/dist/src/aes/ciphers.d.ts +5 -0
  12. package/dist/src/aes/ciphers.d.ts.map +1 -0
  13. package/dist/src/aes/ciphers.js +4 -0
  14. package/dist/src/aes/ciphers.js.map +1 -0
  15. package/dist/src/aes/index.d.ts +6 -0
  16. package/dist/src/aes/index.d.ts.map +1 -0
  17. package/dist/src/aes/index.js +17 -0
  18. package/dist/src/aes/index.js.map +1 -0
  19. package/dist/src/ciphers/aes-gcm.browser.d.ts +3 -0
  20. package/dist/src/ciphers/aes-gcm.browser.d.ts.map +1 -0
  21. package/dist/src/ciphers/aes-gcm.browser.js +61 -0
  22. package/dist/src/ciphers/aes-gcm.browser.js.map +1 -0
  23. package/dist/src/ciphers/aes-gcm.d.ts +3 -0
  24. package/dist/src/ciphers/aes-gcm.d.ts.map +1 -0
  25. package/dist/src/ciphers/aes-gcm.js +83 -0
  26. package/dist/src/ciphers/aes-gcm.js.map +1 -0
  27. package/dist/src/ciphers/interface.d.ts +14 -0
  28. package/dist/src/ciphers/interface.d.ts.map +1 -0
  29. package/dist/src/ciphers/interface.js +2 -0
  30. package/dist/src/ciphers/interface.js.map +1 -0
  31. package/dist/src/hmac/index-browser.d.ts +5 -0
  32. package/dist/src/hmac/index-browser.d.ts.map +1 -0
  33. package/dist/src/hmac/index-browser.js +25 -0
  34. package/dist/src/hmac/index-browser.js.map +1 -0
  35. package/dist/src/hmac/index.d.ts +5 -0
  36. package/dist/src/hmac/index.d.ts.map +1 -0
  37. package/dist/src/hmac/index.js +14 -0
  38. package/dist/src/hmac/index.js.map +1 -0
  39. package/dist/src/hmac/lengths.d.ts +7 -0
  40. package/dist/src/hmac/lengths.d.ts.map +1 -0
  41. package/dist/src/hmac/lengths.js +6 -0
  42. package/dist/src/hmac/lengths.js.map +1 -0
  43. package/dist/src/index.d.ts +11 -0
  44. package/dist/src/index.d.ts.map +1 -0
  45. package/dist/src/index.js +11 -0
  46. package/dist/src/index.js.map +1 -0
  47. package/dist/src/keys/ecdh-browser.d.ts +3 -0
  48. package/dist/src/keys/ecdh-browser.d.ts.map +1 -0
  49. package/dist/src/keys/ecdh-browser.js +97 -0
  50. package/dist/src/keys/ecdh-browser.js.map +1 -0
  51. package/dist/src/keys/ecdh.d.ts +3 -0
  52. package/dist/src/keys/ecdh.d.ts.map +1 -0
  53. package/dist/src/keys/ecdh.js +26 -0
  54. package/dist/src/keys/ecdh.js.map +1 -0
  55. package/dist/src/keys/ed25519-class.d.ts +39 -0
  56. package/dist/src/keys/ed25519-class.d.ts.map +1 -0
  57. package/dist/src/keys/ed25519-class.js +119 -0
  58. package/dist/src/keys/ed25519-class.js.map +1 -0
  59. package/dist/src/keys/ed25519.d.ts +18 -0
  60. package/dist/src/keys/ed25519.d.ts.map +1 -0
  61. package/dist/src/keys/ed25519.js +52 -0
  62. package/dist/src/keys/ed25519.js.map +1 -0
  63. package/dist/src/keys/ephemeral-keys.d.ts +9 -0
  64. package/dist/src/keys/ephemeral-keys.d.ts.map +1 -0
  65. package/dist/src/keys/ephemeral-keys.js +9 -0
  66. package/dist/src/keys/ephemeral-keys.js.map +1 -0
  67. package/dist/src/keys/exporter.d.ts +7 -0
  68. package/dist/src/keys/exporter.d.ts.map +1 -0
  69. package/dist/src/keys/exporter.js +13 -0
  70. package/dist/src/keys/exporter.js.map +1 -0
  71. package/dist/src/keys/importer.d.ts +7 -0
  72. package/dist/src/keys/importer.d.ts.map +1 -0
  73. package/dist/src/keys/importer.js +13 -0
  74. package/dist/src/keys/importer.js.map +1 -0
  75. package/dist/src/keys/index.d.ts +33 -0
  76. package/dist/src/keys/index.d.ts.map +1 -0
  77. package/dist/src/keys/index.js +111 -0
  78. package/dist/src/keys/index.js.map +1 -0
  79. package/dist/src/keys/interface.d.ts +17 -0
  80. package/dist/src/keys/interface.d.ts.map +1 -0
  81. package/dist/src/keys/interface.js +2 -0
  82. package/dist/src/keys/interface.js.map +1 -0
  83. package/dist/src/keys/jwk2pem.d.ts +4 -0
  84. package/dist/src/keys/jwk2pem.d.ts.map +1 -0
  85. package/dist/src/keys/jwk2pem.js +14 -0
  86. package/dist/src/keys/jwk2pem.js.map +1 -0
  87. package/dist/src/keys/key-stretcher.d.ts +17 -0
  88. package/dist/src/keys/key-stretcher.d.ts.map +1 -0
  89. package/dist/src/keys/key-stretcher.js +65 -0
  90. package/dist/src/keys/key-stretcher.js.map +1 -0
  91. package/dist/src/keys/keys.d.ts +225 -0
  92. package/dist/src/keys/keys.d.ts.map +1 -0
  93. package/dist/src/keys/keys.js +345 -0
  94. package/dist/src/keys/keys.js.map +1 -0
  95. package/dist/src/keys/rsa-browser.d.ts +17 -0
  96. package/dist/src/keys/rsa-browser.d.ts.map +1 -0
  97. package/dist/src/keys/rsa-browser.js +99 -0
  98. package/dist/src/keys/rsa-browser.js.map +1 -0
  99. package/dist/src/keys/rsa-class.d.ts +42 -0
  100. package/dist/src/keys/rsa-class.d.ts.map +1 -0
  101. package/dist/src/keys/rsa-class.js +126 -0
  102. package/dist/src/keys/rsa-class.js.map +1 -0
  103. package/dist/src/keys/rsa-utils.d.ts +7 -0
  104. package/dist/src/keys/rsa-utils.d.ts.map +1 -0
  105. package/dist/src/keys/rsa-utils.js +65 -0
  106. package/dist/src/keys/rsa-utils.js.map +1 -0
  107. package/dist/src/keys/rsa.d.ts +13 -0
  108. package/dist/src/keys/rsa.d.ts.map +1 -0
  109. package/dist/src/keys/rsa.js +58 -0
  110. package/dist/src/keys/rsa.js.map +1 -0
  111. package/dist/src/keys/secp256k1-class.d.ts +36 -0
  112. package/dist/src/keys/secp256k1-class.d.ts.map +1 -0
  113. package/dist/src/keys/secp256k1-class.js +95 -0
  114. package/dist/src/keys/secp256k1-class.js.map +1 -0
  115. package/dist/src/keys/secp256k1.d.ts +17 -0
  116. package/dist/src/keys/secp256k1.d.ts.map +1 -0
  117. package/dist/src/keys/secp256k1.js +65 -0
  118. package/dist/src/keys/secp256k1.js.map +1 -0
  119. package/dist/src/pbkdf2.d.ts +5 -0
  120. package/dist/src/pbkdf2.d.ts.map +1 -0
  121. package/dist/src/pbkdf2.js +30 -0
  122. package/dist/src/pbkdf2.js.map +1 -0
  123. package/dist/src/random-bytes.d.ts +2 -0
  124. package/dist/src/random-bytes.d.ts.map +1 -0
  125. package/dist/src/random-bytes.js +9 -0
  126. package/dist/src/random-bytes.js.map +1 -0
  127. package/dist/src/util.d.ts +9 -0
  128. package/dist/src/util.d.ts.map +1 -0
  129. package/dist/src/util.js +37 -0
  130. package/dist/src/util.js.map +1 -0
  131. package/dist/src/webcrypto.d.ts +5 -0
  132. package/dist/src/webcrypto.d.ts.map +1 -0
  133. package/dist/src/webcrypto.js +17 -0
  134. package/dist/src/webcrypto.js.map +1 -0
  135. package/package.json +123 -4
  136. package/src/aes/cipher-mode.ts +15 -0
  137. package/src/aes/ciphers-browser.ts +28 -0
  138. package/src/aes/ciphers.ts +4 -0
  139. package/src/aes/index.ts +25 -0
  140. package/src/ciphers/aes-gcm.browser.ts +74 -0
  141. package/src/ciphers/aes-gcm.ts +102 -0
  142. package/src/ciphers/interface.ts +15 -0
  143. package/src/hmac/index-browser.ts +35 -0
  144. package/src/hmac/index.ts +15 -0
  145. package/src/hmac/lengths.ts +6 -0
  146. package/src/index.ts +11 -0
  147. package/src/keys/ecdh-browser.ts +138 -0
  148. package/src/keys/ecdh.ts +33 -0
  149. package/src/keys/ed25519-class.ts +145 -0
  150. package/src/keys/ed25519.ts +63 -0
  151. package/src/keys/ephemeral-keys.ts +9 -0
  152. package/src/keys/exporter.ts +13 -0
  153. package/src/keys/importer.ts +13 -0
  154. package/src/keys/index.ts +126 -0
  155. package/src/keys/interface.ts +20 -0
  156. package/src/keys/jwk2pem.ts +16 -0
  157. package/src/keys/key-stretcher.ts +77 -0
  158. package/src/keys/keys.d.ts +146 -0
  159. package/src/keys/keys.js +366 -0
  160. package/src/keys/keys.proto +15 -0
  161. package/src/keys/rsa-browser.ts +156 -0
  162. package/src/keys/rsa-class.ts +155 -0
  163. package/src/keys/rsa-utils.ts +74 -0
  164. package/src/keys/rsa.ts +69 -0
  165. package/src/keys/secp256k1-class.ts +118 -0
  166. package/src/keys/secp256k1.ts +69 -0
  167. package/src/pbkdf2.ts +39 -0
  168. package/src/random-bytes.ts +9 -0
  169. package/src/util.ts +42 -0
  170. package/src/webcrypto.ts +24 -0
@@ -0,0 +1,366 @@
1
+ // @ts-nocheck
2
+
3
+ /*eslint-disable*/
4
+ import $protobuf from "protobufjs/minimal.js";
5
+
6
+ // Common aliases
7
+ const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
8
+
9
+ // Exported root namespace
10
+ const $root = $protobuf.roots["libp2p-crypto-keys"] || ($protobuf.roots["libp2p-crypto-keys"] = {});
11
+
12
+ /**
13
+ * KeyType enum.
14
+ * @exports KeyType
15
+ * @enum {number}
16
+ * @property {number} RSA=0 RSA value
17
+ * @property {number} Ed25519=1 Ed25519 value
18
+ * @property {number} Secp256k1=2 Secp256k1 value
19
+ */
20
+ export const KeyType = $root.KeyType = (() => {
21
+ const valuesById = {}, values = Object.create(valuesById);
22
+ values[valuesById[0] = "RSA"] = 0;
23
+ values[valuesById[1] = "Ed25519"] = 1;
24
+ values[valuesById[2] = "Secp256k1"] = 2;
25
+ return values;
26
+ })();
27
+
28
+ /**
29
+ * Properties of a PublicKey.
30
+ * @exports IPublicKey
31
+ * @interface IPublicKey
32
+ * @property {KeyType} Type PublicKey Type
33
+ * @property {Uint8Array} Data PublicKey Data
34
+ */
35
+
36
+ /**
37
+ * Constructs a new PublicKey.
38
+ * @exports PublicKey
39
+ * @classdesc Represents a PublicKey.
40
+ * @implements IPublicKey
41
+ * @constructor
42
+ * @param {IPublicKey=} [p] Properties to set
43
+ */
44
+ export function PublicKey(p) {
45
+ if (p)
46
+ for (var ks = Object.keys(p), i = 0; i < ks.length; ++i)
47
+ if (p[ks[i]] != null)
48
+ this[ks[i]] = p[ks[i]];
49
+ }
50
+
51
+ /**
52
+ * PublicKey Type.
53
+ * @member {KeyType} Type
54
+ * @memberof PublicKey
55
+ * @instance
56
+ */
57
+ PublicKey.prototype.Type = 0;
58
+
59
+ /**
60
+ * PublicKey Data.
61
+ * @member {Uint8Array} Data
62
+ * @memberof PublicKey
63
+ * @instance
64
+ */
65
+ PublicKey.prototype.Data = $util.newBuffer([]);
66
+
67
+ /**
68
+ * Encodes the specified PublicKey message. Does not implicitly {@link PublicKey.verify|verify} messages.
69
+ * @function encode
70
+ * @memberof PublicKey
71
+ * @static
72
+ * @param {IPublicKey} m PublicKey message or plain object to encode
73
+ * @param {$protobuf.Writer} [w] Writer to encode to
74
+ * @returns {$protobuf.Writer} Writer
75
+ */
76
+ PublicKey.encode = function encode(m, w) {
77
+ if (!w)
78
+ w = $Writer.create();
79
+ w.uint32(8).int32(m.Type);
80
+ w.uint32(18).bytes(m.Data);
81
+ return w;
82
+ };
83
+
84
+ /**
85
+ * Decodes a PublicKey message from the specified reader or buffer.
86
+ * @function decode
87
+ * @memberof PublicKey
88
+ * @static
89
+ * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from
90
+ * @param {number} [l] Message length if known beforehand
91
+ * @returns {PublicKey} PublicKey
92
+ * @throws {Error} If the payload is not a reader or valid buffer
93
+ * @throws {$protobuf.util.ProtocolError} If required fields are missing
94
+ */
95
+ PublicKey.decode = function decode(r, l) {
96
+ if (!(r instanceof $Reader))
97
+ r = $Reader.create(r);
98
+ var c = l === undefined ? r.len : r.pos + l, m = new PublicKey();
99
+ while (r.pos < c) {
100
+ var t = r.uint32();
101
+ switch (t >>> 3) {
102
+ case 1:
103
+ m.Type = r.int32();
104
+ break;
105
+ case 2:
106
+ m.Data = r.bytes();
107
+ break;
108
+ default:
109
+ r.skipType(t & 7);
110
+ break;
111
+ }
112
+ }
113
+ if (!m.hasOwnProperty("Type"))
114
+ throw $util.ProtocolError("missing required 'Type'", { instance: m });
115
+ if (!m.hasOwnProperty("Data"))
116
+ throw $util.ProtocolError("missing required 'Data'", { instance: m });
117
+ return m;
118
+ };
119
+
120
+ /**
121
+ * Creates a PublicKey message from a plain object. Also converts values to their respective internal types.
122
+ * @function fromObject
123
+ * @memberof PublicKey
124
+ * @static
125
+ * @param {Object.<string,*>} d Plain object
126
+ * @returns {PublicKey} PublicKey
127
+ */
128
+ PublicKey.fromObject = function fromObject(d) {
129
+ if (d instanceof PublicKey)
130
+ return d;
131
+ var m = new PublicKey();
132
+ switch (d.Type) {
133
+ case "RSA":
134
+ case 0:
135
+ m.Type = 0;
136
+ break;
137
+ case "Ed25519":
138
+ case 1:
139
+ m.Type = 1;
140
+ break;
141
+ case "Secp256k1":
142
+ case 2:
143
+ m.Type = 2;
144
+ break;
145
+ }
146
+ if (d.Data != null) {
147
+ if (typeof d.Data === "string")
148
+ $util.base64.decode(d.Data, m.Data = $util.newBuffer($util.base64.length(d.Data)), 0);
149
+ else if (d.Data.length)
150
+ m.Data = d.Data;
151
+ }
152
+ return m;
153
+ };
154
+
155
+ /**
156
+ * Creates a plain object from a PublicKey message. Also converts values to other types if specified.
157
+ * @function toObject
158
+ * @memberof PublicKey
159
+ * @static
160
+ * @param {PublicKey} m PublicKey
161
+ * @param {$protobuf.IConversionOptions} [o] Conversion options
162
+ * @returns {Object.<string,*>} Plain object
163
+ */
164
+ PublicKey.toObject = function toObject(m, o) {
165
+ if (!o)
166
+ o = {};
167
+ var d = {};
168
+ if (o.defaults) {
169
+ d.Type = o.enums === String ? "RSA" : 0;
170
+ if (o.bytes === String)
171
+ d.Data = "";
172
+ else {
173
+ d.Data = [];
174
+ if (o.bytes !== Array)
175
+ d.Data = $util.newBuffer(d.Data);
176
+ }
177
+ }
178
+ if (m.Type != null && m.hasOwnProperty("Type")) {
179
+ d.Type = o.enums === String ? $root.KeyType[m.Type] : m.Type;
180
+ }
181
+ if (m.Data != null && m.hasOwnProperty("Data")) {
182
+ d.Data = o.bytes === String ? $util.base64.encode(m.Data, 0, m.Data.length) : o.bytes === Array ? Array.prototype.slice.call(m.Data) : m.Data;
183
+ }
184
+ return d;
185
+ };
186
+
187
+ /**
188
+ * Converts this PublicKey to JSON.
189
+ * @function toJSON
190
+ * @memberof PublicKey
191
+ * @instance
192
+ * @returns {Object.<string,*>} JSON object
193
+ */
194
+ PublicKey.prototype.toJSON = function toJSON() {
195
+ return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
196
+ };
197
+
198
+ /**
199
+ * Properties of a PrivateKey.
200
+ * @exports IPrivateKey
201
+ * @interface IPrivateKey
202
+ * @property {KeyType} Type PrivateKey Type
203
+ * @property {Uint8Array} Data PrivateKey Data
204
+ */
205
+
206
+ /**
207
+ * Constructs a new PrivateKey.
208
+ * @exports PrivateKey
209
+ * @classdesc Represents a PrivateKey.
210
+ * @implements IPrivateKey
211
+ * @constructor
212
+ * @param {IPrivateKey=} [p] Properties to set
213
+ */
214
+ export function PrivateKey(p) {
215
+ if (p)
216
+ for (var ks = Object.keys(p), i = 0; i < ks.length; ++i)
217
+ if (p[ks[i]] != null)
218
+ this[ks[i]] = p[ks[i]];
219
+ }
220
+
221
+ /**
222
+ * PrivateKey Type.
223
+ * @member {KeyType} Type
224
+ * @memberof PrivateKey
225
+ * @instance
226
+ */
227
+ PrivateKey.prototype.Type = 0;
228
+
229
+ /**
230
+ * PrivateKey Data.
231
+ * @member {Uint8Array} Data
232
+ * @memberof PrivateKey
233
+ * @instance
234
+ */
235
+ PrivateKey.prototype.Data = $util.newBuffer([]);
236
+
237
+ /**
238
+ * Encodes the specified PrivateKey message. Does not implicitly {@link PrivateKey.verify|verify} messages.
239
+ * @function encode
240
+ * @memberof PrivateKey
241
+ * @static
242
+ * @param {IPrivateKey} m PrivateKey message or plain object to encode
243
+ * @param {$protobuf.Writer} [w] Writer to encode to
244
+ * @returns {$protobuf.Writer} Writer
245
+ */
246
+ PrivateKey.encode = function encode(m, w) {
247
+ if (!w)
248
+ w = $Writer.create();
249
+ w.uint32(8).int32(m.Type);
250
+ w.uint32(18).bytes(m.Data);
251
+ return w;
252
+ };
253
+
254
+ /**
255
+ * Decodes a PrivateKey message from the specified reader or buffer.
256
+ * @function decode
257
+ * @memberof PrivateKey
258
+ * @static
259
+ * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from
260
+ * @param {number} [l] Message length if known beforehand
261
+ * @returns {PrivateKey} PrivateKey
262
+ * @throws {Error} If the payload is not a reader or valid buffer
263
+ * @throws {$protobuf.util.ProtocolError} If required fields are missing
264
+ */
265
+ PrivateKey.decode = function decode(r, l) {
266
+ if (!(r instanceof $Reader))
267
+ r = $Reader.create(r);
268
+ var c = l === undefined ? r.len : r.pos + l, m = new PrivateKey();
269
+ while (r.pos < c) {
270
+ var t = r.uint32();
271
+ switch (t >>> 3) {
272
+ case 1:
273
+ m.Type = r.int32();
274
+ break;
275
+ case 2:
276
+ m.Data = r.bytes();
277
+ break;
278
+ default:
279
+ r.skipType(t & 7);
280
+ break;
281
+ }
282
+ }
283
+ if (!m.hasOwnProperty("Type"))
284
+ throw $util.ProtocolError("missing required 'Type'", { instance: m });
285
+ if (!m.hasOwnProperty("Data"))
286
+ throw $util.ProtocolError("missing required 'Data'", { instance: m });
287
+ return m;
288
+ };
289
+
290
+ /**
291
+ * Creates a PrivateKey message from a plain object. Also converts values to their respective internal types.
292
+ * @function fromObject
293
+ * @memberof PrivateKey
294
+ * @static
295
+ * @param {Object.<string,*>} d Plain object
296
+ * @returns {PrivateKey} PrivateKey
297
+ */
298
+ PrivateKey.fromObject = function fromObject(d) {
299
+ if (d instanceof PrivateKey)
300
+ return d;
301
+ var m = new PrivateKey();
302
+ switch (d.Type) {
303
+ case "RSA":
304
+ case 0:
305
+ m.Type = 0;
306
+ break;
307
+ case "Ed25519":
308
+ case 1:
309
+ m.Type = 1;
310
+ break;
311
+ case "Secp256k1":
312
+ case 2:
313
+ m.Type = 2;
314
+ break;
315
+ }
316
+ if (d.Data != null) {
317
+ if (typeof d.Data === "string")
318
+ $util.base64.decode(d.Data, m.Data = $util.newBuffer($util.base64.length(d.Data)), 0);
319
+ else if (d.Data.length)
320
+ m.Data = d.Data;
321
+ }
322
+ return m;
323
+ };
324
+
325
+ /**
326
+ * Creates a plain object from a PrivateKey message. Also converts values to other types if specified.
327
+ * @function toObject
328
+ * @memberof PrivateKey
329
+ * @static
330
+ * @param {PrivateKey} m PrivateKey
331
+ * @param {$protobuf.IConversionOptions} [o] Conversion options
332
+ * @returns {Object.<string,*>} Plain object
333
+ */
334
+ PrivateKey.toObject = function toObject(m, o) {
335
+ if (!o)
336
+ o = {};
337
+ var d = {};
338
+ if (o.defaults) {
339
+ d.Type = o.enums === String ? "RSA" : 0;
340
+ if (o.bytes === String)
341
+ d.Data = "";
342
+ else {
343
+ d.Data = [];
344
+ if (o.bytes !== Array)
345
+ d.Data = $util.newBuffer(d.Data);
346
+ }
347
+ }
348
+ if (m.Type != null && m.hasOwnProperty("Type")) {
349
+ d.Type = o.enums === String ? $root.KeyType[m.Type] : m.Type;
350
+ }
351
+ if (m.Data != null && m.hasOwnProperty("Data")) {
352
+ d.Data = o.bytes === String ? $util.base64.encode(m.Data, 0, m.Data.length) : o.bytes === Array ? Array.prototype.slice.call(m.Data) : m.Data;
353
+ }
354
+ return d;
355
+ };
356
+
357
+ /**
358
+ * Converts this PrivateKey to JSON.
359
+ * @function toJSON
360
+ * @memberof PrivateKey
361
+ * @instance
362
+ * @returns {Object.<string,*>} JSON object
363
+ */
364
+ PrivateKey.prototype.toJSON = function toJSON() {
365
+ return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
366
+ };
@@ -0,0 +1,15 @@
1
+ syntax = "proto3";
2
+
3
+ enum KeyType {
4
+ RSA = 0;
5
+ Ed25519 = 1;
6
+ Secp256k1 = 2;
7
+ }
8
+ message PublicKey {
9
+ required KeyType Type = 1;
10
+ required bytes Data = 2;
11
+ }
12
+ message PrivateKey {
13
+ required KeyType Type = 1;
14
+ required bytes Data = 2;
15
+ }
@@ -0,0 +1,156 @@
1
+ import webcrypto from '../webcrypto.js'
2
+ import randomBytes from '../random-bytes.js'
3
+ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
4
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
5
+ import * as utils from './rsa-utils.js'
6
+ import { jwk2pub, jwk2priv } from './jwk2pem.js'
7
+ import errcode from 'err-code'
8
+
9
+ export { utils }
10
+
11
+ export async function generateKey (bits: number) {
12
+ const pair = await webcrypto.get().subtle.generateKey(
13
+ {
14
+ name: 'RSASSA-PKCS1-v1_5',
15
+ modulusLength: bits,
16
+ publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
17
+ hash: { name: 'SHA-256' }
18
+ },
19
+ true,
20
+ ['sign', 'verify']
21
+ )
22
+
23
+ const keys = await exportKey(pair)
24
+
25
+ return {
26
+ privateKey: keys[0],
27
+ publicKey: keys[1]
28
+ }
29
+ }
30
+
31
+ // Takes a jwk key
32
+ export async function unmarshalPrivateKey (key: JsonWebKey) {
33
+ const privateKey = await webcrypto.get().subtle.importKey(
34
+ 'jwk',
35
+ key,
36
+ {
37
+ name: 'RSASSA-PKCS1-v1_5',
38
+ hash: { name: 'SHA-256' }
39
+ },
40
+ true,
41
+ ['sign']
42
+ )
43
+
44
+ const pair = [
45
+ privateKey,
46
+ await derivePublicFromPrivate(key)
47
+ ]
48
+
49
+ const keys = await exportKey({
50
+ privateKey: pair[0],
51
+ publicKey: pair[1]
52
+ })
53
+
54
+ return {
55
+ privateKey: keys[0],
56
+ publicKey: keys[1]
57
+ }
58
+ }
59
+
60
+ export { randomBytes as getRandomValues }
61
+
62
+ export async function hashAndSign (key: JsonWebKey, msg: Uint8Array) {
63
+ const privateKey = await webcrypto.get().subtle.importKey(
64
+ 'jwk',
65
+ key,
66
+ {
67
+ name: 'RSASSA-PKCS1-v1_5',
68
+ hash: { name: 'SHA-256' }
69
+ },
70
+ false,
71
+ ['sign']
72
+ )
73
+
74
+ const sig = await webcrypto.get().subtle.sign(
75
+ { name: 'RSASSA-PKCS1-v1_5' },
76
+ privateKey,
77
+ Uint8Array.from(msg)
78
+ )
79
+
80
+ return new Uint8Array(sig, 0, sig.byteLength)
81
+ }
82
+
83
+ export async function hashAndVerify (key: JsonWebKey, sig: Uint8Array, msg: Uint8Array) {
84
+ const publicKey = await webcrypto.get().subtle.importKey(
85
+ 'jwk',
86
+ key,
87
+ {
88
+ name: 'RSASSA-PKCS1-v1_5',
89
+ hash: { name: 'SHA-256' }
90
+ },
91
+ false,
92
+ ['verify']
93
+ )
94
+
95
+ return await webcrypto.get().subtle.verify(
96
+ { name: 'RSASSA-PKCS1-v1_5' },
97
+ publicKey,
98
+ sig,
99
+ msg
100
+ )
101
+ }
102
+
103
+ async function exportKey (pair: CryptoKeyPair) {
104
+ if (pair.privateKey == null || pair.publicKey == null) {
105
+ throw errcode(new Error('Private and public key are required'), 'ERR_INVALID_PARAMETERS')
106
+ }
107
+
108
+ return await Promise.all([
109
+ webcrypto.get().subtle.exportKey('jwk', pair.privateKey),
110
+ webcrypto.get().subtle.exportKey('jwk', pair.publicKey)
111
+ ])
112
+ }
113
+
114
+ async function derivePublicFromPrivate (jwKey: JsonWebKey) {
115
+ return await webcrypto.get().subtle.importKey(
116
+ 'jwk',
117
+ {
118
+ kty: jwKey.kty,
119
+ n: jwKey.n,
120
+ e: jwKey.e
121
+ },
122
+ {
123
+ name: 'RSASSA-PKCS1-v1_5',
124
+ hash: { name: 'SHA-256' }
125
+ },
126
+ true,
127
+ ['verify']
128
+ )
129
+ }
130
+
131
+ /*
132
+
133
+ RSA encryption/decryption for the browser with webcrypto workaround
134
+ "bloody dark magic. webcrypto's why."
135
+
136
+ Explanation:
137
+ - Convert JWK to nodeForge
138
+ - Convert msg Uint8Array to nodeForge buffer: ByteBuffer is a "binary-string backed buffer", so let's make our Uint8Array a binary string
139
+ - Convert resulting nodeForge buffer to Uint8Array: it returns a binary string, turn that into a Uint8Array
140
+
141
+ */
142
+
143
+ function convertKey (key: JsonWebKey, pub: boolean, msg: Uint8Array, handle: (msg: string, key: { encrypt: (msg: string) => string, decrypt: (msg: string) => string}) => string) {
144
+ const fkey = pub ? jwk2pub(key) : jwk2priv(key)
145
+ const fmsg = uint8ArrayToString(Uint8Array.from(msg), 'ascii')
146
+ const fomsg = handle(fmsg, fkey)
147
+ return uint8ArrayFromString(fomsg, 'ascii')
148
+ }
149
+
150
+ export function encrypt (key: JsonWebKey, msg: Uint8Array) {
151
+ return convertKey(key, true, msg, (msg, key) => key.encrypt(msg))
152
+ }
153
+
154
+ export function decrypt (key: JsonWebKey, msg: Uint8Array) {
155
+ return convertKey(key, false, msg, (msg, key) => key.decrypt(msg))
156
+ }
@@ -0,0 +1,155 @@
1
+
2
+ import { sha256 } from 'multiformats/hashes/sha2'
3
+ import errcode from 'err-code'
4
+ import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
5
+ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
6
+ import 'node-forge/lib/sha512.js'
7
+ // @ts-expect-error types are missing
8
+ import forge from 'node-forge/lib/forge.js'
9
+ import * as crypto from './rsa.js'
10
+ import * as pbm from './keys.js'
11
+ import { exporter } from './exporter.js'
12
+
13
+ export class RsaPublicKey {
14
+ private readonly _key: JsonWebKey
15
+
16
+ constructor (key: JsonWebKey) {
17
+ this._key = key
18
+ }
19
+
20
+ async verify (data: Uint8Array, sig: Uint8Array) { // eslint-disable-line require-await
21
+ return await crypto.hashAndVerify(this._key, sig, data)
22
+ }
23
+
24
+ marshal () {
25
+ return crypto.utils.jwkToPkix(this._key)
26
+ }
27
+
28
+ get bytes () {
29
+ return pbm.PublicKey.encode({
30
+ Type: pbm.KeyType.RSA,
31
+ Data: this.marshal()
32
+ }).finish()
33
+ }
34
+
35
+ encrypt (bytes: Uint8Array) {
36
+ return crypto.encrypt(this._key, bytes)
37
+ }
38
+
39
+ equals (key: RsaPublicKey) {
40
+ return uint8ArrayEquals(this.bytes, key.bytes)
41
+ }
42
+
43
+ async hash () {
44
+ const { bytes } = await sha256.digest(this.bytes)
45
+
46
+ return bytes
47
+ }
48
+ }
49
+
50
+ export class RsaPrivateKey {
51
+ private readonly _key: JsonWebKey
52
+ private readonly _publicKey: JsonWebKey
53
+
54
+ constructor (key: JsonWebKey, publicKey: JsonWebKey) {
55
+ this._key = key
56
+ this._publicKey = publicKey
57
+ }
58
+
59
+ genSecret () {
60
+ return crypto.getRandomValues(16)
61
+ }
62
+
63
+ async sign (message: Uint8Array) { // eslint-disable-line require-await
64
+ return await crypto.hashAndSign(this._key, message)
65
+ }
66
+
67
+ get public () {
68
+ if (this._publicKey == null) {
69
+ throw errcode(new Error('public key not provided'), 'ERR_PUBKEY_NOT_PROVIDED')
70
+ }
71
+
72
+ return new RsaPublicKey(this._publicKey)
73
+ }
74
+
75
+ decrypt (bytes: Uint8Array) {
76
+ return crypto.decrypt(this._key, bytes)
77
+ }
78
+
79
+ marshal () {
80
+ return crypto.utils.jwkToPkcs1(this._key)
81
+ }
82
+
83
+ get bytes () {
84
+ return pbm.PrivateKey.encode({
85
+ Type: pbm.KeyType.RSA,
86
+ Data: this.marshal()
87
+ }).finish()
88
+ }
89
+
90
+ equals (key: RsaPrivateKey) {
91
+ return uint8ArrayEquals(this.bytes, key.bytes)
92
+ }
93
+
94
+ async hash () {
95
+ const { bytes } = await sha256.digest(this.bytes)
96
+
97
+ return bytes
98
+ }
99
+
100
+ /**
101
+ * Gets the ID of the key.
102
+ *
103
+ * The key id is the base58 encoding of the SHA-256 multihash of its public key.
104
+ * The public key is a protobuf encoding containing a type and the DER encoding
105
+ * of the PKCS SubjectPublicKeyInfo.
106
+ */
107
+ async id () {
108
+ const hash = await this.public.hash()
109
+ return uint8ArrayToString(hash, 'base58btc')
110
+ }
111
+
112
+ /**
113
+ * Exports the key into a password protected PEM format
114
+ */
115
+ async export (password: string, format = 'pkcs-8') { // eslint-disable-line require-await
116
+ if (format === 'pkcs-8') {
117
+ const buffer = new forge.util.ByteBuffer(this.marshal())
118
+ const asn1 = forge.asn1.fromDer(buffer)
119
+ const privateKey = forge.pki.privateKeyFromAsn1(asn1)
120
+
121
+ const options = {
122
+ algorithm: 'aes256',
123
+ count: 10000,
124
+ saltSize: 128 / 8,
125
+ prfAlgorithm: 'sha512'
126
+ }
127
+ return forge.pki.encryptRsaPrivateKey(privateKey, password, options)
128
+ } else if (format === 'libp2p-key') {
129
+ return await exporter(this.bytes, password)
130
+ } else {
131
+ throw errcode(new Error(`export format '${format}' is not supported`), 'ERR_INVALID_EXPORT_FORMAT')
132
+ }
133
+ }
134
+ }
135
+
136
+ export async function unmarshalRsaPrivateKey (bytes: Uint8Array) {
137
+ const jwk = crypto.utils.pkcs1ToJwk(bytes)
138
+ const keys = await crypto.unmarshalPrivateKey(jwk)
139
+ return new RsaPrivateKey(keys.privateKey, keys.publicKey)
140
+ }
141
+
142
+ export function unmarshalRsaPublicKey (bytes: Uint8Array) {
143
+ const jwk = crypto.utils.pkixToJwk(bytes)
144
+ return new RsaPublicKey(jwk)
145
+ }
146
+
147
+ export async function fromJwk (jwk: JsonWebKey) {
148
+ const keys = await crypto.unmarshalPrivateKey(jwk)
149
+ return new RsaPrivateKey(keys.privateKey, keys.publicKey)
150
+ }
151
+
152
+ export async function generateKeyPair (bits: number) {
153
+ const keys = await crypto.generateKey(bits)
154
+ return new RsaPrivateKey(keys.privateKey, keys.publicKey)
155
+ }