tiny-essentials 1.1.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +3 -1
  2. package/dist/TinyBasicsEs.js +2784 -0
  3. package/dist/TinyBasicsEs.min.js +2 -0
  4. package/dist/TinyBasicsEs.min.js.LICENSE.txt +8 -0
  5. package/dist/TinyCertCrypto.js +76229 -0
  6. package/dist/TinyCertCrypto.min.js +2 -0
  7. package/dist/TinyCertCrypto.min.js.LICENSE.txt +10 -0
  8. package/dist/TinyCrypto.js +48115 -0
  9. package/dist/TinyCrypto.min.js +2 -0
  10. package/dist/TinyCrypto.min.js.LICENSE.txt +10 -0
  11. package/dist/TinyEssentials.js +77599 -0
  12. package/dist/TinyEssentials.min.js +2 -1
  13. package/dist/TinyEssentials.min.js.LICENSE.txt +10 -0
  14. package/dist/TinyLevelUp.js +173 -0
  15. package/dist/TinyLevelUp.min.js +1 -0
  16. package/dist/v1/basics/index.cjs +27 -0
  17. package/dist/v1/basics/index.d.mts +17 -0
  18. package/dist/v1/basics/index.mjs +7 -0
  19. package/dist/v1/basics/objFilter.cjs +3 -1
  20. package/dist/v1/basics/objFilter.mjs +1 -0
  21. package/dist/v1/build/TinyCertCrypto.cjs +7 -0
  22. package/dist/v1/build/TinyCertCrypto.d.mts +2 -0
  23. package/dist/v1/build/TinyCertCrypto.mjs +2 -0
  24. package/dist/v1/build/TinyCrypto.cjs +7 -0
  25. package/dist/v1/build/TinyCrypto.d.mts +2 -0
  26. package/dist/v1/build/TinyCrypto.mjs +2 -0
  27. package/dist/v1/build/TinyLevelUp.cjs +7 -0
  28. package/dist/v1/build/TinyLevelUp.d.mts +2 -0
  29. package/dist/v1/build/TinyLevelUp.mjs +2 -0
  30. package/dist/v1/index.cjs +2 -0
  31. package/dist/v1/index.d.mts +2 -1
  32. package/dist/v1/index.mjs +2 -1
  33. package/dist/v1/libs/TinyCertCrypto.cjs +514 -0
  34. package/dist/v1/libs/TinyCertCrypto.d.mts +191 -0
  35. package/dist/v1/libs/TinyCertCrypto.mjs +450 -0
  36. package/dist/v1/libs/TinyCrypto.cjs +21 -12
  37. package/dist/v1/libs/TinyCrypto.d.mts +1 -0
  38. package/dist/v1/libs/TinyCrypto.mjs +10 -1
  39. package/docs/README.md +2 -0
  40. package/docs/libs/TinyCertCrypto.md +202 -0
  41. package/package.json +11 -6
  42. package/webpack.config.mjs +69 -0
