react-native-quick-crypto 0.2.0 → 0.3.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 (69) hide show
  1. package/README.md +23 -6
  2. package/cpp/Cipher/MGLCipherHostObject.cpp +64 -48
  3. package/cpp/Cipher/MGLCipherKeys.cpp +1469 -0
  4. package/cpp/Cipher/MGLCipherKeys.h +124 -0
  5. package/cpp/Cipher/MGLCreateCipherInstaller.cpp +56 -53
  6. package/cpp/Cipher/MGLCreateCipherInstaller.h +5 -0
  7. package/cpp/Cipher/MGLCreateDecipherInstaller.cpp +56 -53
  8. package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +107 -0
  9. package/cpp/Cipher/MGLGenerateKeyPairInstaller.h +32 -0
  10. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +60 -0
  11. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.h +35 -0
  12. package/cpp/Cipher/MGLPublicCipher.h +120 -0
  13. package/cpp/Cipher/MGLPublicCipherInstaller.h +113 -0
  14. package/cpp/Cipher/MGLRsa.cpp +188 -0
  15. package/cpp/Cipher/MGLRsa.h +61 -0
  16. package/cpp/JSIUtils/MGLJSIUtils.h +24 -0
  17. package/cpp/JSIUtils/MGLThreadAwareHostObject.h +1 -1
  18. package/cpp/MGLQuickCryptoHostObject.cpp +42 -3
  19. package/cpp/Utils/MGLUtils.cpp +156 -0
  20. package/cpp/Utils/MGLUtils.h +254 -0
  21. package/lib/commonjs/Cipher.js +307 -0
  22. package/lib/commonjs/Cipher.js.map +1 -1
  23. package/lib/commonjs/NativeQuickCrypto/Cipher.js +11 -0
  24. package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
  25. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  26. package/lib/commonjs/QuickCrypto.js +8 -0
  27. package/lib/commonjs/QuickCrypto.js.map +1 -1
  28. package/lib/commonjs/Utils.js +82 -1
  29. package/lib/commonjs/Utils.js.map +1 -1
  30. package/lib/commonjs/constants.js +86 -0
  31. package/lib/commonjs/constants.js.map +1 -0
  32. package/lib/commonjs/index.js +5 -0
  33. package/lib/commonjs/index.js.map +1 -1
  34. package/lib/commonjs/keys.js +207 -0
  35. package/lib/commonjs/keys.js.map +1 -0
  36. package/lib/module/Cipher.js +296 -3
  37. package/lib/module/Cipher.js.map +1 -1
  38. package/lib/module/NativeQuickCrypto/Cipher.js +9 -1
  39. package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
  40. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  41. package/lib/module/QuickCrypto.js +8 -1
  42. package/lib/module/QuickCrypto.js.map +1 -1
  43. package/lib/module/Utils.js +67 -1
  44. package/lib/module/Utils.js.map +1 -1
  45. package/lib/module/constants.js +79 -0
  46. package/lib/module/constants.js.map +1 -0
  47. package/lib/module/index.js +2 -0
  48. package/lib/module/index.js.map +1 -1
  49. package/lib/module/keys.js +193 -0
  50. package/lib/module/keys.js.map +1 -0
  51. package/lib/typescript/Cipher.d.ts +58 -1
  52. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +10 -0
  53. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +6 -1
  54. package/lib/typescript/QuickCrypto.d.ts +105 -1
  55. package/lib/typescript/Utils.d.ts +11 -0
  56. package/lib/typescript/constants.d.ts +75 -0
  57. package/lib/typescript/index.d.ts +2 -0
  58. package/lib/typescript/keys.d.ts +60 -0
  59. package/package.json +5 -5
  60. package/react-native-quick-crypto.podspec +1 -1
  61. package/src/.DS_Store +0 -0
  62. package/src/Cipher.ts +444 -3
  63. package/src/NativeQuickCrypto/Cipher.ts +44 -0
  64. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +13 -1
  65. package/src/QuickCrypto.ts +12 -0
  66. package/src/Utils.ts +91 -0
  67. package/src/constants.ts +79 -0
  68. package/src/index.ts +4 -0
  69. package/src/keys.ts +297 -0
@@ -3,8 +3,8 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
3
3
  /* eslint-disable no-dupe-class-members */
4
4
  import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';
5
5
  import Stream from 'stream';
6
- import { binaryLikeToArrayBuffer, getDefaultEncoding } from './Utils';
7
- // TODO(osp) re-enable type specific constructors
6
+ import { binaryLikeToArrayBuffer, getDefaultEncoding, kEmptyObject, validateFunction, validateObject, validateString, validateUint32, validateInt32 } from './Utils';
7
+ import { RSAKeyVariant } from './NativeQuickCrypto/Cipher'; // TODO(osp) re-enable type specific constructors
8
8
  // They are nice to have but not absolutely necessary
9
9
  // import type {
10
10
  // CipherCCMOptions,
@@ -13,14 +13,23 @@ import { binaryLikeToArrayBuffer, getDefaultEncoding } from './Utils';
13
13
  // CipherGCMOptions,
14
14
  // // CipherKey,
15
15
  // // KeyObject,
16
- // // TODO @Szymon20000 This types seem to be missing? Where did you get this definitions from?
16
+ // // TODO(Szymon) This types seem to be missing? Where did you get this definitions from?
17
17
  // // CipherOCBTypes,
18
18
  // // CipherOCBOptions,
19
19
  // } from 'crypto'; // Node crypto typings
20
+
20
21
  import { StringDecoder } from 'string_decoder';
22
+ import { Buffer } from '@craftzdog/react-native-buffer';
21
23
  import { Buffer as SBuffer } from 'safe-buffer';
24
+ import { constants } from './constants';
25
+ import { parsePrivateKeyEncoding, parsePublicKeyEncoding, preparePrivateKey, preparePublicOrPrivateKey } from './keys'; // make sure that nextTick is there
26
+
27
+ global.process.nextTick = setImmediate;
22
28
  const createInternalCipher = NativeQuickCrypto.createCipher;
23
29
  const createInternalDecipher = NativeQuickCrypto.createDecipher;
30
+ const _publicEncrypt = NativeQuickCrypto.publicEncrypt;
31
+ const _publicDecrypt = NativeQuickCrypto.publicDecrypt;
32
+ const _privateDecrypt = NativeQuickCrypto.privateDecrypt;
24
33
 
