react-native-quick-crypto 1.0.17 → 1.0.18

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 +1 @@
1
- {"version":3,"file":"ed.d.ts","sourceRoot":"","sources":["../../src/ed.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC;AAExD,OAAO,EACL,SAAS,EAIV,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,KAAK,EACV,UAAU,EACV,eAAe,EACf,aAAa,EACb,qBAAqB,EACrB,oBAAoB,EACpB,uBAAuB,EACvB,qBAAqB,EACrB,GAAG,EACH,gBAAgB,EAChB,QAAQ,EACR,eAAe,EAChB,MAAM,SAAS,CAAC;AAWjB,qBAAa,EAAE;IACb,IAAI,EAAE,eAAe,CAAC;IACtB,MAAM,EAAE,gBAAgB,CAAC;IACzB,MAAM,EAAE,SAAS,CAAC;gBAEN,IAAI,EAAE,eAAe,EAAE,MAAM,EAAE,gBAAgB;IAO3D;;;;;;;;;;OAUG;IACH,aAAa,CACX,OAAO,EAAE,oBAAoB,EAC7B,QAAQ,CAAC,EAAE,qBAAqB,GAC/B,MAAM,GAAG,IAAI;IAyBV,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;IAWtC,mBAAmB,IAAI,IAAI;IAW3B,YAAY,IAAI,WAAW;IAI3B,aAAa,IAAI,WAAW;IAI5B;;;;;;;;;OASG;IACH,eAAe,CAAC,UAAU,EAAE,GAAG,EAAE,SAAS,EAAE,GAAG,GAAG,WAAW;IAIvD,IAAI,CAAC,OAAO,EAAE,UAAU,EAAE,GAAG,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,WAAW,CAAC;IAMvE,QAAQ,CAAC,OAAO,EAAE,UAAU,EAAE,GAAG,CAAC,EAAE,UAAU,GAAG,WAAW;IAMtD,MAAM,CACV,SAAS,E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E,uBAAuB,GAAG,SAAS,GAC5C,qBAAqB,GAAG,IAAI,CAyC9B;AAmGD,wBAAsB,2BAA2B,CAC/C,IAAI,EAAE,SAAS,GAAG,OAAO,EACzB,WAAW,EAAE,OAAO,EACpB,SAAS,EAAE,QAAQ,EAAE,GACpB,OAAO,CAAC,aAAa,CAAC,CAuCxB;AAED,wBAAsB,0BAA0B,CAC9C,IAAI,EAAE,QAAQ,GAAG,MAAM,EACvB,WAAW,EAAE,OAAO,EACpB,SAAS,EAAE,QAAQ,EAAE,GACpB,OAAO,CAAC,aAAa,CAAC,CAuCxB;AAED,wBAAgB,WAAW,CACzB,SAAS,EAAE,eAAe,EAC1B,OAAO,EAAE,SAAS,EAClB,MAAM,EAAE,MAAM,GAAG,IAAI,GACpB,WAAW,CAmCb"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-quick-crypto",
3
- "version": "1.0.17",
3
+ "version": "1.0.18",
4
4
  "description": "A fast implementation of Node's `crypto` module written in C/C++ JSI",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",
package/src/ed.ts CHANGED
@@ -58,9 +58,11 @@ export class Ed {
58
58
  options: DiffieHellmanOptions,
59
59
  callback?: DiffieHellmanCallback,
60
60
  ): Buffer | void {
61
- // extract the private and public keys as ArrayBuffers
62
- const privateKey = toAB(options.privateKey);
63
- const publicKey = toAB(options.publicKey);
61
+ // extract raw key bytes from KeyObject instances
62
+ const privKeyObj = options.privateKey as AsymmetricKeyObject;
63
+ const pubKeyObj = options.publicKey as AsymmetricKeyObject;
64
+ const privateKey = privKeyObj.handle.exportKey();
65
+ const publicKey = pubKeyObj.handle.exportKey();
64
66
 
65
67
  try {
66
68
  const ret = this.native.diffieHellman(privateKey, publicKey);
@@ -178,51 +180,97 @@ export function diffieHellman(
178
180
  return ed.diffieHellman(options, callback);
179
181
  }
180
182
 
183
+ function ed_createKeyObjects(ed: Ed): {
184
+ pub: PublicKeyObject;
185
+ priv: PrivateKeyObjectClass;
186
+ } {
187
+ const publicKeyData = ed.getPublicKey();
188
+ const privateKeyData = ed.getPrivateKey();
189
+ const pub = KeyObject.createKeyObject(
190
+ 'public',
191
+ publicKeyData,
192
+ KFormatType.DER,
193
+ KeyEncoding.SPKI,
194
+ ) as PublicKeyObject;
195
+ const priv = KeyObject.createKeyObject(
196
+ 'private',
197
+ privateKeyData,
198
+ KFormatType.DER,
199
+ KeyEncoding.PKCS8,
200
+ ) as PrivateKeyObjectClass;
201
+ return { pub, priv };
202
+ }
203
+
181
204
  // Node API
205
+ function ed_formatKeyPairOutput(
206
+ ed: Ed,
207
+ encoding: KeyPairGenConfig,
208
+ ): {
209
+ publicKey: PublicKeyObject | string | ArrayBuffer;
210
+ privateKey: PrivateKeyObjectClass | string | ArrayBuffer;
211
+ } {
212
+ const { publicFormat, privateFormat, cipher, passphrase } = encoding;
213
+ const { pub, priv } = ed_createKeyObjects(ed);
214
+
215
+ let publicKey: PublicKeyObject | string | ArrayBuffer;
216
+ let privateKey: PrivateKeyObjectClass | string | ArrayBuffer;
217
+
218
+ if (publicFormat == null || publicFormat === -1) {
219
+ publicKey = pub;
220
+ } else {
221
+ const format =
222
+ publicFormat === KFormatType.PEM ? KFormatType.PEM : KFormatType.DER;
223
+ const exported = pub.handle.exportKey(format, KeyEncoding.SPKI);
224
+ if (format === KFormatType.PEM) {
225
+ publicKey = Buffer.from(new Uint8Array(exported)).toString('utf-8');
226
+ } else {
227
+ publicKey = exported;
228
+ }
229
+ }
230
+
231
+ if (privateFormat == null || privateFormat === -1) {
232
+ privateKey = priv;
233
+ } else {
234
+ const format =
235
+ privateFormat === KFormatType.PEM ? KFormatType.PEM : KFormatType.DER;
236
+ const exported = priv.handle.exportKey(
237
+ format,
238
+ KeyEncoding.PKCS8,
239
+ cipher,
240
+ passphrase,
241
+ );
242
+ if (format === KFormatType.PEM) {
243
+ privateKey = Buffer.from(new Uint8Array(exported)).toString('utf-8');
244
+ } else {
245
+ privateKey = exported;
246
+ }
247
+ }
248
+
249
+ return { publicKey, privateKey };
250
+ }
251
+
182
252
  export function ed_generateKeyPair(
183
253
  isAsync: boolean,
184
254
  type: CFRGKeyPairType,
185
255
  encoding: KeyPairGenConfig,
186
256
  callback: GenerateKeyPairCallback | undefined,
187
257
  ): GenerateKeyPairReturn | void {
188
- const ed = new Ed(type, encoding);
189
-
190
- // Helper to convert keys to proper output format
191
- const formatKeys = (): {
192
- publicKey: string | ArrayBuffer;
193
- privateKey: string | ArrayBuffer;
194
- } => {
195
- const publicKeyRaw = ed.getPublicKey();
196
- const privateKeyRaw = ed.getPrivateKey();
197
-
198
- // Check if PEM format was requested (KFormatType.PEM = 1)
199
- const isPemPublic = encoding.publicFormat === KFormatType.PEM;
200
- const isPemPrivate = encoding.privateFormat === KFormatType.PEM;
201
-
202
- // Convert ArrayBuffer to string for PEM format
203
- const arrayBufferToString = (ab: ArrayBuffer): string => {
204
- return Buffer.from(new Uint8Array(ab)).toString('utf-8');
205
- };
206
-
207
- const publicKey = isPemPublic
208
- ? arrayBufferToString(publicKeyRaw)
209
- : publicKeyRaw;
210
- const privateKey = isPemPrivate
211
- ? arrayBufferToString(privateKeyRaw)
212
- : privateKeyRaw;
213
-
214
- return { publicKey, privateKey };
258
+ const derConfig: KeyPairGenConfig = {
259
+ ...encoding,
260
+ publicFormat: KFormatType.DER,
261
+ publicType: KeyEncoding.SPKI,
262
+ privateFormat: KFormatType.DER,
263
+ privateType: KeyEncoding.PKCS8,
215
264
  };
265
+ const ed = new Ed(type, derConfig);
216
266
 
217
- // Async path
218
267
  if (isAsync) {
219
268
  if (!callback) {
220
- // This should not happen if called from public API
221
269
  throw new Error('A callback is required for async key generation.');
222
270
  }
223
271
  ed.generateKeyPair()
224
272
  .then(() => {
225
- const { publicKey, privateKey } = formatKeys();
273
+ const { publicKey, privateKey } = ed_formatKeyPairOutput(ed, encoding);
226
274
  callback(undefined, publicKey, privateKey);
227
275
  })
228
276
  .catch(err => {
@@ -231,7 +279,6 @@ export function ed_generateKeyPair(
231
279
  return;
232
280
  }
233
281
 
234
- // Sync path
235
282
  let err: Error | undefined;
236
283
  try {
237
284
  ed.generateKeyPairSync();
@@ -241,7 +288,7 @@ export function ed_generateKeyPair(
241
288
 
242
289
  const { publicKey, privateKey } = err
243
290
  ? { publicKey: undefined, privateKey: undefined }
244
- : formatKeys();
291
+ : ed_formatKeyPairOutput(ed, encoding);
245
292
 
246
293
  if (callback) {
247
294
  callback(err, publicKey, privateKey);
@@ -374,29 +421,14 @@ export async function ed_generateKeyPairWebCrypto(
374
421
  await ed.generateKeyPair();
375
422
 
376
423
  const algorithmName = type === 'ed25519' ? 'Ed25519' : 'Ed448';
424
+ const { pub, priv } = ed_createKeyObjects(ed);
377
425
 
378
- const publicKeyData = ed.getPublicKey();
379
- const privateKeyData = ed.getPrivateKey();
380
-
381
- const pub = KeyObject.createKeyObject(
382
- 'public',
383
- publicKeyData,
384
- KFormatType.DER,
385
- KeyEncoding.SPKI,
386
- ) as PublicKeyObject;
387
426
  const publicKey = new CryptoKey(
388
427
  pub,
389
428
  { name: algorithmName } as SubtleAlgorithm,
390
429
  publicUsages,
391
430
  true,
392
431
  );
393
-
394
- const priv = KeyObject.createKeyObject(
395
- 'private',
396
- privateKeyData,
397
- KFormatType.DER,
398
- KeyEncoding.PKCS8,
399
- ) as PrivateKeyObjectClass;
400
432
  const privateKey = new CryptoKey(
401
433
  priv,
402
434
  { name: algorithmName } as SubtleAlgorithm,
@@ -434,29 +466,14 @@ export async function x_generateKeyPairWebCrypto(
434
466
  await ed.generateKeyPair();
435
467
 
436
468
  const algorithmName = type === 'x25519' ? 'X25519' : 'X448';
469
+ const { pub, priv } = ed_createKeyObjects(ed);
437
470
 
438
- const publicKeyData = ed.getPublicKey();
439
- const privateKeyData = ed.getPrivateKey();
440
-
441
- const pub = KeyObject.createKeyObject(
442
- 'public',
443
- publicKeyData,
444
- KFormatType.DER,
445
- KeyEncoding.SPKI,
446
- ) as PublicKeyObject;
447
471
  const publicKey = new CryptoKey(
448
472
  pub,
449
473
  { name: algorithmName } as SubtleAlgorithm,
450
474
  publicUsages,
451
475
  true,
452
476
  );
453
-
454
- const priv = KeyObject.createKeyObject(
455
- 'private',
456
- privateKeyData,
457
- KFormatType.DER,
458
- KeyEncoding.PKCS8,
459
- ) as PrivateKeyObjectClass;
460
477
  const privateKey = new CryptoKey(
461
478
  priv,
462
479
  { name: algorithmName } as SubtleAlgorithm,