@@ -0,0 +1,514 @@
1
+ 'use strict';
2
+
3
+ var fs = require('fs');
4
+ var crypto = require('crypto');
5
+ var buffer = require('buffer');
6
+
7
+ /**
8
+ * Determines if the environment is a browser.
9
+ *
10
+ * This constant checks if the code is running in a browser environment by verifying if
11
+ * the `window` object and the `window.document` object are available. It will return `true`
12
+ * if the environment is a browser, and `false` otherwise (e.g., in a Node.js environment).
13
+ *
14
+ * @constant {boolean}
15
+ */
16
+ const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
17
+
18
+ /**
19
+ * Class representing a certificate and key management utility.
20
+ *
21
+ * This class provides functionality to load, initialize, and manage X.509 certificates and RSA keys.
22
+ * It supports encryption and decryption operations using RSA keys, and also includes utility methods
23
+ * for certificate handling and metadata extraction.
24
+ *
25
+ * @class
26
+ */
27
+ class TinyCertCrypto {
28
+ /**
29
+ * Regular expression for matching X.509 PEM certificates.
30
+ *
31
+ * This pattern captures the base64-encoded body of a certificate
32
+ * enclosed between `-----BEGIN CERTIFICATE-----` and `-----END CERTIFICATE-----`.
33
+ * It supports multi-line content.
34
+ *
35
+ * @type {RegExp}
36
+ * @private
37
+ */
38
+ #certRegex = /-----BEGIN CERTIFICATE-----([\s\S]+?)-----END CERTIFICATE-----/;
39
+
40
+ /**
41
+ * Regular expression for matching RSA PEM keys.
42
+ *
43
+ * This pattern captures both RSA public and private keys, supporting
44
+ * the standard PEM formatting:
45
+ * `-----BEGIN [RSA] PUBLIC|PRIVATE KEY-----` to `-----END ... KEY-----`.
46
+ * It captures the key type (PUBLIC|PRIVATE) and the base64 content.
47
+ *
48
+ * @type {RegExp}
49
+ * @private
50
+ */
51
+ #keyRegex =
52
+ /-----BEGIN\s+(?:RSA\s+)?(PUBLIC|PRIVATE)\s+KEY-----([\s\S]+?)-----END\s+(?:RSA\s+)?\1\s+KEY-----/;
53
+
54
+ /**
55
+ * Constructs a new instance of TinyCertCrypto.
56
+ *
57
+ * This class provides cross-platform (Node.js and browser) support
58
+ * for handling X.509 certificates and performing RSA-based encryption and decryption.
59
+ *
60
+ * The constructor accepts optional paths or PEM buffers for the public certificate and private key.
61
+ * If used in a browser environment, either `publicCertPath` or `publicCertBuffer` is required.
62
+ *
63
+ * @param {Object} [options={}] - Initialization options.
64
+ * @param {string|null} [options.publicCertPath=null] - Path or URL to the public certificate in PEM format.
65
+ * @param {string|null} [options.privateKeyPath=null] - Path or URL to the private key in PEM format.
66
+ * @param {string|Buffer|null} [options.publicCertBuffer=null] - The public certificate as a PEM string or Buffer.
67
+ * @param {string|Buffer|null} [options.privateKeyBuffer=null] - The private key as a PEM string or Buffer.
68
+ * @param {string} [options.cryptoType='RSA-OAEP'] - The algorithm identifier used with Crypto API.
69
+ *
70
+ * @throws {Error} If in a browser and neither `publicCertPath` nor `publicCertBuffer` is provided.
71
+ * @throws {TypeError} If provided buffers are not strings or Buffers.
72
+ */
73
+ constructor({
74
+ publicCertPath = null,
75
+ privateKeyPath = null,
76
+ publicCertBuffer = null,
77
+ privateKeyBuffer = null,
78
+ cryptoType = 'RSA-OAEP',
79
+ } = {}) {
80
+ if (!publicCertPath && !publicCertBuffer && isBrowser)
81
+ throw new Error('In browser, publicCertPath or publicCertBuffer must be provided');
82
+
83
+ if (
84
+ publicCertBuffer &&
85
+ typeof publicCertBuffer !== 'string' &&
86
+ !buffer.Buffer.isBuffer(publicCertBuffer)
87
+ )
88
+ throw new TypeError('publicCertBuffer must be a string or Buffer');
89
+
90
+ if (
91
+ privateKeyBuffer &&
92
+ typeof privateKeyBuffer !== 'string' &&
93
+ !buffer.Buffer.isBuffer(privateKeyBuffer)
94
+ )
95
+ throw new TypeError('privateKeyBuffer must be a string or Buffer');
96
+
97
+ this.source = null;
98
+ this.cryptoType = cryptoType;
99
+ this.publicCertPath = publicCertPath;
100
+ this.privateKeyPath = privateKeyPath;
101
+ this.publicCertBuffer = publicCertBuffer;
102
+ this.privateKeyBuffer = privateKeyBuffer;
103
+ this.publicKey = null;
104
+ this.privateKey = null;
105
+ this.publicCert = null;
106
+ this.metadata = null;
107
+ this.forge = null;
108
+ }
109
+
110
+ /**
111
+ * Dynamically imports the `node-forge` module and stores it in the instance.
112
+ * Ensures the module is loaded only once (lazy singleton).
113
+ *
114
+ * This method is private and should not be called directly from outside.
115
+ *
116
+ * @returns {Promise<Object>} The loaded `node-forge` module.
117
+ * @private
118
+ */
119
+ async #fetchNodeForge() {
120
+ if (!this.forge) {
121
+ const forge = await import(/* webpackMode: "eager" */ 'node-forge');
122
+ this.forge = forge.default;
123
+ }
124
+ return this.#getNodeForge();
125
+ }
126
+
127
+ /**
128
+ * Public wrapper for fetching the `node-forge` module.
129
+ * Useful for on-demand loading in environments like browsers.
130
+ *
131
+ * @returns {Promise<Object>} The loaded `node-forge` module.
132
+ */
133
+ async fetchNodeForge() {
134
+ return this.#fetchNodeForge();
135
+ }
136
+
137
+ /**
138
+ * Returns the previously loaded `node-forge` instance.
139
+ * Assumes the module has already been loaded.
140
+ *
141
+ * @returns {Object} The `node-forge` module.
142
+ * @private
143
+ */
144
+ #getNodeForge() {
145
+ return this.forge;
146
+ }
147
+
148
+ /**
149
+ * Public wrapper for accessing the `node-forge` instance.
150
+ *
151
+ * @returns {Object} The `node-forge` module.
152
+ */
153
+ getNodeForge() {
154
+ return this.#getNodeForge();
155
+ }
156
+
157
+ /**
158
+ * Detects the type of a PEM-formatted string.
159
+ *
160
+ * Recognizes X.509 certificates and RSA keys (public/private).
161
+ * Returns a string describing the type, such as `'certificate'`, `'public_key'`, `'private_key'`,
162
+ * or `'unknown'` if the format is not recognized.
163
+ *
164
+ * @param {string} pemString - The PEM string to inspect.
165
+ * @returns {'certificate' | 'public_key' | 'private_key' | 'unknown'} The detected PEM type.
166
+ * @private
167
+ */
168
+ #detectPemType(pemString) {
169
+ if (this.#certRegex.test(pemString)) return 'certificate';
170
+ const keyMatch = this.#keyRegex.exec(pemString);
171
+ if (keyMatch) return keyMatch[1].toLowerCase() + '_key'; // "public_key" or "private_key"
172
+ return 'unknown';
173
+ }
174
+
175
+ /**
176
+ * Generates a new X.509 certificate along with a public/private RSA key pair.
177
+ *
178
+ * This method can only be used in Node.js environments. It throws an error if a certificate
179
+ * or key is already loaded into the instance.
180
+ *
181
+ * @param {Object} subjectFields - An object representing the subject fields of the certificate (e.g., CN, O, C).
182
+ * @param {Object} [options={}] - Optional configuration for key and certificate generation.
183
+ * @param {number} [options.modulusLength=2048] - Length of the RSA key in bits.
184
+ * @param {Object} [options.publicKeyEncoding={ type: 'spki', format: 'pem' }] - Options for public key encoding.
185
+ * @param {Object} [options.privateKeyEncoding={ type: 'pkcs8', format: 'pem' }] - Options for private key encoding.
186
+ * @param {string} [options.publicKeyType] - Overrides `publicKeyEncoding.type` if provided.
187
+ * @param {string} [options.privateKeyType] - Overrides `privateKeyEncoding.type` if provided.
188
+ * @param {number} [options.validityInYears=1] - Number of years the certificate will be valid.
189
+ * @param {number} [options.randomBytesLength=16] - Number of random bytes to use for serial number generation.
190
+ *
191
+ * @returns {Promise<{publicKey: string, privateKey: string, cert: string}>} The generated keys and certificate in PEM format.
192
+ * @throws {Error} If running in a browser or if a cert/key is already loaded.
193
+ */
194
+ async generateX509Cert(subjectFields, options = {}) {
195
+ // Errors
196
+ if (this.publicKey || this.privateKey || this.publicCert)
197
+ throw new Error('A certificate is already loaded into the instance.');
198
+
199
+ // Prepare cert
200
+ const { pki } = await this.#fetchNodeForge();
201
+ const {
202
+ modulusLength = 2048,
203
+ publicKeyEncoding = { type: 'spki', format: 'pem' },
204
+ privateKeyEncoding = { type: 'pkcs8', format: 'pem' },
205
+ validityInYears = 1,
206
+ randomBytesLength = 16,
207
+ } = options;
208
+
209
+ // Value types
210
+ const publicKeyType = options.publicKeyType || publicKeyEncoding.type;
211
+ const privateKeyType = options.privateKeyType || privateKeyEncoding.type;
212
+
213
+ // Validator
214
+ if (isBrowser) throw new Error('generateKeyPair can only be used in Node.js environments');
215
+
216
+ // Generate keys
217
+ const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
218
+ modulusLength,
219
+ publicKeyEncoding: { ...publicKeyEncoding, type: publicKeyType },
220
+ privateKeyEncoding: { ...privateKeyEncoding, type: privateKeyType },
221
+ });
222
+
223
+ // Get pem files
224
+ const { cert, publicPem, privatePem } = this.#generateCertificate(
225
+ subjectFields,
226
+ publicKey,
227
+ privateKey,
228
+ validityInYears,
229
+ randomBytesLength,
230
+ );
231
+
232
+ // Insert data
233
+ this.publicKey = publicPem;
234
+ this.privateKey = privatePem;
235
+
236
+ this.publicCertPath = 'MEMORY';
237
+ this.privateKeyPath = 'MEMORY';
238
+ this.source = 'memory';
239
+ this.publicCertBuffer = publicKey;
240
+ this.privateKeyBuffer = privateKey;
241
+
242
+ this.#loadX509Certificate(cert);
243
+ return { publicKey, privateKey, cert };
244
+ }
245
+
246
+ /**
247
+ * Generates a self-signed X.509 certificate using the given public and private RSA keys.
248
+ *
249
+ * This method creates a certificate, assigns the subject and issuer fields,
250
+ * sets the validity period, and signs it with the private key.
251
+ *
252
+ * @param {Object} subject - An object representing the subject/issuer fields (e.g., { CN: 'example.com' }).
253
+ * @param {string} publicKey - The public key in PEM format.
254
+ * @param {string} privateKey - The private key in PEM format.
255
+ * @param {number} validityInYears - Number of years the certificate will remain valid.
256
+ * @param {number} randomBytesLength - Number of random bytes used to generate the serial number.
257
+ *
258
+ * @returns {Object} An object containing:
259
+ * - {string} cert: The generated certificate in PEM format.
260
+ * - {Object} publicPem: The parsed public key object (from node-forge).
261
+ * - {Object} privatePem: The parsed private key object (from node-forge).
262
+ */
263
+ #generateCertificate(subject, publicKey, privateKey, validityInYears, randomBytesLength) {
264
+ const { pki } = this.forge;
265
+ const cert = pki.createCertificate();
266
+ const publicPem = pki.publicKeyFromPem(publicKey);
267
+ const privatePem = pki.privateKeyFromPem(privateKey);
268
+
269
+ cert.publicKey = publicPem;
270
+ cert.serialNumber = buffer.Buffer.from(crypto.randomBytes(randomBytesLength)).toString('hex');
271
+ cert.validity.notBefore = new Date();
272
+ cert.validity.notAfter = new Date();
273
+ cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + validityInYears);
274
+
275
+ const attrs = [];
276
+ for (const name in subject) attrs.push({ name, value: subject[name] });
277
+
278
+ cert.setSubject(attrs);
279
+ cert.setIssuer(attrs);
280
+ cert.sign(privatePem);
281
+ return { cert: pki.certificateToPem(cert), publicPem, privatePem };
282
+ }
283
+
284
+ /**
285
+ * Initializes the instance by loading the public certificate and, optionally, the private key.
286
+ *
287
+ * This method must be called before using cryptographic operations that depend on a loaded certificate.
288
+ * It supports both Node.js and browser environments.
289
+ *
290
+ * In Node.js:
291
+ * - It loads PEM data from provided file paths or buffers.
292
+ *
293
+ * In browsers:
294
+ * - It loads PEM data from provided URLs or ArrayBuffers.
295
+ *
296
+ * @async
297
+ * @throws {Error} If a certificate is already loaded.
298
+ * @throws {Error} If no public certificate is provided.
299
+ * @throws {Error} If the PEM type cannot be determined or is invalid.
300
+ */
301
+ async init() {
302
+ // Errors
303
+ if (this.publicKey || this.privateKey || this.publicCert)
304
+ throw new Error('A certificate is already loaded into the instance.');
305
+
306
+ if (!this.publicCertPath && !this.publicCertBuffer)
307
+ throw new Error('Public certificate is required to initialize');
308
+
309
+ // Load public key
310
+ this.metadata = {};
311
+ const { pki } = await this.#fetchNodeForge();
312
+ const loadPublicKey = (publicPem) => {
313
+ // File type
314
+ const fileType = this.#detectPemType(publicPem);
315
+
316
+ // Cert
317
+ if (fileType === 'certificate') {
318
+ const cert = pki.certificateFromPem(publicPem);
319
+ this.publicKey = cert.publicKey;
320
+ this.#loadX509Certificate(cert);
321
+ }
322
+
323
+ // Public key
324
+ else if (fileType === 'public_key') this.publicKey = pki.publicKeyFromPem(publicPem);
325
+ else throw new Error('Public key is required to initialize');
326
+ };
327
+
328
+ const loadPrivateKey = (privatePem) => {
329
+ const fileType = this.#detectPemType(privatePem);
330
+ if (fileType === 'private_key') this.privateKey = pki.privateKeyFromPem(privatePem);
331
+ else throw new Error('Private key is required to initialize');
332
+ };
333
+
334
+ // Nodejs
335
+ if (!isBrowser) {
336
+ const usedPublicBuffer = !!this.publicCertBuffer;
337
+ const usedPrivateBuffer = !!this.privateKeyBuffer;
338
+
339
+ // Public key
340
+ const publicPem = usedPublicBuffer
341
+ ? typeof this.publicCertBuffer === 'string'
342
+ ? this.publicCertBuffer
343
+ : this.publicCertBuffer.toString('utf-8')
344
+ : fs.readFileSync(this.publicCertPath, 'utf-8');
345
+ loadPublicKey(publicPem);
346
+
347
+ // Private Key
348
+ if (this.privateKeyPath || this.privateKeyBuffer) {
349
+ const privatePem = usedPrivateBuffer
350
+ ? typeof this.privateKeyBuffer === 'string'
351
+ ? this.privateKeyBuffer
352
+ : this.privateKeyBuffer.toString('utf-8')
353
+ : fs.readFileSync(this.privateKeyPath, 'utf-8');
354
+
355
+ loadPrivateKey(privatePem);
356
+ }
357
+
358
+ // Insert source
359
+ this.source = this.publicCertBuffer || this.privateKeyBuffer ? 'memory' : 'file';
360
+ }
361
+
362
+ // Browser
363
+ else {
364
+ // Public key
365
+ const publicPem = this.publicCertBuffer
366
+ ? typeof this.publicCertBuffer === 'string'
367
+ ? this.publicCertBuffer
368
+ : new TextDecoder().decode(this.publicCertBuffer)
369
+ : await fetch(this.publicCertPath).then((r) => r.text());
370
+ loadPublicKey(publicPem);
371
+
372
+ // Private key
373
+ if (this.privateKeyPath || this.privateKeyBuffer) {
374
+ const privatePem = this.privateKeyBuffer
375
+ ? typeof this.privateKeyBuffer === 'string'
376
+ ? this.privateKeyBuffer
377
+ : new TextDecoder().decode(this.privateKeyBuffer)
378
+ : await fetch(this.privateKeyPath).then((r) => r.text());
379
+ loadPrivateKey(privatePem);
380
+ }
381
+
382
+ // Insert key
383
+ this.source = 'url';
384
+ }
385
+ }
386
+
387
+ /**
388
+ * Parses and stores an X.509 certificate in the instance, extracting metadata such as
389
+ * subject, issuer, serial number, and validity period.
390
+ *
391
+ * This method supports both PEM strings and already-parsed Forge certificate objects.
392
+ * The parsed certificate is saved to `this.publicCert` and its metadata is extracted to `this.metadata`.
393
+ *
394
+ * @private
395
+ * @param {string|object} certPem - A PEM-encoded certificate string or a `forge.pki.Certificate` object.
396
+ * @throws {Error} If the certificate cannot be parsed or processed.
397
+ */
398
+ #loadX509Certificate(certPem) {
399
+ try {
400
+ const { pki } = this.forge;
401
+ const cert = typeof certPem === 'string' ? pki.certificateFromPem(certPem) : certPem;
402
+ this.publicCert = cert;
403
+
404
+ const insertData = (attributes) => {
405
+ const names = {};
406
+ const shortNames = {};
407
+ const raw = attributes.map((attr) => `${attr.shortName}=${attr.value}`).join(',');
408
+ for (const item of attributes) {
409
+ names[item.name] = item.value;
410
+ shortNames[item.shortName] = item.value;
411
+ }
412
+ return { names, shortNames, raw };
413
+ };
414
+
415
+ this.metadata = {
416
+ subject: insertData(cert.subject.attributes),
417
+ issuer: insertData(cert.issuer.attributes),
418
+ serialNumber: cert.serialNumber,
419
+ validFrom: cert.validity.notBefore,
420
+ validTo: cert.validity.notAfter,
421
+ };
422
+ } catch (err) {
423
+ throw new Error('Failed to parse X.509 certificate in browser: ' + err.message);
424
+ }
425
+ }
426
+
427
+ /**
428
+ * Extracts the metadata of the loaded X.509 certificate.
429
+ *
430
+ * Returns an object containing the certificate's metadata such as the subject, issuer,
431
+ * serial number, and validity period. If no certificate is loaded, an empty object is returned.
432
+ *
433
+ * @returns {Object} The metadata of the certificate, or an empty object if no certificate is loaded.
434
+ */
435
+ extractCertMetadata() {
436
+ return this.metadata || {};
437
+ }
438
+
439
+ /**
440
+ * Encrypts a JSON object using the initialized public key.
441
+ *
442
+ * This method serializes the provided JSON object to a string and encrypts it using the
443
+ * public key in PEM format. The encryption is done using the algorithm defined in the
444
+ * `cryptoType` property (e.g., 'RSA-OAEP').
445
+ *
446
+ * @param {Object} jsonObject - The JSON object to be encrypted.
447
+ * @returns {string} The encrypted JSON object, encoded in Base64 format.
448
+ * @throws {Error} If the public key is not initialized (i.e., if `init()` or `generateKeyPair()` has not been called).
449
+ */
450
+ encryptJson(jsonObject) {
451
+ if (!this.publicKey)
452
+ throw new Error('Public key is not initialized. Call init() or generateKeyPair() first.');
453
+ const jsonString = JSON.stringify(jsonObject);
454
+ const encrypted = this.publicKey.encrypt(jsonString, this.cryptoType);
455
+ return this.forge.util.encode64(encrypted);
456
+ }
457
+
458
+ /**
459
+ * Decrypts a Base64-encoded encrypted JSON string using the initialized private key.
460
+ *
461
+ * This method takes the encrypted Base64 string, decodes it, and decrypts it using the
462
+ * private key in PEM format. It then parses the decrypted string back into a JSON object.
463
+ *
464
+ * @param {string} encryptedBase64 - The encrypted JSON string in Base64 format to be decrypted.
465
+ * @returns {Object} The decrypted JSON object.
466
+ * @throws {Error} If the private key is not initialized.
467
+ */
468
+ decryptToJson(encryptedBase64) {
469
+ if (!this.privateKey) throw new Error('Private key is required for decryption');
470
+ const data = this.forge.util.decode64(encryptedBase64);
471
+ const decrypted = this.privateKey.decrypt(data, this.cryptoType);
472
+ return JSON.parse(decrypted);
473
+ }
474
+
475
+ /**
476
+ * Checks if both the public and private keys are initialized.
477
+ *
478
+ * This method verifies if both the public key and private key have been initialized
479
+ * in the instance. It returns `true` if both keys are present, otherwise `false`.
480
+ *
481
+ * @returns {boolean} `true` if both public and private keys are initialized, `false` otherwise.
482
+ */
483
+ hasKeys() {
484
+ return this.publicKey !== null && this.privateKey !== null;
485
+ }
486
+
487
+ /**
488
+ * Checks if a public certificate is initialized.
489
+ *
490
+ * This method checks if the public certificate has been loaded or initialized in the instance.
491
+ * It returns `true` if the public certificate is available, otherwise `false`.
492
+ *
493
+ * @returns {boolean} `true` if the public certificate is initialized, `false` otherwise.
494
+ */
495
+ hasCert() {
496
+ return this.publicCert !== null;
497
+ }
498
+
499
+ /**
500
+ * Resets the instance by clearing the keys and certificate data.
501
+ *
502
+ * This method sets the public and private keys, the public certificate, metadata, and
503
+ * the source to `null`, effectively resetting the instance to its initial state.
504
+ */
505
+ reset() {
506
+ this.publicKey = null;
507
+ this.privateKey = null;
508
+ this.publicCert = null;
509
+ this.metadata = null;
510
+ this.source = null;
511
+ }
512
+ }
513
+
514
+ module.exports = TinyCertCrypto;