25
34
  function getUIntOption(options, key) {
26
35
  let value;
@@ -283,5 +292,289 @@ export function createCipher(algorithm, password, options) {
283
292
 
284
293
  export function createCipheriv(algorithm, key, iv, options) {
285
294
  return new Cipher(algorithm, key, options, iv);
295
+ } // RSA Functions
296
+ // Follows closely the model implemented in node
297
+ // TODO(osp) types...
298
+
299
+ function rsaFunctionFor(method, defaultPadding, keyType) {
300
+ return (options, buffer) => {
301
+ const {
302
+ format,
303
+ type,
304
+ data,
305
+ passphrase
306
+ } = keyType === 'private' ? preparePrivateKey(options) : preparePublicOrPrivateKey(options);
307
+ const padding = options.padding || defaultPadding;
308
+ const {
309
+ oaepHash,
310
+ encoding
311
+ } = options;
312
+ let {
313
+ oaepLabel
314
+ } = options;
315
+ if (oaepHash !== undefined) validateString(oaepHash, 'key.oaepHash');
316
+ if (oaepLabel !== undefined) oaepLabel = binaryLikeToArrayBuffer(oaepLabel, encoding);
317
+ buffer = binaryLikeToArrayBuffer(buffer, encoding);
318
+ const rawRes = method(data, format, type, passphrase, buffer, padding, oaepHash, oaepLabel);
319
+ return Buffer.from(rawRes);
320
+ };
321
+ }
322
+
323
+ export const publicEncrypt = rsaFunctionFor(_publicEncrypt, constants.RSA_PKCS1_OAEP_PADDING, 'public');
324
+ export const publicDecrypt = rsaFunctionFor(_publicDecrypt, constants.RSA_PKCS1_PADDING, 'public'); // const privateEncrypt = rsaFunctionFor(_privateEncrypt, constants.RSA_PKCS1_PADDING,
325
+ // 'private');
326
+
327
+ export const privateDecrypt = rsaFunctionFor(_privateDecrypt, constants.RSA_PKCS1_OAEP_PADDING, 'private'); // _ _ __ _____ _
328
+ // | | | |/ / | __ \ (_)
329
+ // __ _ ___ _ __ ___ _ __ __ _| |_ ___| ' / ___ _ _| |__) |_ _ _ _ __
330
+ // / _` |/ _ \ '_ \ / _ \ '__/ _` | __/ _ \ < / _ \ | | | ___/ _` | | '__|
331
+ // | (_| | __/ | | | __/ | | (_| | || __/ . \ __/ |_| | | | (_| | | |
332
+ // \__, |\___|_| |_|\___|_| \__,_|\__\___|_|\_\___|\__, |_| \__,_|_|_|
333
+ // __/ | __/ |
334
+ // |___/ |___/
335
+
336
+ function parseKeyEncoding(keyType) {
337
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : kEmptyObject;
338
+ const {
339
+ publicKeyEncoding,
340
+ privateKeyEncoding
341
+ } = options;
342
+ let publicFormat, publicType;
343
+
344
+ if (publicKeyEncoding == null) {
345
+ publicFormat = publicType = undefined;
346
+ } else if (typeof publicKeyEncoding === 'object') {
347
+ ({
348
+ format: publicFormat,
349
+ type: publicType
350
+ } = parsePublicKeyEncoding(publicKeyEncoding, keyType, 'publicKeyEncoding'));
351
+ } else {
352
+ throw new Error('Invalid argument options.publicKeyEncoding', publicKeyEncoding);
353
+ }
354
+
355
+ let privateFormat, privateType, cipher, passphrase;
356
+
357
+ if (privateKeyEncoding == null) {
358
+ privateFormat = privateType = undefined;
359
+ } else if (typeof privateKeyEncoding === 'object') {
360
+ ({
361
+ format: privateFormat,
362
+ type: privateType,
363
+ cipher,
364
+ passphrase
365
+ } = parsePrivateKeyEncoding(privateKeyEncoding, keyType, 'privateKeyEncoding'));
366
+ } else {
367
+ throw new Error('Invalid argument options.privateKeyEncoding', publicKeyEncoding);
368
+ }
369
+
370
+ return [publicFormat, publicType, privateFormat, privateType, cipher, passphrase];
371
+ }
372
+
373
+ function internalGenerateKeyPair(isAsync, type, options, callback) {
374
+ // On node a very complex "job" chain is created, we are going for a far simpler approach and calling
375
+ // an internal function that basically executes the same byte shuffling on the native side
376
+ const encoding = parseKeyEncoding(type, options); // if (options !== undefined)
377
+ // validateObject(options, 'options');
378
+
379
+ switch (type) {
380
+ case 'rsa-pss':
381
+ case 'rsa':
382
+ {
383
+ validateObject(options, 'options');
384
+ const {
385
+ modulusLength
386
+ } = options;
387
+ validateUint32(modulusLength, 'options.modulusLength');
388
+ let {
389
+ publicExponent
390
+ } = options;
391
+
392
+ if (publicExponent == null) {
393
+ publicExponent = 0x10001;
394
+ } else {
395
+ validateUint32(publicExponent, 'options.publicExponent');
396
+ }
397
+
398
+ if (type === 'rsa') {
399
+ if (isAsync) {
400
+ NativeQuickCrypto.generateKeyPair(RSAKeyVariant.kKeyVariantRSA_SSA_PKCS1_v1_5, modulusLength, publicExponent, ...encoding).then(_ref => {
401
+ let [err, publicKey, privateKey] = _ref;
402
+
403
+ if (typeof publicKey === 'object') {
404
+ publicKey = Buffer.from(publicKey);
405
+ }
406
+
407
+ if (typeof privateKey === 'object') {
408
+ privateKey = Buffer.from(privateKey);
409
+ }
410
+
411
+ callback === null || callback === void 0 ? void 0 : callback(err, publicKey, privateKey);
412
+ }).catch(err => {
413
+ callback === null || callback === void 0 ? void 0 : callback(err, undefined, undefined);
414
+ });
415
+ return;
416
+ } else {
417
+ let [err, publicKey, privateKey] = NativeQuickCrypto.generateKeyPairSync(RSAKeyVariant.kKeyVariantRSA_SSA_PKCS1_v1_5, modulusLength, publicExponent, ...encoding);
418
+
419
+ if (typeof publicKey === 'object') {
420
+ publicKey = Buffer.from(publicKey);
421
+ }
422
+
423
+ if (typeof privateKey === 'object') {
424
+ privateKey = Buffer.from(privateKey);
425
+ }
426
+
427
+ return [err, publicKey, privateKey];
428
+ }
429
+ }
430
+
431
+ const {
432
+ hash,
433
+ mgf1Hash,
434
+ hashAlgorithm,
435
+ mgf1HashAlgorithm,
436
+ saltLength
437
+ } = options; // // We don't have a process object on RN
438
+ // // const pendingDeprecation = getOptionValue('--pending-deprecation');
439
+
440
+ if (saltLength !== undefined) validateInt32(saltLength, 'options.saltLength', 0);
441
+ if (hashAlgorithm !== undefined) validateString(hashAlgorithm, 'options.hashAlgorithm');
442
+ if (mgf1HashAlgorithm !== undefined) validateString(mgf1HashAlgorithm, 'options.mgf1HashAlgorithm');
443
+
444
+ if (hash !== undefined) {
445
+ // pendingDeprecation && process.emitWarning(
446
+ // '"options.hash" is deprecated, ' +
447
+ // 'use "options.hashAlgorithm" instead.',
448
+ // 'DeprecationWarning',
449
+ // 'DEP0154');
450
+ validateString(hash, 'options.hash');
451
+
452
+ if (hashAlgorithm && hash !== hashAlgorithm) {
453
+ throw new Error(`Invalid Argument options.hash ${hash}`);
454
+ }
455
+ }
456
+
457
+ if (mgf1Hash !== undefined) {
458
+ // pendingDeprecation && process.emitWarning(
459
+ // '"options.mgf1Hash" is deprecated, ' +
460
+ // 'use "options.mgf1HashAlgorithm" instead.',
461
+ // 'DeprecationWarning',
462
+ // 'DEP0154');
463
+ validateString(mgf1Hash, 'options.mgf1Hash');
464
+
465
+ if (mgf1HashAlgorithm && mgf1Hash !== mgf1HashAlgorithm) {
466
+ throw new Error(`Invalid Argument options.mgf1Hash ${mgf1Hash}`);
467
+ }
468
+ }
469
+
470
+ return NativeQuickCrypto.generateKeyPairSync(RSAKeyVariant.kKeyVariantRSA_PSS, modulusLength, publicExponent, hashAlgorithm || hash, mgf1HashAlgorithm || mgf1Hash, saltLength, ...encoding);
471
+ }
472
+ // case 'dsa': {
473
+ // validateObject(options, 'options');
474
+ // const { modulusLength } = options!;
475
+ // validateUint32(modulusLength, 'options.modulusLength');
476
+ // let { divisorLength } = options!;
477
+ // if (divisorLength == null) {
478
+ // divisorLength = -1;
479
+ // } else validateInt32(divisorLength, 'options.divisorLength', 0);
480
+ // // return new DsaKeyPairGenJob(
481
+ // // mode,
482
+ // // modulusLength,
483
+ // // divisorLength,
484
+ // // ...encoding);
485
+ // }
486
+ // case 'ec': {
487
+ // validateObject(options, 'options');
488
+ // const { namedCurve } = options!;
489
+ // validateString(namedCurve, 'options.namedCurve');
490
+ // let { paramEncoding } = options!;
491
+ // if (paramEncoding == null || paramEncoding === 'named')
492
+ // paramEncoding = OPENSSL_EC_NAMED_CURVE;
493
+ // else if (paramEncoding === 'explicit')
494
+ // paramEncoding = OPENSSL_EC_EXPLICIT_CURVE;
495
+ // else
496
+ // throw new Error(`Invalid Argument options.paramEncoding ${paramEncoding}`);
497
+ // // throw new ERR_INVALID_ARG_VALUE('options.paramEncoding', paramEncoding);
498
+ // // return new EcKeyPairGenJob(mode, namedCurve, paramEncoding, ...encoding);
499
+ // }
500
+ // case 'ed25519':
501
+ // case 'ed448':
502
+ // case 'x25519':
503
+ // case 'x448': {
504
+ // let id;
505
+ // switch (type) {
506
+ // case 'ed25519':
507
+ // id = EVP_PKEY_ED25519;
508
+ // break;
509
+ // case 'ed448':
510
+ // id = EVP_PKEY_ED448;
511
+ // break;
512
+ // case 'x25519':
513
+ // id = EVP_PKEY_X25519;
514
+ // break;
515
+ // case 'x448':
516
+ // id = EVP_PKEY_X448;
517
+ // break;
518
+ // }
519
+ // return new NidKeyPairGenJob(mode, id, ...encoding);
520
+ // }
521
+ // case 'dh': {
522
+ // validateObject(options, 'options');
523
+ // const { group, primeLength, prime, generator } = options;
524
+ // if (group != null) {
525
+ // if (prime != null)
526
+ // throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'prime');
527
+ // if (primeLength != null)
528
+ // throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'primeLength');
529
+ // if (generator != null)
530
+ // throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'generator');
531
+ // validateString(group, 'options.group');
532
+ // return new DhKeyPairGenJob(mode, group, ...encoding);
533
+ // }
534
+ // if (prime != null) {
535
+ // if (primeLength != null)
536
+ // throw new ERR_INCOMPATIBLE_OPTION_PAIR('prime', 'primeLength');
537
+ // validateBuffer(prime, 'options.prime');
538
+ // } else if (primeLength != null) {
539
+ // validateInt32(primeLength, 'options.primeLength', 0);
540
+ // } else {
541
+ // throw new ERR_MISSING_OPTION(
542
+ // 'At least one of the group, prime, or primeLength options'
543
+ // );
544
+ // }
545
+ // if (generator != null) {
546
+ // validateInt32(generator, 'options.generator', 0);
547
+ // }
548
+ // return new DhKeyPairGenJob(
549
+ // mode,
550
+ // prime != null ? prime : primeLength,
551
+ // generator == null ? 2 : generator,
552
+ // ...encoding
553
+ // );
554
+ // }
555
+
556
+ default: // Fall through
557
+
558
+ }
559
+
560
+ throw new Error(`Invalid Argument options: ${type} scheme not supported. Currently not all encryption methods are supported in quick-crypto!`);
561
+ } // TODO(osp) put correct types (e.g. type -> 'rsa', etc..)
562
+
563
+
564
+ export function generateKeyPair(type, options, callback) {
565
+ if (typeof options === 'function') {
566
+ callback = options;
567
+ options = undefined;
568
+ }
569
+
570
+ validateFunction(callback);
571
+ internalGenerateKeyPair(true, type, options, callback);
572
+ }
573
+ export function generateKeyPairSync(type, options) {
574
+ const [_, publicKey, privateKey] = internalGenerateKeyPair(false, type, options, undefined);
575
+ return {
576
+ publicKey,
577
+ privateKey
578
+ };
286
579
  }
287
580
  //# sourceMappingURL=Cipher.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["Cipher.ts"],"names":["NativeQuickCrypto","Stream","binaryLikeToArrayBuffer","getDefaultEncoding","StringDecoder","Buffer","SBuffer","createInternalCipher","createCipher","createInternalDecipher","createDecipher","getUIntOption","options","key","value","Error","normalizeEncoding","enc","retried","toLowerCase","validateEncoding","data","encoding","normalizedEncoding","length","getDecoder","decoder","CipherCommon","Transform","constructor","cipherType","cipherKey","isCipher","iv","cipherKeyBuffer","authTagLength","args","cipher_type","cipher_key","auth_tag_len","internal","update","inputEncoding","outputEncoding","defaultEncoding","ArrayBuffer","isView","ret","write","from","final","end","_transform","chunk","callback","push","_flush","setAutoPadding","autoPadding","setAAD","buffer","plaintextLength","setAuthTag","tag","Cipher","Decipher","algorithm","password","createDecipheriv","createCipheriv"],"mappings":";;AAAA;AACA,SAASA,iBAAT,QAAkC,uCAAlC;AACA,OAAOC,MAAP,MAAmB,QAAnB;AACA,SAEEC,uBAFF,EAKEC,kBALF,QAMO,SANP;AAQA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASC,aAAT,QAA8B,gBAA9B;AAEA,SAASC,MAAM,IAAIC,OAAnB,QAAkC,aAAlC;AAEA,MAAMC,oBAAoB,GAAGP,iBAAiB,CAACQ,YAA/C;AACA,MAAMC,sBAAsB,GAAGT,iBAAiB,CAACU,cAAjD;;AAEA,SAASC,aAAT,CAAuBC,OAAvB,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C;AACD;;AAZiC,C,CAepC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,OAAO,SAASvB,cAAT,CACLwD,SADK,EAELC,QAFK,EAGLvD,OAHK,EAIK;AACV,SAAO,IAAIqD,QAAJ,CAAaC,SAAb,EAAwBC,QAAxB,EAAkCvD,OAAlC,CAAP;AACD,C,CAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASwD,gBAAT,CACLF,SADK,EAELrD,GAFK,EAGLoB,EAHK,EAILrB,OAJK,EAKK;AACV,SAAO,IAAIqD,QAAJ,CAAaC,SAAb,EAAwBrD,GAAxB,EAA6BD,OAA7B,EAAsCqB,EAAtC,CAAP;AACD,C,CAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASzB,YAAT,CACL0D,SADK,EAELC,QAFK,EAGLvD,OAHK,EAIG;AACR,SAAO,IAAIoD,MAAJ,CAAWE,SAAX,EAAsBC,QAAtB,EAAgCvD,OAAhC,CAAP;AACD,C,CAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASyD,cAAT,CACLH,SADK,EAELrD,GAFK,EAGLoB,EAHK,EAILrB,OAJK,EAKG;AACR,SAAO,IAAIoD,MAAJ,CAAWE,SAAX,EAAsBrD,GAAtB,EAA2BD,OAA3B,EAAoCqB,EAApC,CAAP;AACD","sourcesContent":["/* eslint-disable no-dupe-class-members */\nimport { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';\nimport Stream from 'stream';\nimport {\n BinaryLike,\n binaryLikeToArrayBuffer,\n CipherEncoding,\n Encoding,\n getDefaultEncoding,\n} from './Utils';\nimport type { InternalCipher } from './NativeQuickCrypto/Cipher';\n// TODO(osp) re-enable type specific constructors\n// They are nice to have but not absolutely necessary\n// import type {\n// CipherCCMOptions,\n// CipherCCMTypes,\n// CipherGCMTypes,\n// CipherGCMOptions,\n// // CipherKey,\n// // KeyObject,\n// // TODO @Szymon20000 This types seem to be missing? Where did you get this definitions from?\n// // CipherOCBTypes,\n// // CipherOCBOptions,\n// } from 'crypto'; // Node crypto typings\nimport { StringDecoder } from 'string_decoder';\nimport type { Buffer } from '@craftzdog/react-native-buffer';\nimport { Buffer as SBuffer } from 'safe-buffer';\n\nconst createInternalCipher = NativeQuickCrypto.createCipher;\nconst createInternalDecipher = NativeQuickCrypto.createDecipher;\n\nfunction getUIntOption(options: Record<string, any>, key: string) {\n let value;\n if (options && (value = options[key]) != null) {\n // >>> Turns any type into a positive integer (also sets the sign bit to 0)\n // eslint-disable-next-line no-bitwise\n if (value >>> 0 !== value) throw new Error(`options.${key}: ${value}`);\n return value;\n }\n return -1;\n}\n\nfunction normalizeEncoding(enc: string) {\n if (!enc) return 'utf8';\n var retried;\n while (true) {\n switch (enc) {\n case 'utf8':\n case 'utf-8':\n return 'utf8';\n case 'ucs2':\n case 'ucs-2':\n case 'utf16le':\n case 'utf-16le':\n return 'utf16le';\n case 'latin1':\n case 'binary':\n return 'latin1';\n case 'base64':\n case 'ascii':\n case 'hex':\n return enc;\n default:\n if (retried) return; // undefined\n enc = ('' + enc).toLowerCase();\n retried = true;\n }\n }\n}\n\nfunction validateEncoding(data: string, encoding: string) {\n const normalizedEncoding = normalizeEncoding(encoding);\n const length = data.length;\n\n if (normalizedEncoding === 'hex' && length % 2 !== 0) {\n throw new Error(`Encoding ${encoding} not valid for data length ${length}`);\n }\n}\n\nfunction getDecoder(decoder?: StringDecoder, encoding?: BufferEncoding) {\n return decoder ?? new StringDecoder(encoding);\n}\n\nclass CipherCommon extends Stream.Transform {\n private internal: InternalCipher;\n private decoder: StringDecoder | undefined;\n\n constructor(\n cipherType: string,\n cipherKey: BinaryLike,\n isCipher: boolean,\n options: Record<string, any> = {},\n iv?: BinaryLike | null\n ) {\n super(options);\n const cipherKeyBuffer = binaryLikeToArrayBuffer(cipherKey);\n // TODO(osp) This might not be smart, check again after release\n const authTagLength = getUIntOption(options, 'authTagLength');\n const args = {\n cipher_type: cipherType,\n cipher_key: cipherKeyBuffer,\n iv,\n ...options,\n auth_tag_len: authTagLength,\n };\n this.internal = isCipher\n ? createInternalCipher(args)\n : createInternalDecipher(args);\n }\n\n update(\n data: BinaryLike,\n inputEncoding?: CipherEncoding,\n outputEncoding?: CipherEncoding\n ): ArrayBuffer | string {\n const defaultEncoding = getDefaultEncoding();\n inputEncoding = inputEncoding ?? defaultEncoding;\n outputEncoding = outputEncoding ?? defaultEncoding;\n\n if (typeof data === 'string') {\n validateEncoding(data, inputEncoding);\n } else if (!ArrayBuffer.isView(data)) {\n throw new Error('Invalid data argument');\n }\n\n if (typeof data === 'string') {\n // On node this is handled on the native side\n // on our case we need to correctly send the arraybuffer to the jsi side\n inputEncoding = inputEncoding === 'buffer' ? 'utf8' : inputEncoding;\n data = binaryLikeToArrayBuffer(data, inputEncoding);\n } else {\n data = binaryLikeToArrayBuffer(data as any, inputEncoding);\n }\n\n const ret = this.internal.update(data);\n\n if (outputEncoding && outputEncoding !== 'buffer') {\n this.decoder = getDecoder(this.decoder, outputEncoding);\n\n return this.decoder!.write(SBuffer.from(ret) as any);\n }\n\n return ret;\n }\n\n final(): ArrayBuffer;\n final(outputEncoding: BufferEncoding | 'buffer'): string;\n final(outputEncoding?: BufferEncoding | 'buffer'): ArrayBuffer | string {\n const ret = this.internal.final();\n\n if (outputEncoding && outputEncoding !== 'buffer') {\n this.decoder = getDecoder(this.decoder, outputEncoding);\n\n return this.decoder!.end(SBuffer.from(ret) as any);\n }\n\n return ret;\n }\n\n _transform(chunk: BinaryLike, encoding: Encoding, callback: () => void) {\n this.push(this.update(chunk, encoding));\n callback();\n }\n\n _flush(callback: () => void) {\n this.push(this.final());\n callback();\n }\n\n public setAutoPadding(autoPadding?: boolean): this {\n this.internal.setAutoPadding(!!autoPadding);\n return this;\n }\n\n public setAAD(\n buffer: Buffer,\n options?: {\n plaintextLength: number;\n }\n ): this {\n this.internal.setAAD({\n data: buffer.buffer,\n plaintextLength: options?.plaintextLength,\n });\n return this;\n }\n\n // protected getAuthTag(): Buffer {\n // return Buffer.from(this.internal.getAuthTag());\n // }\n\n public setAuthTag(tag: Buffer): this {\n this.internal.setAuthTag(tag.buffer);\n return this;\n }\n}\n\nclass Cipher extends CipherCommon {\n constructor(\n cipherType: string,\n cipherKey: BinaryLike,\n options: Record<string, any> = {},\n iv?: BinaryLike | null\n ) {\n if (iv != null) {\n iv = binaryLikeToArrayBuffer(iv);\n }\n super(cipherType, cipherKey, true, options, iv);\n }\n}\n\nclass Decipher extends CipherCommon {\n constructor(\n cipherType: string,\n cipherKey: BinaryLike,\n options: Record<string, any> = {},\n iv?: BinaryLike | null\n ) {\n if (iv != null) {\n iv = binaryLikeToArrayBuffer(iv);\n }\n\n super(cipherType, cipherKey, false, options, iv);\n }\n}\n\n// TODO(osp) This definitions cause typescript errors when using the API\n// export function createDecipher(\n// algorithm: CipherCCMTypes,\n// password: BinaryLike,\n// options: CipherCCMOptions\n// ): Decipher;\n// export function createDecipher(\n// algorithm: CipherGCMTypes,\n// password: BinaryLike,\n// options?: CipherGCMOptions\n// ): Decipher;\nexport function createDecipher(\n algorithm: string,\n password: BinaryLike,\n options?: Stream.TransformOptions\n): Decipher {\n return new Decipher(algorithm, password, options);\n}\n\n// TODO(osp) This definitions cause typescript errors when using the API\n// export function createDecipheriv(\n// algorithm: CipherCCMTypes,\n// key: BinaryLike,\n// iv: BinaryLike,\n// options: CipherCCMOptions\n// ): Decipher;\n// export function createDecipheriv(\n// algorithm: CipherOCBTypes,\n// key: BinaryLike,\n// iv: BinaryLike,\n// options: CipherOCBOptions\n// ): DecipherOCB;\n// export function createDecipheriv(\n// algorithm: CipherGCMTypes,\n// key: BinaryLike,\n// iv: BinaryLike,\n// options?: CipherGCMOptions\n// ): Decipher;\nexport function createDecipheriv(\n algorithm: string,\n key: BinaryLike,\n iv: BinaryLike | null,\n options?: Stream.TransformOptions\n): Decipher {\n return new Decipher(algorithm, key, options, iv);\n}\n\n// TODO(osp) This definitions cause typescript errors when using the API\n// commenting them out for now\n// export function createCipher(\n// algorithm: CipherCCMTypes,\n// password: BinaryLike,\n// options: CipherCCMOptions\n// ): Cipher;\n// export function createCipher(\n// algorithm: CipherGCMTypes,\n// password: BinaryLike,\n// options?: CipherGCMOptions\n// ): Cipher;\nexport function createCipher(\n algorithm: string,\n password: BinaryLike,\n options?: Stream.TransformOptions\n): Cipher {\n return new Cipher(algorithm, password, options);\n}\n\n// TODO(osp) on all the createCipheriv methods, node seems to use a \"KeyObject\" is seems to be a thread safe\n// object that creates keys and what not. Not sure if we should support it.\n// Fow now I replaced all of them to BinaryLike\n// export function createCipheriv(\n// algorithm: CipherCCMTypes,\n// key: BinaryLike,\n// iv: BinaryLike,\n// options: CipherCCMOptions\n// ): Cipher;\n// export function createCipheriv(\n// algorithm: CipherOCBTypes,\n// key: BinaryLike,\n// iv: BinaryLike,\n// options: CipherOCBOptions\n// ): CipherOCB;\n// export function createCipheriv(\n// algorithm: CipherGCMTypes,\n// key: BinaryLike,\n// iv: BinaryLike,\n// options?: CipherGCMOptions\n// ): Cipher;\nexport function createCipheriv(\n algorithm: string,\n key: BinaryLike,\n iv: BinaryLike | null,\n options?: Stream.TransformOptions\n): Cipher {\n return new Cipher(algorithm, key, options, iv);\n}\n"]}
1
+ {"version":3,"sources":["Cipher.ts"],"names":["NativeQuickCrypto","Stream","binaryLikeToArrayBuffer","getDefaultEncoding","kEmptyObject","validateFunction","validateObject","validateString","validateUint32","validateInt32","RSAKeyVariant","StringDecoder","Buffer","SBuffer","constants","parsePrivateKeyEncoding","parsePublicKeyEncoding","preparePrivateKey","preparePublicOrPrivateKey","global","process","nextTick","setImmediate","createInternalCipher","createCipher","createInternalDecipher","createDecipher","_publicEncrypt","publicEncrypt","_publicDecrypt","publicDecrypt","_privateDecrypt","privateDecrypt","getUIntOption","options","key","value","Error","normalizeEncoding","enc","retried","toLowerCase","validateEncoding","data","encoding","normalizedEncoding","length","getDecoder","decoder","CipherCommon","Transform","constructor","cipherType","cipherKey","isCipher","iv","cipherKeyBuffer","authTagLength","args","cipher_type","cipher_key","auth_tag_len","internal","update","inputEncoding","outputEncoding","defaultEncoding","ArrayBuffer","isView","ret","write","from","final","end","_transform","chunk","callback","push","_flush","setAutoPadding","autoPadding","setAAD","buffer","plaintextLength","setAuthTag","tag","Cipher","Decipher","algorithm","password","createDecipheriv","createCipheriv","rsaFunctionFor","method","defaultPadding","keyType","format","type","passphrase","padding","oaepHash","oaepLabel","undefined","rawRes","RSA_PKCS1_OAEP_PADDING","RSA_PKCS1_PADDING","parseKeyEncoding","publicKeyEncoding","privateKeyEncoding","publicFormat","publicType","privateFormat","privateType","cipher","internalGenerateKeyPair","isAsync","modulusLength","publicExponent","generateKeyPair","kKeyVariantRSA_SSA_PKCS1_v1_5","then","err","publicKey","privateKey","catch","generateKeyPairSync","hash","mgf1Hash","hashAlgorithm","mgf1HashAlgorithm","saltLength","kKeyVariantRSA_PSS","_"],"mappings":";;AAAA;AACA,SAASA,iBAAT,QAAkC,uCAAlC;AACA,OAAOC,MAAP,MAAmB,QAAnB;AACA,SAEEC,uBAFF,EAKEC,kBALF,EAMEC,YANF,EAOEC,gBAPF,EAQEC,cARF,EASEC,cATF,EAUEC,cAVF,EAWEC,aAXF,QAYO,SAZP;AAaA,SAAyBC,aAAzB,QAA8C,4BAA9C,C,CACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,aAAT,QAA8B,gBAA9B;AACA,SAASC,MAAT,QAAuB,gCAAvB;AACA,SAASA,MAAM,IAAIC,OAAnB,QAAkC,aAAlC;AACA,SAASC,SAAT,QAA0B,aAA1B;AACA,SACEC,uBADF,EAEEC,sBAFF,EAGEC,iBAHF,EAIEC,yBAJF,QAKO,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3B,SAA1B,EACE/F,cAAc,CAAC0H,iBAAD,EAAoB,2BAApB,CAAd;;AACF,YAAIH,IAAI,KAAKxB,SAAb,EAAwB;AACtB;AACA;AACA;AACA;AACA;AACA/F,UAAAA,cAAc,CAACuH,IAAD,EAAO,cAAP,CAAd;;AACA,cAAIE,aAAa,IAAIF,IAAI,KAAKE,aAA9B,EAA6C;AAC3C,kBAAM,IAAI3F,KAAJ,CAAW,iCAAgCyF,IAAK,EAAhD,CAAN;AACD;AACF;;AACD,YAAIC,QAAQ,KAAKzB,SAAjB,EAA4B;AAC1B;AACA;AACA;AACA;AACA;AACA/F,UAAAA,cAAc,CAACwH,QAAD,EAAW,kBAAX,CAAd;;AACA,cAAIE,iBAAiB,IAAIF,QAAQ,KAAKE,iBAAtC,EAAyD;AACvD,kBAAM,IAAI5F,KAAJ,CAAW,qCAAoC0F,QAAS,EAAxD,CAAN;AACD;AACF;;AAED,eAAO/H,iBAAiB,CAAC6H,mBAAlB,CACLnH,aAAa,CAACyH,kBADT,EAELf,aAFK,EAGLC,cAHK,EAILW,aAAa,IAAIF,IAJZ,EAKLG,iBAAiB,IAAIF,QALhB,EAMLG,UANK,EAOL,GAAGtF,QAPE,CAAP;AASD;AACD;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AAEA;AACA;AAEA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,YA/LF,CAgME;;AAhMF;;AAkMA,QAAM,IAAIP,KAAJ,CACH,6BAA4B4D,IAAK,4FAD9B,CAAN;AAGD,C,CAED;;;AAUA,OAAO,SAASqB,eAAT,CACLrB,IADK,EAEL/D,OAFK,EAGL0C,QAHK,EAIL;AACA,MAAI,OAAO1C,OAAP,KAAmB,UAAvB,EAAmC;AACjC0C,IAAAA,QAAQ,GAAG1C,OAAX;AACAA,IAAAA,OAAO,GAAGoE,SAAV;AACD;;AAEDjG,EAAAA,gBAAgB,CAACuE,QAAD,CAAhB;AAEAsC,EAAAA,uBAAuB,CAAC,IAAD,EAAOjB,IAAP,EAAa/D,OAAb,EAAsB0C,QAAtB,CAAvB;AACD;AAUD,OAAO,SAASiD,mBAAT,CACL5B,IADK,EAEL/D,OAFK,EAGgC;AACrC,QAAM,CAACkG,CAAD,EAAIV,SAAJ,EAAeC,UAAf,IAA6BT,uBAAuB,CACxD,KADwD,EAExDjB,IAFwD,EAGxD/D,OAHwD,EAIxDoE,SAJwD,CAA1D;AAOA,SAAO;AACLoB,IAAAA,SADK;AAELC,IAAAA;AAFK,GAAP;AAID","sourcesContent":["/* eslint-disable no-dupe-class-members */\nimport { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';\nimport Stream from 'stream';\nimport {\n BinaryLike,\n binaryLikeToArrayBuffer,\n CipherEncoding,\n Encoding,\n getDefaultEncoding,\n kEmptyObject,\n validateFunction,\n validateObject,\n validateString,\n validateUint32,\n validateInt32,\n} from './Utils';\nimport { InternalCipher, RSAKeyVariant } from './NativeQuickCrypto/Cipher';\n// TODO(osp) re-enable type specific constructors\n// They are nice to have but not absolutely necessary\n// import type {\n// CipherCCMOptions,\n// CipherCCMTypes,\n// CipherGCMTypes,\n// CipherGCMOptions,\n// // CipherKey,\n// // KeyObject,\n// // TODO(Szymon) This types seem to be missing? Where did you get this definitions from?\n// // CipherOCBTypes,\n// // CipherOCBOptions,\n// } from 'crypto'; // Node crypto typings\nimport { StringDecoder } from 'string_decoder';\nimport { Buffer } from '@craftzdog/react-native-buffer';\nimport { Buffer as SBuffer } from 'safe-buffer';\nimport { constants } from './constants';\nimport {\n parsePrivateKeyEncoding,\n parsePublicKeyEncoding,\n preparePrivateKey,\n preparePublicOrPrivateKey,\n} from './keys';\n\n// make sure that nextTick is there\nglobal.process.nextTick = setImmediate;\n\nconst createInternalCipher = NativeQuickCrypto.createCipher;\nconst createInternalDecipher = NativeQuickCrypto.createDecipher;\nconst _publicEncrypt = NativeQuickCrypto.publicEncrypt;\nconst _publicDecrypt = NativeQuickCrypto.publicDecrypt;\nconst _privateDecrypt = NativeQuickCrypto.privateDecrypt;\n\nfunction getUIntOption(options: Record<string, any>, key: string) {\n let value;\n if (options && (value = options[key]) != null) {\n // >>> Turns any type into a positive integer (also sets the sign bit to 0)\n // eslint-disable-next-line no-bitwise\n if (value >>> 0 !== value) throw new Error(`options.${key}: ${value}`);\n return value;\n }\n return -1;\n}\n\nfunction normalizeEncoding(enc: string) {\n if (!enc) return 'utf8';\n var retried;\n while (true) {\n switch (enc) {\n case 'utf8':\n case 'utf-8':\n return 'utf8';\n case 'ucs2':\n case 'ucs-2':\n case 'utf16le':\n case 'utf-16le':\n return 'utf16le';\n case 'latin1':\n case 'binary':\n return 'latin1';\n case 'base64':\n case 'ascii':\n case 'hex':\n return enc;\n default:\n if (retried) return; // undefined\n enc = ('' + enc).toLowerCase();\n retried = true;\n }\n }\n}\n\nfunction validateEncoding(data: string, encoding: string) {\n const normalizedEncoding = normalizeEncoding(encoding);\n const length = data.length;\n\n if (normalizedEncoding === 'hex' && length % 2 !== 0) {\n throw new Error(`Encoding ${encoding} not valid for data length ${length}`);\n }\n}\n\nfunction getDecoder(decoder?: StringDecoder, encoding?: BufferEncoding) {\n return decoder ?? new StringDecoder(encoding);\n}\n\nclass CipherCommon extends Stream.Transform {\n private internal: InternalCipher;\n private decoder: StringDecoder | undefined;\n\n constructor(\n cipherType: string,\n cipherKey: BinaryLike,\n isCipher: boolean,\n options: Record<string, any> = {},\n iv?: BinaryLike | null\n ) {\n super(options);\n const cipherKeyBuffer = binaryLikeToArrayBuffer(cipherKey);\n // TODO(osp) This might not be smart, check again after release\n const authTagLength = getUIntOption(options, 'authTagLength');\n const args = {\n cipher_type: cipherType,\n cipher_key: cipherKeyBuffer,\n iv,\n ...options,\n auth_tag_len: authTagLength,\n };\n this.internal = isCipher\n ? createInternalCipher(args)\n : createInternalDecipher(args);\n }\n\n update(\n data: BinaryLike,\n inputEncoding?: CipherEncoding,\n outputEncoding?: CipherEncoding\n ): ArrayBuffer | string {\n const defaultEncoding = getDefaultEncoding();\n inputEncoding = inputEncoding ?? defaultEncoding;\n outputEncoding = outputEncoding ?? defaultEncoding;\n\n if (typeof data === 'string') {\n validateEncoding(data, inputEncoding);\n } else if (!ArrayBuffer.isView(data)) {\n throw new Error('Invalid data argument');\n }\n\n if (typeof data === 'string') {\n // On node this is handled on the native side\n // on our case we need to correctly send the arraybuffer to the jsi side\n inputEncoding = inputEncoding === 'buffer' ? 'utf8' : inputEncoding;\n data = binaryLikeToArrayBuffer(data, inputEncoding);\n } else {\n data = binaryLikeToArrayBuffer(data as any, inputEncoding);\n }\n\n const ret = this.internal.update(data);\n\n if (outputEncoding && outputEncoding !== 'buffer') {\n this.decoder = getDecoder(this.decoder, outputEncoding);\n\n return this.decoder!.write(SBuffer.from(ret) as any);\n }\n\n return ret;\n }\n\n final(): ArrayBuffer;\n final(outputEncoding: BufferEncoding | 'buffer'): string;\n final(outputEncoding?: BufferEncoding | 'buffer'): ArrayBuffer | string {\n const ret = this.internal.final();\n\n if (outputEncoding && outputEncoding !== 'buffer') {\n this.decoder = getDecoder(this.decoder, outputEncoding);\n\n return this.decoder!.end(SBuffer.from(ret) as any);\n }\n\n return ret;\n }\n\n _transform(chunk: BinaryLike, encoding: Encoding, callback: () => void) {\n this.push(this.update(chunk, encoding));\n callback();\n }\n\n _flush(callback: () => void) {\n this.push(this.final());\n callback();\n }\n\n public setAutoPadding(autoPadding?: boolean): this {\n this.internal.setAutoPadding(!!autoPadding);\n return this;\n }\n\n public setAAD(\n buffer: Buffer,\n options?: {\n plaintextLength: number;\n }\n ): this {\n this.internal.setAAD({\n data: buffer.buffer,\n plaintextLength: options?.plaintextLength,\n });\n return this;\n }\n\n // protected getAuthTag(): Buffer {\n // return Buffer.from(this.internal.getAuthTag());\n // }\n\n public setAuthTag(tag: Buffer): this {\n this.internal.setAuthTag(tag.buffer);\n return this;\n }\n}\n\nclass Cipher extends CipherCommon {\n constructor(\n cipherType: string,\n cipherKey: BinaryLike,\n options: Record<string, any> = {},\n iv?: BinaryLike | null\n ) {\n if (iv != null) {\n iv = binaryLikeToArrayBuffer(iv);\n }\n super(cipherType, cipherKey, true, options, iv);\n }\n}\n\nclass Decipher extends CipherCommon {\n constructor(\n cipherType: string,\n cipherKey: BinaryLike,\n options: Record<string, any> = {},\n iv?: BinaryLike | null\n ) {\n if (iv != null) {\n iv = binaryLikeToArrayBuffer(iv);\n }\n\n super(cipherType, cipherKey, false, options, iv);\n }\n}\n\n// TODO(osp) This definitions cause typescript errors when using the API\n// export function createDecipher(\n// algorithm: CipherCCMTypes,\n// password: BinaryLike,\n// options: CipherCCMOptions\n// ): Decipher;\n// export function createDecipher(\n// algorithm: CipherGCMTypes,\n// password: BinaryLike,\n// options?: CipherGCMOptions\n// ): Decipher;\nexport function createDecipher(\n algorithm: string,\n password: BinaryLike,\n options?: Stream.TransformOptions\n): Decipher {\n return new Decipher(algorithm, password, options);\n}\n\n// TODO(osp) This definitions cause typescript errors when using the API\n// export function createDecipheriv(\n// algorithm: CipherCCMTypes,\n// key: BinaryLike,\n// iv: BinaryLike,\n// options: CipherCCMOptions\n// ): Decipher;\n// export function createDecipheriv(\n// algorithm: CipherOCBTypes,\n// key: BinaryLike,\n// iv: BinaryLike,\n// options: CipherOCBOptions\n// ): DecipherOCB;\n// export function createDecipheriv(\n// algorithm: CipherGCMTypes,\n// key: BinaryLike,\n// iv: BinaryLike,\n// options?: CipherGCMOptions\n// ): Decipher;\nexport function createDecipheriv(\n algorithm: string,\n key: BinaryLike,\n iv: BinaryLike | null,\n options?: Stream.TransformOptions\n): Decipher {\n return new Decipher(algorithm, key, options, iv);\n}\n\n// TODO(osp) This definitions cause typescript errors when using the API\n// commenting them out for now\n// export function createCipher(\n// algorithm: CipherCCMTypes,\n// password: BinaryLike,\n// options: CipherCCMOptions\n// ): Cipher;\n// export function createCipher(\n// algorithm: CipherGCMTypes,\n// password: BinaryLike,\n// options?: CipherGCMOptions\n// ): Cipher;\nexport function createCipher(\n algorithm: string,\n password: BinaryLike,\n options?: Stream.TransformOptions\n): Cipher {\n return new Cipher(algorithm, password, options);\n}\n\n// TODO(osp) on all the createCipheriv methods, node seems to use a \"KeyObject\" is seems to be a thread safe\n// object that creates keys and what not. Not sure if we should support it.\n// Fow now I replaced all of them to BinaryLike\n// export function createCipheriv(\n// algorithm: CipherCCMTypes,\n// key: BinaryLike,\n// iv: BinaryLike,\n// options: CipherCCMOptions\n// ): Cipher;\n// export function createCipheriv(\n// algorithm: CipherOCBTypes,\n// key: BinaryLike,\n// iv: BinaryLike,\n// options: CipherOCBOptions\n// ): CipherOCB;\n// export function createCipheriv(\n// algorithm: CipherGCMTypes,\n// key: BinaryLike,\n// iv: BinaryLike,\n// options?: CipherGCMOptions\n// ): Cipher;\nexport function createCipheriv(\n algorithm: string,\n key: BinaryLike,\n iv: BinaryLike | null,\n options?: Stream.TransformOptions\n): Cipher {\n return new Cipher(algorithm, key, options, iv);\n}\n\n// RSA Functions\n// Follows closely the model implemented in node\n\n// TODO(osp) types...\nfunction rsaFunctionFor(\n method: (\n data: ArrayBuffer,\n format: number,\n type: any,\n passphrase: any,\n buffer: ArrayBuffer,\n padding: number,\n oaepHash: any,\n oaepLabel: any\n ) => Buffer,\n defaultPadding: number,\n keyType: 'public' | 'private'\n) {\n return (\n options: {\n key: any;\n encoding?: string;\n format?: any;\n padding?: any;\n oaepHash?: any;\n oaepLabel?: any;\n passphrase?: string;\n },\n buffer: BinaryLike\n ) => {\n const { format, type, data, passphrase } =\n keyType === 'private'\n ? preparePrivateKey(options)\n : preparePublicOrPrivateKey(options);\n const padding = options.padding || defaultPadding;\n const { oaepHash, encoding } = options;\n let { oaepLabel } = options;\n if (oaepHash !== undefined) validateString(oaepHash, 'key.oaepHash');\n if (oaepLabel !== undefined)\n oaepLabel = binaryLikeToArrayBuffer(oaepLabel, encoding);\n buffer = binaryLikeToArrayBuffer(buffer, encoding);\n\n const rawRes = method(\n data,\n format,\n type,\n passphrase,\n buffer,\n padding,\n oaepHash,\n oaepLabel\n );\n\n return Buffer.from(rawRes);\n };\n}\n\nexport const publicEncrypt = rsaFunctionFor(\n _publicEncrypt,\n constants.RSA_PKCS1_OAEP_PADDING,\n 'public'\n);\nexport const publicDecrypt = rsaFunctionFor(\n _publicDecrypt,\n constants.RSA_PKCS1_PADDING,\n 'public'\n);\n// const privateEncrypt = rsaFunctionFor(_privateEncrypt, constants.RSA_PKCS1_PADDING,\n// 'private');\nexport const privateDecrypt = rsaFunctionFor(\n _privateDecrypt,\n constants.RSA_PKCS1_OAEP_PADDING,\n 'private'\n);\n\n// _ _ __ _____ _\n// | | | |/ / | __ \\ (_)\n// __ _ ___ _ __ ___ _ __ __ _| |_ ___| ' / ___ _ _| |__) |_ _ _ _ __\n// / _` |/ _ \\ '_ \\ / _ \\ '__/ _` | __/ _ \\ < / _ \\ | | | ___/ _` | | '__|\n// | (_| | __/ | | | __/ | | (_| | || __/ . \\ __/ |_| | | | (_| | | |\n// \\__, |\\___|_| |_|\\___|_| \\__,_|\\__\\___|_|\\_\\___|\\__, |_| \\__,_|_|_|\n// __/ | __/ |\n// |___/ |___/\ntype GenerateKeyPairOptions = {\n modulusLength: number; // Key size in bits (RSA, DSA).\n publicExponent?: number; // Public exponent (RSA). Default: 0x10001.\n hashAlgorithm?: string; // Name of the message digest (RSA-PSS).\n mgf1HashAlgorithm?: string; // string Name of the message digest used by MGF1 (RSA-PSS).\n saltLength?: number; // Minimal salt length in bytes (RSA-PSS).\n divisorLength?: number; // Size of q in bits (DSA).\n namedCurve?: string; // Name of the curve to use (EC).\n prime?: Buffer; // The prime parameter (DH).\n primeLength?: number; // Prime length in bits (DH).\n generator?: number; // Custom generator (DH). Default: 2.\n groupName?: string; // Diffie-Hellman group name (DH). See crypto.getDiffieHellman().\n publicKeyEncoding?: any; // See keyObject.export().\n privateKeyEncoding?: any; // See keyObject.export().\n paramEncoding?: string;\n hash?: any;\n mgf1Hash?: any;\n};\ntype GenerateKeyPairCallback = (\n error: unknown | null,\n publicKey?: Buffer,\n privateKey?: Buffer\n) => void;\n\nfunction parseKeyEncoding(\n keyType: string,\n options: GenerateKeyPairOptions = kEmptyObject\n) {\n const { publicKeyEncoding, privateKeyEncoding } = options;\n\n let publicFormat, publicType;\n if (publicKeyEncoding == null) {\n publicFormat = publicType = undefined;\n } else if (typeof publicKeyEncoding === 'object') {\n ({ format: publicFormat, type: publicType } = parsePublicKeyEncoding(\n publicKeyEncoding,\n keyType,\n 'publicKeyEncoding'\n ));\n } else {\n throw new Error(\n 'Invalid argument options.publicKeyEncoding',\n publicKeyEncoding\n );\n }\n\n let privateFormat, privateType, cipher, passphrase;\n if (privateKeyEncoding == null) {\n privateFormat = privateType = undefined;\n } else if (typeof privateKeyEncoding === 'object') {\n ({\n format: privateFormat,\n type: privateType,\n cipher,\n passphrase,\n } = parsePrivateKeyEncoding(\n privateKeyEncoding,\n keyType,\n 'privateKeyEncoding'\n ));\n } else {\n throw new Error(\n 'Invalid argument options.privateKeyEncoding',\n publicKeyEncoding\n );\n }\n\n return [\n publicFormat,\n publicType,\n privateFormat,\n privateType,\n cipher,\n passphrase,\n ];\n}\n\nfunction internalGenerateKeyPair(\n isAsync: boolean,\n type: string,\n options: GenerateKeyPairOptions | undefined,\n callback: GenerateKeyPairCallback | undefined\n) {\n // On node a very complex \"job\" chain is created, we are going for a far simpler approach and calling\n // an internal function that basically executes the same byte shuffling on the native side\n const encoding = parseKeyEncoding(type, options);\n\n // if (options !== undefined)\n // validateObject(options, 'options');\n\n switch (type) {\n case 'rsa-pss':\n case 'rsa': {\n validateObject<GenerateKeyPairOptions>(options, 'options');\n const { modulusLength } = options!;\n validateUint32(modulusLength, 'options.modulusLength');\n\n let { publicExponent } = options!;\n if (publicExponent == null) {\n publicExponent = 0x10001;\n } else {\n validateUint32(publicExponent, 'options.publicExponent');\n }\n\n if (type === 'rsa') {\n if (isAsync) {\n NativeQuickCrypto.generateKeyPair(\n RSAKeyVariant.kKeyVariantRSA_SSA_PKCS1_v1_5,\n modulusLength,\n publicExponent,\n ...encoding\n )\n .then(([err, publicKey, privateKey]) => {\n if (typeof publicKey === 'object') {\n publicKey = Buffer.from(publicKey);\n }\n if (typeof privateKey === 'object') {\n privateKey = Buffer.from(privateKey);\n }\n callback?.(err, publicKey, privateKey);\n })\n .catch((err) => {\n callback?.(err, undefined, undefined);\n });\n return;\n } else {\n let [err, publicKey, privateKey] =\n NativeQuickCrypto.generateKeyPairSync(\n RSAKeyVariant.kKeyVariantRSA_SSA_PKCS1_v1_5,\n modulusLength,\n publicExponent,\n ...encoding\n );\n\n if (typeof publicKey === 'object') {\n publicKey = Buffer.from(publicKey);\n }\n if (typeof privateKey === 'object') {\n privateKey = Buffer.from(privateKey);\n }\n\n return [err, publicKey, privateKey];\n }\n }\n\n const { hash, mgf1Hash, hashAlgorithm, mgf1HashAlgorithm, saltLength } =\n options!;\n\n // // We don't have a process object on RN\n // // const pendingDeprecation = getOptionValue('--pending-deprecation');\n\n if (saltLength !== undefined)\n validateInt32(saltLength, 'options.saltLength', 0);\n if (hashAlgorithm !== undefined)\n validateString(hashAlgorithm, 'options.hashAlgorithm');\n if (mgf1HashAlgorithm !== undefined)\n validateString(mgf1HashAlgorithm, 'options.mgf1HashAlgorithm');\n if (hash !== undefined) {\n // pendingDeprecation && process.emitWarning(\n // '\"options.hash\" is deprecated, ' +\n // 'use \"options.hashAlgorithm\" instead.',\n // 'DeprecationWarning',\n // 'DEP0154');\n validateString(hash, 'options.hash');\n if (hashAlgorithm && hash !== hashAlgorithm) {\n throw new Error(`Invalid Argument options.hash ${hash}`);\n }\n }\n if (mgf1Hash !== undefined) {\n // pendingDeprecation && process.emitWarning(\n // '\"options.mgf1Hash\" is deprecated, ' +\n // 'use \"options.mgf1HashAlgorithm\" instead.',\n // 'DeprecationWarning',\n // 'DEP0154');\n validateString(mgf1Hash, 'options.mgf1Hash');\n if (mgf1HashAlgorithm && mgf1Hash !== mgf1HashAlgorithm) {\n throw new Error(`Invalid Argument options.mgf1Hash ${mgf1Hash}`);\n }\n }\n\n return NativeQuickCrypto.generateKeyPairSync(\n RSAKeyVariant.kKeyVariantRSA_PSS,\n modulusLength,\n publicExponent,\n hashAlgorithm || hash,\n mgf1HashAlgorithm || mgf1Hash,\n saltLength,\n ...encoding\n );\n }\n // case 'dsa': {\n // validateObject(options, 'options');\n // const { modulusLength } = options!;\n // validateUint32(modulusLength, 'options.modulusLength');\n\n // let { divisorLength } = options!;\n // if (divisorLength == null) {\n // divisorLength = -1;\n // } else validateInt32(divisorLength, 'options.divisorLength', 0);\n\n // // return new DsaKeyPairGenJob(\n // // mode,\n // // modulusLength,\n // // divisorLength,\n // // ...encoding);\n // }\n // case 'ec': {\n // validateObject(options, 'options');\n // const { namedCurve } = options!;\n // validateString(namedCurve, 'options.namedCurve');\n // let { paramEncoding } = options!;\n // if (paramEncoding == null || paramEncoding === 'named')\n // paramEncoding = OPENSSL_EC_NAMED_CURVE;\n // else if (paramEncoding === 'explicit')\n // paramEncoding = OPENSSL_EC_EXPLICIT_CURVE;\n // else\n // throw new Error(`Invalid Argument options.paramEncoding ${paramEncoding}`);\n // // throw new ERR_INVALID_ARG_VALUE('options.paramEncoding', paramEncoding);\n\n // // return new EcKeyPairGenJob(mode, namedCurve, paramEncoding, ...encoding);\n // }\n // case 'ed25519':\n // case 'ed448':\n // case 'x25519':\n // case 'x448': {\n // let id;\n // switch (type) {\n // case 'ed25519':\n // id = EVP_PKEY_ED25519;\n // break;\n // case 'ed448':\n // id = EVP_PKEY_ED448;\n // break;\n // case 'x25519':\n // id = EVP_PKEY_X25519;\n // break;\n // case 'x448':\n // id = EVP_PKEY_X448;\n // break;\n // }\n // return new NidKeyPairGenJob(mode, id, ...encoding);\n // }\n // case 'dh': {\n // validateObject(options, 'options');\n // const { group, primeLength, prime, generator } = options;\n // if (group != null) {\n // if (prime != null)\n // throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'prime');\n // if (primeLength != null)\n // throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'primeLength');\n // if (generator != null)\n // throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'generator');\n\n // validateString(group, 'options.group');\n\n // return new DhKeyPairGenJob(mode, group, ...encoding);\n // }\n\n // if (prime != null) {\n // if (primeLength != null)\n // throw new ERR_INCOMPATIBLE_OPTION_PAIR('prime', 'primeLength');\n\n // validateBuffer(prime, 'options.prime');\n // } else if (primeLength != null) {\n // validateInt32(primeLength, 'options.primeLength', 0);\n // } else {\n // throw new ERR_MISSING_OPTION(\n // 'At least one of the group, prime, or primeLength options'\n // );\n // }\n\n // if (generator != null) {\n // validateInt32(generator, 'options.generator', 0);\n // }\n // return new DhKeyPairGenJob(\n // mode,\n // prime != null ? prime : primeLength,\n // generator == null ? 2 : generator,\n // ...encoding\n // );\n // }\n default:\n // Fall through\n }\n throw new Error(\n `Invalid Argument options: ${type} scheme not supported. Currently not all encryption methods are supported in quick-crypto!`\n );\n}\n\n// TODO(osp) put correct types (e.g. type -> 'rsa', etc..)\nexport function generateKeyPair(\n type: string,\n callback: GenerateKeyPairCallback\n): void;\nexport function generateKeyPair(\n type: string,\n options: GenerateKeyPairOptions,\n callback: GenerateKeyPairCallback\n): void;\nexport function generateKeyPair(\n type: string,\n options?: GenerateKeyPairCallback | GenerateKeyPairOptions,\n callback?: GenerateKeyPairCallback\n) {\n if (typeof options === 'function') {\n callback = options;\n options = undefined;\n }\n\n validateFunction(callback);\n\n internalGenerateKeyPair(true, type, options, callback);\n}\n\nexport function generateKeyPairSync(type: string): {\n publicKey: any;\n privateKey: any;\n};\nexport function generateKeyPairSync(\n type: string,\n options: GenerateKeyPairOptions\n): { publicKey: any; privateKey: any };\nexport function generateKeyPairSync(\n type: string,\n options?: GenerateKeyPairOptions\n): { publicKey: any; privateKey: any } {\n const [_, publicKey, privateKey] = internalGenerateKeyPair(\n false,\n type,\n options,\n undefined\n )!;\n\n return {\n publicKey,\n privateKey,\n };\n}\n"]}
@@ -1,2 +1,10 @@
1
- export {};
1
+ // TODO(osp) on node this is defined on the native side
2
+ // Need to do the same so that values are always in sync
3
+ export let RSAKeyVariant;
4
+
5
+ (function (RSAKeyVariant) {
6
+ RSAKeyVariant[RSAKeyVariant["kKeyVariantRSA_SSA_PKCS1_v1_5"] = 0] = "kKeyVariantRSA_SSA_PKCS1_v1_5";
7
+ RSAKeyVariant[RSAKeyVariant["kKeyVariantRSA_PSS"] = 1] = "kKeyVariantRSA_PSS";
8
+ RSAKeyVariant[RSAKeyVariant["kKeyVariantRSA_OAEP"] = 2] = "kKeyVariantRSA_OAEP";
9
+ })(RSAKeyVariant || (RSAKeyVariant = {}));
2
10
  //# sourceMappingURL=Cipher.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":[],"names":[],"mappings":"","sourcesContent":[]}
1
+ {"version":3,"sources":["Cipher.ts"],"names":["RSAKeyVariant"],"mappings":"AAGA;AACA;AACA,WAAYA,aAAZ;;WAAYA,a;AAAAA,EAAAA,a,CAAAA,a;AAAAA,EAAAA,a,CAAAA,a;AAAAA,EAAAA,a,CAAAA,a;GAAAA,a,KAAAA,a","sourcesContent":["import type { BinaryLike } from 'src/Utils';\nimport type { Buffer } from '@craftzdog/react-native-buffer';\n\n// TODO(osp) on node this is defined on the native side\n// Need to do the same so that values are always in sync\nexport enum RSAKeyVariant {\n kKeyVariantRSA_SSA_PKCS1_v1_5,\n kKeyVariantRSA_PSS,\n kKeyVariantRSA_OAEP,\n}\n\nexport type InternalCipher = {\n update: (data: BinaryLike | ArrayBufferView) => ArrayBuffer;\n final: () => ArrayBuffer;\n copy: () => void;\n setAAD: (args: {\n data: BinaryLike;\n plaintextLength?: number;\n }) => InternalCipher;\n setAutoPadding: (autoPad: boolean) => boolean;\n setAuthTag: (tag: ArrayBuffer) => boolean;\n};\n\nexport type CreateCipherMethod = (params: {\n cipher_type: string;\n cipher_key: ArrayBuffer;\n auth_tag_len: number;\n}) => InternalCipher;\n\nexport type CreateDecipherMethod = (params: {\n cipher_type: string;\n cipher_key: ArrayBuffer;\n auth_tag_len: number;\n}) => InternalCipher;\n\nexport type PublicEncryptMethod = (\n data: ArrayBuffer,\n format: number,\n type: any,\n passphrase: any,\n buffer: ArrayBuffer,\n padding: number,\n oaepHash: any,\n oaepLabel: any\n) => Buffer;\nexport type PrivateDecryptMethod = (\n data: ArrayBuffer,\n format: number,\n type: any,\n passphrase: any,\n buffer: ArrayBuffer,\n padding: number,\n oaepHash: any,\n oaepLabel: any\n) => Buffer;\n\nexport type GenerateKeyPairMethod = (\n keyVariant: RSAKeyVariant,\n modulusLength: number,\n publicExponent: number,\n ...rest: any[]\n) => Promise<[error: unknown, publicBuffer: any, privateBuffer: any]>;\n\nexport type GenerateKeyPairSyncMethod = (\n keyVariant: RSAKeyVariant,\n modulusLength: number,\n publicExponent: number,\n ...rest: any[]\n) => [error: unknown, publicBuffer: any, privateBuffer: any];\n"]}
@@ -1 +1 @@
1
- {"version":3,"sources":["NativeQuickCrypto.ts"],"names":["NativeModules","Platform","global","__QuickCryptoProxy","QuickCryptoModule","QuickCrypto","message","OS","ExpoConstants","NativeUnimoduleProxy","modulesConstants","ExponentConstants","appOwnership","Error","nativeCallSyncHook","install","result","proxy","NativeQuickCrypto"],"mappings":"AAAA,SAASA,aAAT,EAAwBC,QAAxB,QAAwC,cAAxC;;AAsBA;AACA,IAAIC,MAAM,CAACC,kBAAP,IAA6B,IAAjC,EAAuC;AACrC;AACA,QAAMC,iBAAiB,GAAGJ,aAAa,CAACK,WAAxC;;AACA,MAAID,iBAAiB,IAAI,IAAzB,EAA+B;AAAA;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NativeModules, Platform } from 'react-native';\nimport type { CreateHmacMethod } from './hmac';\nimport type { CreateHashMethod } from './hash';\nimport type { Pbkdf2Object } from './pbkdf2';\nimport type { RandomObject } from './random';\nimport type { CreateCipherMethod, CreateDecipherMethod } from './Cipher';\n\ninterface NativeQuickCryptoSpec {\n createHmac: CreateHmacMethod;\n pbkdf2: Pbkdf2Object;\n random: RandomObject;\n createHash: CreateHashMethod;\n createCipher: CreateCipherMethod;\n createDecipher: CreateDecipherMethod;\n}\n\n// global func declaration for JSI functions\ndeclare global {\n function nativeCallSyncHook(): unknown;\n var __QuickCryptoProxy: object | undefined;\n}\n\n// Check if the constructor exists. If not, try installing the JSI bindings.\nif (global.__QuickCryptoProxy == null) {\n // Get the native QuickCrypto ReactModule\n const QuickCryptoModule = NativeModules.QuickCrypto;\n if (QuickCryptoModule == null) {\n let message =\n 'Failed to install react-native-quick-crypto: The native `QuickCrypto` Module could not be found.';\n message +=\n '\\n* Make sure react-native-quick-crypto is correctly autolinked (run `npx react-native config` to verify)';\n if (Platform.OS === 'ios' || Platform.OS === 'macos') {\n message += '\\n* Make sure you ran `pod install` in the ios/ directory.';\n }\n if (Platform.OS === 'android') {\n message += '\\n* Make sure gradle is synced.';\n }\n // check if Expo\n const ExpoConstants =\n NativeModules.NativeUnimoduleProxy?.modulesConstants?.ExponentConstants;\n if (ExpoConstants != null) {\n if (ExpoConstants.appOwnership === 'expo') {\n // We're running Expo Go\n throw new Error(\n 'react-native-quick-crypto is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.'\n );\n } else {\n // We're running Expo bare / standalone\n message += '\\n* Make sure you ran `expo prebuild`.';\n }\n }\n\n message += '\\n* Make sure you rebuilt the app.';\n throw new Error(message);\n }\n\n // Check if we are running on-device (JSI)\n if (global.nativeCallSyncHook == null || QuickCryptoModule.install == null) {\n throw new Error(\n 'Failed to install react-native-quick-crypto: React Native is not running on-device. QuickCrypto can only be used when synchronous method invocations (JSI) are possible. If you are using a remote debugger (e.g. Chrome), switch to an on-device debugger (e.g. Flipper) instead.'\n );\n }\n\n // Call the synchronous blocking install() function\n const result = QuickCryptoModule.install();\n if (result !== true)\n throw new Error(\n `Failed to install react-native-quick-crypto: The native QuickCrypto Module could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`\n );\n\n // Check again if the constructor now exists. If not, throw an error.\n if (global.__QuickCryptoProxy == null)\n throw new Error(\n 'Failed to install react-native-quick-crypto, the native initializer function does not exist. Are you trying to use QuickCrypto from different JS Runtimes?'\n );\n}\n\nconst proxy = global.__QuickCryptoProxy;\nexport const NativeQuickCrypto = proxy as any as NativeQuickCryptoSpec;\n"]}
1
+ {"version":3,"sources":["NativeQuickCrypto.ts"],"names":["NativeModules","Platform","global","__QuickCryptoProxy","QuickCryptoModule","QuickCrypto","message","OS","ExpoConstants","NativeUnimoduleProxy","modulesConstants","ExponentConstants","appOwnership","Error","nativeCallSyncHook","install","result","proxy","NativeQuickCrypto"],"mappings":"AAAA,SAASA,aAAT,EAAwBC,QAAxB,QAAwC,cAAxC;;AAkCA;AACA,IAAIC,MAAM,CAACC,kBAAP,IAA6B,IAAjC,EAAuC;AACrC;AACA,QAAMC,iBAAiB,GAAGJ,aAAa,CAACK,WAAxC;;AACA,MAAID,iBAAiB,IAAI,IAAzB,EAA+B;AAAA;;AAC7B,QAAIE,OAAO,GACT,kGADF;AAEAA,IAAAA,OAAO,IACL,2GADF;;AAEA,QAAIL,QAAQ,CAACM,EAAT,KAAgB,KAAhB,IAAyBN,QAAQ,CAACM,EAAT,KAAgB,OAA7C,EAAsD;AACpDD,MAAAA,OAAO,IAAI,4DAAX;AACD;;AACD,QAAIL,QAAQ,CAACM,EAAT,KAAgB,SAApB,EAA+B;AAC7BD,MAAAA,OAAO,IAAI,iCAAX;AACD,KAV4B,CAW7B;;;AACA,UAAME,aAAa,4BACjBR,aAAa,CAACS,oBADG,oFACjB,sBAAoCC,gBADnB,2DACjB,uBAAsDC,iBADxD;;AAEA,QAAIH,aAAa,IAAI,IAArB,EAA2B;AACzB,UAAIA,aAAa,CAACI,YAAd,KAA+B,MAAnC,EAA2C;AACzC;AACA,cAAM,IAAIC,KAAJ,CACJ,uHADI,CAAN;AAGD,OALD,MAKO;AACL;AACAP,QAAAA,OAAO,IAAI,wCAAX;AACD;AACF;;AAEDA,IAAAA,OAAO,IAAI,oCAAX;AACA,UAAM,IAAIO,KAAJ,CAAUP,OAAV,CAAN;AACD,GA/BoC,CAiCrC;;;AACA,MAAIJ,MAAM,CAACY,kBAAP,IAA6B,IAA7B,IAAqCV,iBAAiB,CAACW,OAAlB,IAA6B,IAAtE,EAA4E;AAC1E,UAAM,IAAIF,KAAJ,CACJ,oRADI,CAAN;AAGD,GAtCoC,CAwCrC;;;AACA,QAAMG,MAAM,GAAGZ,iBAAiB,CAACW,OAAlB,EAAf;AACA,MAAIC,MAAM,KAAK,IAAf,EACE,MAAM,IAAIH,KAAJ,CACH,oKAAmKG,MAAO,EADvK,CAAN,CA3CmC,CA+CrC;;AACA,MAAId,MAAM,CAACC,kBAAP,IAA6B,IAAjC,EACE,MAAM,IAAIU,KAAJ,CACJ,4JADI,CAAN;AAGH;;AAED,MAAMI,KAAK,GAAGf,MAAM,CAACC,kBAArB;AACA,OAAO,MAAMe,iBAAiB,GAAGD,KAA1B","sourcesContent":["import { NativeModules, Platform } from 'react-native';\nimport type { CreateHmacMethod } from './hmac';\nimport type { CreateHashMethod } from './hash';\nimport type { Pbkdf2Object } from './pbkdf2';\nimport type { RandomObject } from './random';\nimport type {\n CreateCipherMethod,\n CreateDecipherMethod,\n PublicEncryptMethod,\n PrivateDecryptMethod,\n GenerateKeyPairMethod,\n GenerateKeyPairSyncMethod,\n} from './Cipher';\n\ninterface NativeQuickCryptoSpec {\n createHmac: CreateHmacMethod;\n pbkdf2: Pbkdf2Object;\n random: RandomObject;\n createHash: CreateHashMethod;\n createCipher: CreateCipherMethod;\n createDecipher: CreateDecipherMethod;\n publicEncrypt: PublicEncryptMethod;\n publicDecrypt: PublicEncryptMethod;\n privateDecrypt: PrivateDecryptMethod;\n generateKeyPair: GenerateKeyPairMethod;\n generateKeyPairSync: GenerateKeyPairSyncMethod;\n}\n\n// global func declaration for JSI functions\ndeclare global {\n function nativeCallSyncHook(): unknown;\n var __QuickCryptoProxy: object | undefined;\n}\n\n// Check if the constructor exists. If not, try installing the JSI bindings.\nif (global.__QuickCryptoProxy == null) {\n // Get the native QuickCrypto ReactModule\n const QuickCryptoModule = NativeModules.QuickCrypto;\n if (QuickCryptoModule == null) {\n let message =\n 'Failed to install react-native-quick-crypto: The native `QuickCrypto` Module could not be found.';\n message +=\n '\\n* Make sure react-native-quick-crypto is correctly autolinked (run `npx react-native config` to verify)';\n if (Platform.OS === 'ios' || Platform.OS === 'macos') {\n message += '\\n* Make sure you ran `pod install` in the ios/ directory.';\n }\n if (Platform.OS === 'android') {\n message += '\\n* Make sure gradle is synced.';\n }\n // check if Expo\n const ExpoConstants =\n NativeModules.NativeUnimoduleProxy?.modulesConstants?.ExponentConstants;\n if (ExpoConstants != null) {\n if (ExpoConstants.appOwnership === 'expo') {\n // We're running Expo Go\n throw new Error(\n 'react-native-quick-crypto is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.'\n );\n } else {\n // We're running Expo bare / standalone\n message += '\\n* Make sure you ran `expo prebuild`.';\n }\n }\n\n message += '\\n* Make sure you rebuilt the app.';\n throw new Error(message);\n }\n\n // Check if we are running on-device (JSI)\n if (global.nativeCallSyncHook == null || QuickCryptoModule.install == null) {\n throw new Error(\n 'Failed to install react-native-quick-crypto: React Native is not running on-device. QuickCrypto can only be used when synchronous method invocations (JSI) are possible. If you are using a remote debugger (e.g. Chrome), switch to an on-device debugger (e.g. Flipper) instead.'\n );\n }\n\n // Call the synchronous blocking install() function\n const result = QuickCryptoModule.install();\n if (result !== true)\n throw new Error(\n `Failed to install react-native-quick-crypto: The native QuickCrypto Module could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`\n );\n\n // Check again if the constructor now exists. If not, throw an error.\n if (global.__QuickCryptoProxy == null)\n throw new Error(\n 'Failed to install react-native-quick-crypto, the native initializer function does not exist. Are you trying to use QuickCrypto from different JS Runtimes?'\n );\n}\n\nconst proxy = global.__QuickCryptoProxy;\nexport const NativeQuickCrypto = proxy as any as NativeQuickCryptoSpec;\n"]}
@@ -1,8 +1,9 @@
1
1
  import * as pbkdf2 from './pbkdf2';
2
2
  import * as random from './random';
3
- import { createCipher, createCipheriv, createDecipher, createDecipheriv } from './Cipher';
3
+ import { createCipher, createCipheriv, createDecipher, createDecipheriv, publicEncrypt, publicDecrypt, privateDecrypt, generateKeyPair, generateKeyPairSync } from './Cipher';
4
4
  import { createHmac } from './Hmac';
5
5
  import { createHash } from './Hash';
6
+ import { constants } from './constants';
6
7
  export const QuickCrypto = {
7
8
  createHmac,
8
9
  Hmac: createHmac,
@@ -12,6 +13,12 @@ export const QuickCrypto = {
12
13
  createCipheriv,
13
14
  createDecipher,
14
15
  createDecipheriv,
16
+ publicEncrypt,
17
+ publicDecrypt,
18
+ privateDecrypt,
19
+ generateKeyPair,
20
+ generateKeyPairSync,
21
+ constants,
15
22
  ...pbkdf2,
16
23
  ...random
17
24
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["QuickCrypto.ts"],"names":["pbkdf2","random","createCipher","createCipheriv","createDecipher","createDecipheriv","createHmac","createHash","QuickCrypto","Hmac","Hash"],"mappings":"AAAA,OAAO,KAAKA,MAAZ,MAAwB,UAAxB;AACA,OAAO,KAAKC,MAAZ,MAAwB,UAAxB;AACA,SACEC,YADF,EAEEC,cAFF,EAGEC,cAHF,EAIEC,gBAJF,QAKO,UALP;AAMA,SAASC,UAAT,QAA2B,QAA3B;AACA,SAASC,UAAT,QAA2B,QAA3B;AAEA,OAAO,MAAMC,WAAW,GAAG;AACzBF,EAAAA,UADyB;AAEzBG,EAAAA,IAAI,EAAEH,UAFmB;AAGzBI,EAAAA,IAAI,EAAEH,UAHmB;AAIzBA,EAAAA,UAJyB;AAKzBL,EAAAA,YALyB;AAMzBC,EAAAA,cANyB;AAOzBC,EAAAA,cAPyB;AAQzBC,EAAAA,gBARyB;AASzB,KAAGL,MATsB;AAUzB,KAAGC;AAVsB,CAApB","sourcesContent":["import * as pbkdf2 from './pbkdf2';\nimport * as random from './random';\nimport {\n createCipher,\n createCipheriv,\n createDecipher,\n createDecipheriv,\n} from './Cipher';\nimport { createHmac } from './Hmac';\nimport { createHash } from './Hash';\n\nexport const QuickCrypto = {\n createHmac,\n Hmac: createHmac,\n Hash: createHash,\n createHash,\n createCipher,\n createCipheriv,\n createDecipher,\n createDecipheriv,\n ...pbkdf2,\n ...random,\n};\n"]}
1
+ {"version":3,"sources":["QuickCrypto.ts"],"names":["pbkdf2","random","createCipher","createCipheriv","createDecipher","createDecipheriv","publicEncrypt","publicDecrypt","privateDecrypt","generateKeyPair","generateKeyPairSync","createHmac","createHash","constants","QuickCrypto","Hmac","Hash"],"mappings":"AAAA,OAAO,KAAKA,MAAZ,MAAwB,UAAxB;AACA,OAAO,KAAKC,MAAZ,MAAwB,UAAxB;AACA,SACEC,YADF,EAEEC,cAFF,EAGEC,cAHF,EAIEC,gBAJF,EAKEC,aALF,EAMEC,aANF,EAOEC,cAPF,EAQEC,eARF,EASEC,mBATF,QAUO,UAVP;AAWA,SAASC,UAAT,QAA2B,QAA3B;AACA,SAASC,UAAT,QAA2B,QAA3B;AACA,SAASC,SAAT,QAA0B,aAA1B;AAEA,OAAO,MAAMC,WAAW,GAAG;AACzBH,EAAAA,UADyB;AAEzBI,EAAAA,IAAI,EAAEJ,UAFmB;AAGzBK,EAAAA,IAAI,EAAEJ,UAHmB;AAIzBA,EAAAA,UAJyB;AAKzBV,EAAAA,YALyB;AAMzBC,EAAAA,cANyB;AAOzBC,EAAAA,cAPyB;AAQzBC,EAAAA,gBARyB;AASzBC,EAAAA,aATyB;AAUzBC,EAAAA,aAVyB;AAWzBC,EAAAA,cAXyB;AAYzBC,EAAAA,eAZyB;AAazBC,EAAAA,mBAbyB;AAczBG,EAAAA,SAdyB;AAezB,KAAGb,MAfsB;AAgBzB,KAAGC;AAhBsB,CAApB","sourcesContent":["import * as pbkdf2 from './pbkdf2';\nimport * as random from './random';\nimport {\n createCipher,\n createCipheriv,\n createDecipher,\n createDecipheriv,\n publicEncrypt,\n publicDecrypt,\n privateDecrypt,\n generateKeyPair,\n generateKeyPairSync,\n} from './Cipher';\nimport { createHmac } from './Hmac';\nimport { createHash } from './Hash';\nimport { constants } from './constants';\n\nexport const QuickCrypto = {\n createHmac,\n Hmac: createHmac,\n Hash: createHash,\n createHash,\n createCipher,\n createCipheriv,\n createDecipher,\n createDecipheriv,\n publicEncrypt,\n publicDecrypt,\n privateDecrypt,\n generateKeyPair,\n generateKeyPairSync,\n constants,\n ...pbkdf2,\n ...random,\n};\n"]}
@@ -6,7 +6,9 @@ export function setDefaultEncoding(encoding) {
6
6
  }
7
7
  export function getDefaultEncoding() {
8
8
  return defaultEncoding;
9
- } // function slowCases(enc: string) {
9
+ }
10
+ export const kEmptyObject = Object.freeze(Object.create(null)); // Should be used by Cipher (or any other module that requires valid encodings)
11
+ // function slowCases(enc: string) {
10
12
  // switch (enc.length) {
11
13
  // case 4:
12
14
  // if (enc === 'UTF8') return 'utf8';
@@ -128,4 +130,68 @@ export function ab2str(buf) {
128
130
  let encoding = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'hex';
129
131
  return Buffer.from(buf).toString(encoding);
130
132
  }
133
+ export function validateString(str, name) {
134
+ const isString = typeof str === 'string';
135
+
136
+ if (isString) {
137
+ throw new Error(`${name} is not a string`);
138
+ }
139
+
140
+ return isString;
141
+ }
142
+ export function validateFunction(f) {
143
+ return f != null && typeof f === 'function';
144
+ }
145
+ export function isStringOrBuffer(val) {
146
+ return typeof val === 'string' || ArrayBuffer.isView(val);
147
+ }
148
+ export function validateObject(value, name, options) {
149
+ const useDefaultOptions = options == null;
150
+ const allowArray = useDefaultOptions ? false : options.allowArray;
151
+ const allowFunction = useDefaultOptions ? false : options.allowFunction;
152
+ const nullable = useDefaultOptions ? false : options.nullable;
153
+
154
+ if (!nullable && value === null || !allowArray && Array.isArray(value) || typeof value !== 'object' && (!allowFunction || typeof value !== 'function')) {
155
+ throw new Error(`${name} is not a valid object $${value}`);
156
+ }
157
+
158
+ return true;
159
+ }
160
+ export function validateInt32(value, name) {
161
+ let min = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : -2147483648;
162
+ let max = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 2147483647;
163
+
164
+ // The defaults for min and max correspond to the limits of 32-bit integers.
165
+ if (typeof value !== 'number') {
166
+ throw new Error(`Invalid argument - ${name} is not a number: ${value}`);
167
+ }
168
+
169
+ if (!Number.isInteger(value)) {
170
+ throw new Error(`Argument out of range - ${name} out of integer range: ${value}`);
171
+ }
172
+
173
+ if (value < min || value > max) {
174
+ throw new Error(`Invalid argument - ${name} out of range >= ${min} && <= ${max}: ${value}`);
175
+ }
176
+ }
177
+ export function validateUint32(value, name, positive) {
178
+ if (typeof value !== 'number') {
179
+ // throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
180
+ throw new Error(`Invalid argument - ${name} is not a number: ${value}`);
181
+ }
182
+
183
+ if (!Number.isInteger(value)) {
184
+ // throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
185
+ throw new Error(`Argument out of range - ${name} out of integer range: ${value}`);
186
+ }
187
+
188
+ const min = positive ? 1 : 0; // 2 ** 32 === 4294967296
189
+
190
+ const max = 4294967295;
191
+
192
+ if (value < min || value > max) {
193
+ // throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
194
+ throw new Error(`Invalid argument - ${name} out of range >= ${min} && <= ${max}: ${value}`);
195
+ }
196
+ }
131
197
  //# sourceMappingURL=Utils.js.map