react-native-quick-crypto 0.3.0 → 0.3.1
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.
- package/README.md +15 -8
- package/lib/commonjs/@types/stream-browserify.d.js +2 -0
- package/lib/commonjs/@types/stream-browserify.d.js.map +1 -0
- package/lib/commonjs/Cipher.js +3 -2
- package/lib/commonjs/Cipher.js.map +1 -1
- package/lib/commonjs/Hash.js +2 -2
- package/lib/commonjs/Hash.js.map +1 -1
- package/lib/commonjs/Hmac.js +2 -2
- package/lib/commonjs/Hmac.js.map +1 -1
- package/lib/commonjs/index.js +16 -13
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/@types/stream-browserify.d.js +2 -0
- package/lib/module/@types/stream-browserify.d.js.map +1 -0
- package/lib/module/Cipher.js +2 -1
- package/lib/module/Cipher.js.map +1 -1
- package/lib/module/Hash.js +1 -1
- package/lib/module/Hash.js.map +1 -1
- package/lib/module/Hmac.js +1 -1
- package/lib/module/Hmac.js.map +1 -1
- package/lib/module/index.js +16 -3
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/Cipher.d.ts +1 -1
- package/lib/typescript/Hash.d.ts +1 -1
- package/lib/typescript/Hmac.d.ts +1 -1
- package/lib/typescript/index.d.ts +2 -3
- package/package.json +3 -2
- package/src/@types/stream-browserify.d.ts +4 -0
- package/src/Cipher.ts +2 -1
- package/src/Hash.ts +1 -1
- package/src/Hmac.ts +1 -1
- package/src/index.ts +11 -2
package/README.md
CHANGED
|
@@ -62,18 +62,25 @@ expo prebuild
|
|
|
62
62
|
|
|
63
63
|
## Usage
|
|
64
64
|
|
|
65
|
-
In your `
|
|
65
|
+
In your `babel.config.js`, add a module resolver to replace `crypto` with `react-native-quick-crypto`:
|
|
66
66
|
|
|
67
67
|
```diff
|
|
68
68
|
+const path = require('path');
|
|
69
69
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
+
|
|
74
|
-
+
|
|
75
|
-
+
|
|
76
|
-
|
|
70
|
+
module.exports = {
|
|
71
|
+
presets: ['module:metro-react-native-babel-preset'],
|
|
72
|
+
plugins: [
|
|
73
|
+
+ [
|
|
74
|
+
+ 'module-resolver',
|
|
75
|
+
+ {
|
|
76
|
+
+ alias: {
|
|
77
|
+
+ 'crypto': 'react-native-quick-crypto',
|
|
78
|
+
+ },
|
|
79
|
+
+ },
|
|
80
|
+
+ ],
|
|
81
|
+
...
|
|
82
|
+
],
|
|
83
|
+
};
|
|
77
84
|
```
|
|
78
85
|
|
|
79
86
|
Now, all imports for `crypto` will be resolved as `react-native-quick-crypto` instead.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","sourcesContent":[]}
|
package/lib/commonjs/Cipher.js
CHANGED
|
@@ -13,7 +13,7 @@ exports.publicEncrypt = exports.publicDecrypt = exports.privateDecrypt = void 0;
|
|
|
13
13
|
|
|
14
14
|
var _NativeQuickCrypto = require("./NativeQuickCrypto/NativeQuickCrypto");
|
|
15
15
|
|
|
16
|
-
var
|
|
16
|
+
var _streamBrowserify = _interopRequireDefault(require("stream-browserify"));
|
|
17
17
|
|
|
18
18
|
var _Utils = require("./Utils");
|
|
19
19
|
|
|
@@ -101,7 +101,7 @@ function getDecoder(decoder, encoding) {
|
|
|
101
101
|
return decoder !== null && decoder !== void 0 ? decoder : new _string_decoder.StringDecoder(encoding);
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
class CipherCommon extends
|
|
104
|
+
class CipherCommon extends _streamBrowserify.default.Transform {
|
|
105
105
|
constructor(cipherType, cipherKey, isCipher) {
|
|
106
106
|
let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
107
107
|
let iv = arguments.length > 4 ? arguments[4] : undefined;
|
|
@@ -591,6 +591,7 @@ function generateKeyPair(type, options, callback) {
|
|
|
591
591
|
}
|
|
592
592
|
|
|
593
593
|
function generateKeyPairSync(type, options) {
|
|
594
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
594
595
|
const [_, publicKey, privateKey] = internalGenerateKeyPair(false, type, options, undefined);
|
|
595
596
|
return {
|
|
596
597
|
publicKey,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["Cipher.ts"],"names":["global","process","nextTick","setImmediate","createInternalCipher","NativeQuickCrypto","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","StringDecoder","CipherCommon","Stream","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","SBuffer","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","Buffer","constants","RSA_PKCS1_OAEP_PADDING","RSA_PKCS1_PADDING","parseKeyEncoding","kEmptyObject","publicKeyEncoding","privateKeyEncoding","publicFormat","publicType","privateFormat","privateType","cipher","internalGenerateKeyPair","isAsync","modulusLength","publicExponent","generateKeyPair","RSAKeyVariant","kKeyVariantRSA_SSA_PKCS1_v1_5","then","err","publicKey","privateKey","catch","generateKeyPairSync","hash","mgf1Hash","hashAlgorithm","mgf1HashAlgorithm","saltLength","kKeyVariantRSA_PSS","_"],"mappings":";;;;;;;;;;;;;AACA;;AACA;;AACA;;AAaA;;AAcA;;AACA;;AACA;;AACA;;AACA;;;;;;AAOA;AACAA,MAAM,CAACC,OAAP,CAAeC,QAAf,GAA0BC,YAA1B;AAEA,MAAMC,oBAAoB,GAAGC,qCAAkBC,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AAInG,KAAJ,CAAW,qCAAoCiG,QAAS,EAAxD,CAAN;AACD;AACF;;AAED,eAAO/G,qCAAkB6G,mBAAlB,CACLP,sBAAca,kBADT,EAELhB,aAFK,EAGLC,cAHK,EAILY,aAAa,IAAIF,IAJZ,EAKLG,iBAAiB,IAAIF,QALhB,EAMLG,UANK,EAOL,GAAG7F,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,6BAA4B+D,IAAK,4FAD9B,CAAN;AAGD,C,CAED;;;AAUO,SAASwB,eAAT,CACLxB,IADK,EAELlE,OAFK,EAGL6C,QAHK,EAIL;AACA,MAAI,OAAO7C,OAAP,KAAmB,UAAvB,EAAmC;AACjC6C,IAAAA,QAAQ,GAAG7C,OAAX;AACAA,IAAAA,OAAO,GAAGuE,SAAV;AACD;;AAED,+BAAiB1B,QAAjB;AAEAyC,EAAAA,uBAAuB,CAAC,IAAD,EAAOpB,IAAP,EAAalE,OAAb,EAAsB6C,QAAtB,CAAvB;AACD;;AAUM,SAASqD,mBAAT,CACLhC,IADK,EAELlE,OAFK,EAGgC;AACrC,QAAM,CAACyG,CAAD,EAAIV,SAAJ,EAAeC,UAAf,IAA6BV,uBAAuB,CACxD,KADwD,EAExDpB,IAFwD,EAGxDlE,OAHwD,EAIxDuE,SAJwD,CAA1D;AAOA,SAAO;AACLwB,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
|
+
{"version":3,"sources":["Cipher.ts"],"names":["global","process","nextTick","setImmediate","createInternalCipher","NativeQuickCrypto","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","StringDecoder","CipherCommon","Stream","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","SBuffer","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","Buffer","constants","RSA_PKCS1_OAEP_PADDING","RSA_PKCS1_PADDING","parseKeyEncoding","kEmptyObject","publicKeyEncoding","privateKeyEncoding","publicFormat","publicType","privateFormat","privateType","cipher","internalGenerateKeyPair","isAsync","modulusLength","publicExponent","generateKeyPair","RSAKeyVariant","kKeyVariantRSA_SSA_PKCS1_v1_5","then","err","publicKey","privateKey","catch","generateKeyPairSync","hash","mgf1Hash","hashAlgorithm","mgf1HashAlgorithm","saltLength","kKeyVariantRSA_PSS","_"],"mappings":";;;;;;;;;;;;;AACA;;AACA;;AACA;;AAaA;;AAcA;;AACA;;AACA;;AACA;;AACA;;;;;;AAOA;AACAA,MAAM,CAACC,OAAP,CAAeC,QAAf,GAA0BC,YAA1B;AAEA,MAAMC,oBAAoB,GAAGC,qCAAkBC,YAA/C;AACA,MAAMC,sBAAsB,GAAGF,qCAAkBG,cAAjD;AACA,MAAMC,cAAc,GAAGJ,qCAAkBK,aAAzC;AACA,MAAMC,cAAc,GAAGN,qCAAkBO,aAAzC;AACA,MAAMC,eAAe,GAAGR,qCAAkBS,cAA1C;;AAEA,SAASC,aAAT,CAAuBC,OAAvB,EAAqDC,GAArD,EAAkE;AAChE,MAAIC,KAAJ;;AACA,MAAIF,OAAO,IAAI,CAACE,KAAK,GAAGF,OAAO,CAACC,GAAD,CAAhB,KAA0B,IAAzC,EAA+C;AAC7C;AACA;AACA,QAAIC,KAAK,KAAK,CAAV,KAAgBA,KAApB,EAA2B,MAAM,IAAIC,KAAJ,CAAW,WAAUF,GA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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,6BAA4B+D,IAAK,4FAD9B,CAAN;AAGD,C,CAED;;;AAUO,SAASwB,eAAT,CACLxB,IADK,EAELlE,OAFK,EAGL6C,QAHK,EAIL;AACA,MAAI,OAAO7C,OAAP,KAAmB,UAAvB,EAAmC;AACjC6C,IAAAA,QAAQ,GAAG7C,OAAX;AACAA,IAAAA,OAAO,GAAGuE,SAAV;AACD;;AAED,+BAAiB1B,QAAjB;AAEAyC,EAAAA,uBAAuB,CAAC,IAAD,EAAOpB,IAAP,EAAalE,OAAb,EAAsB6C,QAAtB,CAAvB;AACD;;AAUM,SAASqD,mBAAT,CACLhC,IADK,EAELlE,OAFK,EAGgC;AACrC;AACA,QAAM,CAACyG,CAAD,EAAIV,SAAJ,EAAeC,UAAf,IAA6BV,uBAAuB,CACxD,KADwD,EAExDpB,IAFwD,EAGxDlE,OAHwD,EAIxDuE,SAJwD,CAA1D;AAOA,SAAO;AACLwB,IAAAA,SADK;AAELC,IAAAA;AAFK,GAAP;AAID","sourcesContent":["/* eslint-disable no-dupe-class-members */\nimport { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';\nimport Stream from 'stream-browserify';\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 // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const [_, publicKey, privateKey] = internalGenerateKeyPair(\n false,\n type,\n options,\n undefined\n )!;\n\n return {\n publicKey,\n privateKey,\n };\n}\n"]}
|
package/lib/commonjs/Hash.js
CHANGED
|
@@ -11,7 +11,7 @@ var _NativeQuickCrypto = require("./NativeQuickCrypto/NativeQuickCrypto");
|
|
|
11
11
|
|
|
12
12
|
var _Utils = require("./Utils");
|
|
13
13
|
|
|
14
|
-
var
|
|
14
|
+
var _streamBrowserify = _interopRequireDefault(require("stream-browserify"));
|
|
15
15
|
|
|
16
16
|
var _reactNativeBuffer = require("@craftzdog/react-native-buffer");
|
|
17
17
|
|
|
@@ -26,7 +26,7 @@ function createHash(algorithm, options) {
|
|
|
26
26
|
return new Hash(algorithm, options);
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
class Hash extends
|
|
29
|
+
class Hash extends _streamBrowserify.default.Transform {
|
|
30
30
|
constructor(arg, options) {
|
|
31
31
|
super(options !== null && options !== void 0 ? options : undefined);
|
|
32
32
|
|
package/lib/commonjs/Hash.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["Hash.ts"],"names":["global","process","nextTick","setImmediate","createInternalHash","NativeQuickCrypto","createHash","algorithm","options","Hash","Stream","Transform","constructor","arg","undefined","internalHash","copy","outputLength","update","data","inputEncoding","ArrayBuffer","buffer","Buffer","from","_transform","chunk","encoding","callback","_flush","push","digest","result","toString"],"mappings":";;;;;;;AACA;;AACA;;AAEA;;AACA;;AACA;;;;;;AAOAA,MAAM,CAACC,OAAP,CAAeC,QAAf,GAA0BC,YAA1B;AAEA,MAAMC,kBAAkB,GAAGC,qCAAkBC,UAA7C;;AAIO,SAASA,UAAT,CAAoBC,SAApB,EAAuCC,OAAvC,EAA8D;AACnE,SAAO,IAAIC,IAAJ,CAASF,SAAT,EAAoBC,OAApB,CAAP;AACD;;AAED,MAAMC,IAAN,SAAmBC,
|
|
1
|
+
{"version":3,"sources":["Hash.ts"],"names":["global","process","nextTick","setImmediate","createInternalHash","NativeQuickCrypto","createHash","algorithm","options","Hash","Stream","Transform","constructor","arg","undefined","internalHash","copy","outputLength","update","data","inputEncoding","ArrayBuffer","buffer","Buffer","from","_transform","chunk","encoding","callback","_flush","push","digest","result","toString"],"mappings":";;;;;;;AACA;;AACA;;AAEA;;AACA;;AACA;;;;;;AAOAA,MAAM,CAACC,OAAP,CAAeC,QAAf,GAA0BC,YAA1B;AAEA,MAAMC,kBAAkB,GAAGC,qCAAkBC,UAA7C;;AAIO,SAASA,UAAT,CAAoBC,SAApB,EAAuCC,OAAvC,EAA8D;AACnE,SAAO,IAAIC,IAAJ,CAASF,SAAT,EAAoBC,OAApB,CAAP;AACD;;AAED,MAAMC,IAAN,SAAmBC,0BAAOC,SAA1B,CAAoC;AAKlCC,EAAAA,WAAW,CAACC,GAAD,EAAqBL,OAArB,EAA4C;AACrD,UAAMA,OAAN,aAAMA,OAAN,cAAMA,OAAN,GAAiBM,SAAjB;;AADqD;;AAErD,QAAID,GAAG,YAAYJ,IAAnB,EAAyB;AACvB,WAAKM,YAAL,GAAoBF,GAAG,CAACE,YAAJ,CAAiBC,IAAjB,CAAsBR,OAAtB,aAAsBA,OAAtB,uBAAsBA,OAAO,CAAES,YAA/B,CAApB;AACD,KAFD,MAEO;AACL,WAAKF,YAAL,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sourcesContent":["/* eslint-disable no-dupe-class-members */\nimport 'react-native';\nimport { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';\nimport type { InternalHash } from './NativeQuickCrypto/hash';\nimport { Encoding, toArrayBuffer } from './Utils';\nimport Stream from 'stream-browserify';\nimport { Buffer } from '@craftzdog/react-native-buffer';\ninterface HashOptionsBase extends Stream.TransformOptions {\n outputLength?: number | undefined;\n}\n\ntype HashOptions = null | undefined | HashOptionsBase;\n\nglobal.process.nextTick = setImmediate;\n\nconst createInternalHash = NativeQuickCrypto.createHash;\n\ntype BinaryLike = ArrayBuffer;\n\nexport function createHash(algorithm: string, options?: HashOptions) {\n return new Hash(algorithm, options);\n}\n\nclass Hash extends Stream.Transform {\n private internalHash: InternalHash;\n\n constructor(other: Hash, options?: HashOptions);\n constructor(algorithm: string, options?: HashOptions);\n constructor(arg: string | Hash, options?: HashOptions) {\n super(options ?? undefined);\n if (arg instanceof Hash) {\n this.internalHash = arg.internalHash.copy(options?.outputLength);\n } else {\n this.internalHash = createInternalHash(arg, options?.outputLength);\n }\n }\n\n copy(options?: HashOptionsBase): Hash {\n const copy = new Hash(this, options);\n return copy;\n }\n /**\n * Updates the hash content with the given `data`, the encoding of which\n * is given in `inputEncoding`.\n * If `encoding` is not provided, and the `data` is a string, an\n * encoding of `'utf8'` is enforced. If `data` is a `Buffer`, `TypedArray`, or`DataView`, then `inputEncoding` is ignored.\n *\n * This can be called many times with new data as it is streamed.\n * @since v0.1.92\n * @param inputEncoding The `encoding` of the `data` string.\n */\n update(data: string | BinaryLike, inputEncoding?: Encoding): Hash {\n if (data instanceof ArrayBuffer) {\n this.internalHash.update(data);\n return this;\n }\n const buffer = Buffer.from(data, inputEncoding);\n this.internalHash.update(toArrayBuffer(buffer));\n return this;\n }\n\n _transform(\n chunk: string | BinaryLike,\n encoding: Encoding,\n callback: () => void\n ) {\n this.update(chunk, encoding);\n callback();\n }\n\n _flush(callback: () => void) {\n this.push(this.digest());\n callback();\n }\n\n /**\n * Calculates the digest of all of the data passed to be hashed (using the `hash.update()` method).\n * If `encoding` is provided a string will be returned; otherwise\n * a `Buffer` is returned.\n *\n * The `Hash` object can not be used again after `hash.digest()` method has been\n * called. Multiple calls will cause an error to be thrown.\n * @since v0.1.92\n * @param encoding The `encoding` of the return value.\n */\n digest(): Buffer;\n digest(encoding: 'buffer'): Buffer;\n digest(encoding: Encoding): string;\n digest(encoding?: Encoding | 'buffer'): string | Buffer {\n const result: ArrayBuffer = this.internalHash.digest();\n\n if (encoding && encoding !== 'buffer') {\n return Buffer.from(result).toString(encoding);\n }\n\n return Buffer.from(result);\n }\n}\n"]}
|
package/lib/commonjs/Hmac.js
CHANGED
|
@@ -9,7 +9,7 @@ var _NativeQuickCrypto = require("./NativeQuickCrypto/NativeQuickCrypto");
|
|
|
9
9
|
|
|
10
10
|
var _Utils = require("./Utils");
|
|
11
11
|
|
|
12
|
-
var
|
|
12
|
+
var _streamBrowserify = _interopRequireDefault(require("stream-browserify"));
|
|
13
13
|
|
|
14
14
|
var _reactNativeBuffer = require("@craftzdog/react-native-buffer");
|
|
15
15
|
|
|
@@ -23,7 +23,7 @@ function createHmac(algorithm, key, options) {
|
|
|
23
23
|
return new Hmac(algorithm, key, options);
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
class Hmac extends
|
|
26
|
+
class Hmac extends _streamBrowserify.default.Transform {
|
|
27
27
|
constructor(algorithm, key, _options) {
|
|
28
28
|
super();
|
|
29
29
|
|
package/lib/commonjs/Hmac.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["Hmac.ts"],"names":["createInternalHmac","NativeQuickCrypto","createHmac","algorithm","key","options","Hmac","Stream","Transform","constructor","_options","keyAsString","undefined","internalHmac","update","data","inputEncoding","ArrayBuffer","buffer","Buffer","from","_transform","chunk","encoding","callback","_flush","push","digest","result","isFinalized","toString"],"mappings":";;;;;;;AACA;;AAEA;;AAMA;;AACA;;;;;;AAEA,MAAMA,kBAAkB,GAAGC,qCAAkBC,UAA7C;;AAEO,SAASA,UAAT,CACLC,SADK,EAELC,GAFK,EAGLC,OAHK,EAIL;AACA,SAAO,IAAIC,IAAJ,CAASH,SAAT,EAAoBC,GAApB,EAAyBC,OAAzB,CAAP;AACD;;AAED,MAAMC,IAAN,SAAmBC,
|
|
1
|
+
{"version":3,"sources":["Hmac.ts"],"names":["createInternalHmac","NativeQuickCrypto","createHmac","algorithm","key","options","Hmac","Stream","Transform","constructor","_options","keyAsString","undefined","internalHmac","update","data","inputEncoding","ArrayBuffer","buffer","Buffer","from","_transform","chunk","encoding","callback","_flush","push","digest","result","isFinalized","toString"],"mappings":";;;;;;;AACA;;AAEA;;AAMA;;AACA;;;;;;AAEA,MAAMA,kBAAkB,GAAGC,qCAAkBC,UAA7C;;AAEO,SAASA,UAAT,CACLC,SADK,EAELC,GAFK,EAGLC,OAHK,EAIL;AACA,SAAO,IAAIC,IAAJ,CAASH,SAAT,EAAoBC,GAApB,EAAyBC,OAAzB,CAAP;AACD;;AAED,MAAMC,IAAN,SAAmBC,0BAAOC,SAA1B,CAAoC;AAIlCC,EAAAA,WAAW,CACTN,SADS,EAETC,GAFS,EAGTM,QAHS,EAIT;AACA;;AADA;;AAAA,yCAN6B,KAM7B;;AAEA,QAAIC,WAAW,GAAG,oCAAwBP,GAAxB,CAAlB;;AAEA,QAAIO,WAAW,KAAKC,SAApB,EAA+B;AAC7B,YAAM,gBAAN;AACD;;AAED,SAAKC,YAAL,GAAoBb,kBAAkB,CACpCG,SADoC,EAEpCQ,WAFoC,CAAtC;AAID;AAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACEG,EAAAA,MAAM,CAACC,IAAD,EAA4BC,aAA5B,EAA4D;AAChE,QAAID,IAAI,YAAYE,WAApB,EAAiC;AAC/B,WAAKJ,YAAL,CAAkBC,MAAlB,CAAyBC,IAAzB;AACA,aAAO,IAAP;AACD;;AACD,QAAI,OAAOA,IAAP,KAAgB,QAApB,EAA8B;AAC5B,YAAMG,MAAM,GAAGC,0BAAOC,IAAP,CAAYL,IAAZ,EAAkBC,aAAlB,CAAf;;AACA,WAAKH,YAAL,CAAkBC,MAAlB,CAAyB,0BAAcI,MAAd,CAAzB;AACA,aAAO,IAAP;AACD;;AAED,SAAKL,YAAL,CAAkBC,MAAlB,CAAyB,oCAAwBC,IAAxB,CAAzB;AACA,WAAO,IAAP;AACD;;AAEDM,EAAAA,UAAU,CACRC,KADQ,EAERC,QAFQ,EAGRC,QAHQ,EAIR;AACA,SAAKV,MAAL,CAAYQ,KAAZ,EAAmBC,QAAnB;AACAC,IAAAA,QAAQ;AACT;;AAEDC,EAAAA,MAAM,CAACD,QAAD,EAAuB;AAC3B,SAAKE,IAAL,CAAU,KAAKC,MAAL,EAAV;AACAH,IAAAA,QAAQ;AACT;AAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AAIEG,EAAAA,MAAM,CAACJ,QAAD,EAAkD;AACtD,UAAMK,MAAmB,GAAG,KAAKC,WAAL,GACxB,IAAIZ,WAAJ,CAAgB,CAAhB,CADwB,GAExB,KAAKJ,YAAL,CAAkBc,MAAlB,EAFJ;AAGA,SAAKE,WAAL,GAAmB,IAAnB;;AACA,QAAIN,QAAQ,IAAIA,QAAQ,KAAK,QAA7B,EAAuC;AACrC,aAAOJ,0BAAOC,IAAP,CAAYQ,MAAZ,EAAoBE,QAApB,CAA6BP,QAA7B,CAAP;AACD;;AACD,WAAOJ,0BAAOC,IAAP,CAAYQ,MAAZ,CAAP;AACD;;AAnFiC","sourcesContent":["/* eslint-disable no-dupe-class-members */\nimport { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';\nimport type { InternalHmac } from './NativeQuickCrypto/hmac';\nimport {\n Encoding,\n toArrayBuffer,\n BinaryLike,\n binaryLikeToArrayBuffer,\n} from './Utils';\nimport Stream from 'stream-browserify';\nimport { Buffer } from '@craftzdog/react-native-buffer';\n\nconst createInternalHmac = NativeQuickCrypto.createHmac;\n\nexport function createHmac(\n algorithm: string,\n key: BinaryLike,\n options?: Stream.TransformOptions\n) {\n return new Hmac(algorithm, key, options);\n}\n\nclass Hmac extends Stream.Transform {\n private internalHmac: InternalHmac;\n private isFinalized: boolean = false;\n\n constructor(\n algorithm: string,\n key: BinaryLike,\n _options?: Stream.TransformOptions\n ) {\n super();\n let keyAsString = binaryLikeToArrayBuffer(key);\n\n if (keyAsString === undefined) {\n throw 'Wrong key type';\n }\n\n this.internalHmac = createInternalHmac(\n algorithm,\n keyAsString as ArrayBuffer\n );\n }\n\n /**\n * Updates the `Hmac` content with the given `data`, the encoding of which\n * is given in `inputEncoding`.\n * If `encoding` is not provided, and the `data` is a string, an\n * encoding of `'utf8'` is enforced. If `data` is a `Buffer`, `TypedArray`, or`DataView`, then `inputEncoding` is ignored.\n *\n * This can be called many times with new data as it is streamed.\n * @since v0.1.94\n * @param inputEncoding The `encoding` of the `data` string.\n */\n update(data: string | BinaryLike, inputEncoding?: Encoding): Hmac {\n if (data instanceof ArrayBuffer) {\n this.internalHmac.update(data);\n return this;\n }\n if (typeof data === 'string') {\n const buffer = Buffer.from(data, inputEncoding);\n this.internalHmac.update(toArrayBuffer(buffer));\n return this;\n }\n\n this.internalHmac.update(binaryLikeToArrayBuffer(data));\n return this;\n }\n\n _transform(\n chunk: string | BinaryLike,\n encoding: Encoding,\n callback: () => void\n ) {\n this.update(chunk, encoding);\n callback();\n }\n\n _flush(callback: () => void) {\n this.push(this.digest());\n callback();\n }\n\n /**\n * Calculates the HMAC digest of all of the data passed using `hmac.update()`.\n * If `encoding` is\n * provided a string is returned; otherwise a `Buffer` is returned;\n *\n * The `Hmac` object can not be used again after `hmac.digest()` has been\n * called. Multiple calls to `hmac.digest()` will result in an error being thrown.\n * @since v0.1.94\n * @param encoding The `encoding` of the return value.\n */\n digest(): Buffer;\n digest(encoding: 'buffer'): Buffer;\n digest(encoding: Encoding): string;\n digest(encoding?: Encoding | 'buffer'): string | Buffer {\n const result: ArrayBuffer = this.isFinalized\n ? new ArrayBuffer(0)\n : this.internalHmac.digest();\n this.isFinalized = true;\n if (encoding && encoding !== 'buffer') {\n return Buffer.from(result).toString(encoding);\n }\n return Buffer.from(result);\n }\n}\n"]}
|
package/lib/commonjs/index.js
CHANGED
|
@@ -3,22 +3,25 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
var _exportNames = {};
|
|
7
6
|
exports.default = void 0;
|
|
8
7
|
|
|
8
|
+
var _reactNativeBuffer = require("@craftzdog/react-native-buffer");
|
|
9
|
+
|
|
9
10
|
var _QuickCrypto = require("./QuickCrypto");
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
12
|
+
// @ts-expect-error
|
|
13
|
+
global.Buffer = _reactNativeBuffer.Buffer; // @ts-expect-error
|
|
14
|
+
|
|
15
|
+
global.crypto = _QuickCrypto.QuickCrypto; // for randombytes https://github.com/crypto-browserify/randombytes/blob/master/browser.js#L16
|
|
16
|
+
|
|
17
|
+
const fallbackCrypto = require('crypto-browserify');
|
|
18
|
+
|
|
19
|
+
const crypto = { ...fallbackCrypto,
|
|
20
|
+
..._QuickCrypto.QuickCrypto
|
|
21
|
+
}; // Maybe use proxy to not load everything?
|
|
22
|
+
|
|
23
|
+
global.crypto = crypto;
|
|
24
|
+
module.exports = crypto;
|
|
25
|
+
var _default = crypto;
|
|
23
26
|
exports.default = _default;
|
|
24
27
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["index.ts"],"names":["QuickCrypto"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["index.ts"],"names":["global","Buffer","crypto","QuickCrypto","fallbackCrypto","require","module","exports"],"mappings":";;;;;;;AAAA;;AACA;;AAEA;AACAA,MAAM,CAACC,MAAP,GAAgBA,yBAAhB,C,CACA;;AACAD,MAAM,CAACE,MAAP,GAAgBC,wBAAhB,C,CAA6B;;AAE7B,MAAMC,cAAc,GAAGC,OAAO,CAAC,mBAAD,CAA9B;;AACA,MAAMH,MAAM,GAAG,EAAE,GAAGE,cAAL;AAAqB,KAAGD;AAAxB,CAAf,C,CAAsD;;AACtDH,MAAM,CAACE,MAAP,GAAgBA,MAAhB;AAEAI,MAAM,CAACC,OAAP,GAAiBL,MAAjB;eACeA,M","sourcesContent":["import { Buffer } from '@craftzdog/react-native-buffer';\nimport { QuickCrypto } from './QuickCrypto';\n\n// @ts-expect-error\nglobal.Buffer = Buffer;\n// @ts-expect-error\nglobal.crypto = QuickCrypto; // for randombytes https://github.com/crypto-browserify/randombytes/blob/master/browser.js#L16\n\nconst fallbackCrypto = require('crypto-browserify');\nconst crypto = { ...fallbackCrypto, ...QuickCrypto }; // Maybe use proxy to not load everything?\nglobal.crypto = crypto;\n\nmodule.exports = crypto;\nexport default crypto;\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","sourcesContent":[]}
|
package/lib/module/Cipher.js
CHANGED
|
@@ -2,7 +2,7 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
|
|
|
2
2
|
|
|
3
3
|
/* eslint-disable no-dupe-class-members */
|
|
4
4
|
import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';
|
|
5
|
-
import Stream from 'stream';
|
|
5
|
+
import Stream from 'stream-browserify';
|
|
6
6
|
import { binaryLikeToArrayBuffer, getDefaultEncoding, kEmptyObject, validateFunction, validateObject, validateString, validateUint32, validateInt32 } from './Utils';
|
|
7
7
|
import { RSAKeyVariant } from './NativeQuickCrypto/Cipher'; // TODO(osp) re-enable type specific constructors
|
|
8
8
|
// They are nice to have but not absolutely necessary
|
|
@@ -571,6 +571,7 @@ export function generateKeyPair(type, options, callback) {
|
|
|
571
571
|
internalGenerateKeyPair(true, type, options, callback);
|
|
572
572
|
}
|
|
573
573
|
export function generateKeyPairSync(type, options) {
|
|
574
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
574
575
|
const [_, publicKey, privateKey] = internalGenerateKeyPair(false, type, options, undefined);
|
|
575
576
|
return {
|
|
576
577
|
publicKey,
|
package/lib/module/Cipher.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,QALP,C,CAOA;;AACAC,MAAM,CAACC,OAAP,CAAeC,QAAf,GAA0BC,YAA1B;AAEA,MAAMC,oBAAoB,GAAGvB,iBAAiB,CAACwB,YAA/C;AACA,MAAMC,sBAAsB,GAAGzB,iBAAiB,CAAC0B,cAAjD;AACA,MAAMC,cAAc,GAAG3B,iBAAiB,CAAC4B,aAAzC;AACA,MAAMC,cAAc,GAAG7B,iBAAiB,CAAC8B,aAAzC;AACA,MAAMC,eAAe,GAAG/B,iBAAiB,CAACgC,cAA1C;;AAEA,SAASC,aAAT,CAAuBC,OAAvB,EAAqDC,GAArD,EAAkE;AAChE,MAAIC,KAAJ;;AACA,MAAIF,OAAO,IAAI,CAACE,KAAK,GAAGF,OAAO,CAACC,GAAD,CAAhB,KAA0B,IAAzC,EAA+C;AAC7C;AACA;AACA,QAAIC,KAAK,KAAK,CAAV,KAAgBA,KAApB,EAA2B,MAAM,IAAIC,KAAJ,CAAW,WAAUF,GAAI,KAAIC,KAAM,EAAnC,CAAN;AAC3B,WAAO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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
|
+
{"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,mBAAnB;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,QALP,C,CAOA;;AACAC,MAAM,CAACC,OAAP,CAAeC,QAAf,GAA0BC,YAA1B;AAEA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,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;AACA,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-browserify';\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 // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const [_, publicKey, privateKey] = internalGenerateKeyPair(\n false,\n type,\n options,\n undefined\n )!;\n\n return {\n publicKey,\n privateKey,\n };\n}\n"]}
|
package/lib/module/Hash.js
CHANGED
|
@@ -4,7 +4,7 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
|
|
|
4
4
|
import 'react-native';
|
|
5
5
|
import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';
|
|
6
6
|
import { toArrayBuffer } from './Utils';
|
|
7
|
-
import Stream from 'stream';
|
|
7
|
+
import Stream from 'stream-browserify';
|
|
8
8
|
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
9
9
|
global.process.nextTick = setImmediate;
|
|
10
10
|
const createInternalHash = NativeQuickCrypto.createHash;
|
package/lib/module/Hash.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["Hash.ts"],"names":["NativeQuickCrypto","toArrayBuffer","Stream","Buffer","global","process","nextTick","setImmediate","createInternalHash","createHash","algorithm","options","Hash","Transform","constructor","arg","undefined","internalHash","copy","outputLength","update","data","inputEncoding","ArrayBuffer","buffer","from","_transform","chunk","encoding","callback","_flush","push","digest","result","toString"],"mappings":";;AAAA;AACA,OAAO,cAAP;AACA,SAASA,iBAAT,QAAkC,uCAAlC;AAEA,SAAmBC,aAAnB,QAAwC,SAAxC;AACA,OAAOC,MAAP,MAAmB,
|
|
1
|
+
{"version":3,"sources":["Hash.ts"],"names":["NativeQuickCrypto","toArrayBuffer","Stream","Buffer","global","process","nextTick","setImmediate","createInternalHash","createHash","algorithm","options","Hash","Transform","constructor","arg","undefined","internalHash","copy","outputLength","update","data","inputEncoding","ArrayBuffer","buffer","from","_transform","chunk","encoding","callback","_flush","push","digest","result","toString"],"mappings":";;AAAA;AACA,OAAO,cAA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rB,MAAM,CAACsB,IAAP,CAAYJ,IAAZ,EAAkBC,aAAlB,CAAf;AACA,SAAKL,YAAL,CAAkBG,MAAlB,CAAyBnB,aAAa,CAACuB,MAAD,CAAtC;AACA,WAAO,IAAP;AACD;;AAEDE,EAAAA,UAAU,CACRC,KADQ,EAERC,QAFQ,EAGRC,QAHQ,EAIR;AACA,SAAKT,MAAL,CAAYO,KAAZ,EAAmBC,QAAnB;AACAC,IAAAA,QAAQ;AACT;;AAEDC,EAAAA,MAAM,CAACD,QAAD,EAAuB;AAC3B,SAAKE,IAAL,CAAU,KAAKC,MAAL,EAAV;AACAH,IAAAA,QAAQ;AACT;AAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AAIEG,EAAAA,MAAM,CAACJ,QAAD,EAAkD;AACtD,UAAMK,MAAmB,GAAG,KAAKhB,YAAL,CAAkBe,MAAlB,EAA5B;;AAEA,QAAIJ,QAAQ,IAAIA,QAAQ,KAAK,QAA7B,EAAuC;AACrC,aAAOzB,MAAM,CAACsB,IAAP,CAAYQ,MAAZ,EAAoBC,QAApB,CAA6BN,QAA7B,CAAP;AACD;;AAED,WAAOzB,MAAM,CAACsB,IAAP,CAAYQ,MAAZ,CAAP;AACD;;AAzEiC","sourcesContent":["/* eslint-disable no-dupe-class-members */\nimport 'react-native';\nimport { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';\nimport type { InternalHash } from './NativeQuickCrypto/hash';\nimport { Encoding, toArrayBuffer } from './Utils';\nimport Stream from 'stream-browserify';\nimport { Buffer } from '@craftzdog/react-native-buffer';\ninterface HashOptionsBase extends Stream.TransformOptions {\n outputLength?: number | undefined;\n}\n\ntype HashOptions = null | undefined | HashOptionsBase;\n\nglobal.process.nextTick = setImmediate;\n\nconst createInternalHash = NativeQuickCrypto.createHash;\n\ntype BinaryLike = ArrayBuffer;\n\nexport function createHash(algorithm: string, options?: HashOptions) {\n return new Hash(algorithm, options);\n}\n\nclass Hash extends Stream.Transform {\n private internalHash: InternalHash;\n\n constructor(other: Hash, options?: HashOptions);\n constructor(algorithm: string, options?: HashOptions);\n constructor(arg: string | Hash, options?: HashOptions) {\n super(options ?? undefined);\n if (arg instanceof Hash) {\n this.internalHash = arg.internalHash.copy(options?.outputLength);\n } else {\n this.internalHash = createInternalHash(arg, options?.outputLength);\n }\n }\n\n copy(options?: HashOptionsBase): Hash {\n const copy = new Hash(this, options);\n return copy;\n }\n /**\n * Updates the hash content with the given `data`, the encoding of which\n * is given in `inputEncoding`.\n * If `encoding` is not provided, and the `data` is a string, an\n * encoding of `'utf8'` is enforced. If `data` is a `Buffer`, `TypedArray`, or`DataView`, then `inputEncoding` is ignored.\n *\n * This can be called many times with new data as it is streamed.\n * @since v0.1.92\n * @param inputEncoding The `encoding` of the `data` string.\n */\n update(data: string | BinaryLike, inputEncoding?: Encoding): Hash {\n if (data instanceof ArrayBuffer) {\n this.internalHash.update(data);\n return this;\n }\n const buffer = Buffer.from(data, inputEncoding);\n this.internalHash.update(toArrayBuffer(buffer));\n return this;\n }\n\n _transform(\n chunk: string | BinaryLike,\n encoding: Encoding,\n callback: () => void\n ) {\n this.update(chunk, encoding);\n callback();\n }\n\n _flush(callback: () => void) {\n this.push(this.digest());\n callback();\n }\n\n /**\n * Calculates the digest of all of the data passed to be hashed (using the `hash.update()` method).\n * If `encoding` is provided a string will be returned; otherwise\n * a `Buffer` is returned.\n *\n * The `Hash` object can not be used again after `hash.digest()` method has been\n * called. Multiple calls will cause an error to be thrown.\n * @since v0.1.92\n * @param encoding The `encoding` of the return value.\n */\n digest(): Buffer;\n digest(encoding: 'buffer'): Buffer;\n digest(encoding: Encoding): string;\n digest(encoding?: Encoding | 'buffer'): string | Buffer {\n const result: ArrayBuffer = this.internalHash.digest();\n\n if (encoding && encoding !== 'buffer') {\n return Buffer.from(result).toString(encoding);\n }\n\n return Buffer.from(result);\n }\n}\n"]}
|
package/lib/module/Hmac.js
CHANGED
|
@@ -3,7 +3,7 @@ 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 { toArrayBuffer, binaryLikeToArrayBuffer } from './Utils';
|
|
6
|
-
import Stream from 'stream';
|
|
6
|
+
import Stream from 'stream-browserify';
|
|
7
7
|
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
8
8
|
const createInternalHmac = NativeQuickCrypto.createHmac;
|
|
9
9
|
export function createHmac(algorithm, key, options) {
|
package/lib/module/Hmac.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["Hmac.ts"],"names":["NativeQuickCrypto","toArrayBuffer","binaryLikeToArrayBuffer","Stream","Buffer","createInternalHmac","createHmac","algorithm","key","options","Hmac","Transform","constructor","_options","keyAsString","undefined","internalHmac","update","data","inputEncoding","ArrayBuffer","buffer","from","_transform","chunk","encoding","callback","_flush","push","digest","result","isFinalized","toString"],"mappings":";;AAAA;AACA,SAASA,iBAAT,QAAkC,uCAAlC;AAEA,SAEEC,aAFF,EAIEC,uBAJF,QAKO,SALP;AAMA,OAAOC,MAAP,MAAmB,
|
|
1
|
+
{"version":3,"sources":["Hmac.ts"],"names":["NativeQuickCrypto","toArrayBuffer","binaryLikeToArrayBuffer","Stream","Buffer","createInternalHmac","createHmac","algorithm","key","options","Hmac","Transform","constructor","_options","keyAsString","undefined","internalHmac","update","data","inputEncoding","ArrayBuffer","buffer","from","_transform","chunk","encoding","callback","_flush","push","digest","result","isFinalized","toString"],"mappings":";;AAAA;AACA,SAASA,iBAAT,QAAkC,uCAAlC;AAEA,SAEEC,aAFF,EAIEC,uBAJF,QAKO,SALP;AAMA,OAAOC,MAAP,MAAmB,mBAAnB;AACA,SAASC,MAAT,QAAuB,gCAAvB;AAEA,MAAMC,kBAAkB,GAAGL,iBAAiB,CAACM,UAA7C;AAEA,OAAO,SAASA,UAAT,CACLC,SADK,E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AA,QAAQ;AACT;AAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AAIEG,EAAAA,MAAM,CAACJ,QAAD,EAAkD;AACtD,UAAMK,MAAmB,GAAG,KAAKC,WAAL,GACxB,IAAIX,WAAJ,CAAgB,CAAhB,CADwB,GAExB,KAAKJ,YAAL,CAAkBa,MAAlB,EAFJ;AAGA,SAAKE,WAAL,GAAmB,IAAnB;;AACA,QAAIN,QAAQ,IAAIA,QAAQ,KAAK,QAA7B,EAAuC;AACrC,aAAOrB,MAAM,CAACkB,IAAP,CAAYQ,MAAZ,EAAoBE,QAApB,CAA6BP,QAA7B,CAAP;AACD;;AACD,WAAOrB,MAAM,CAACkB,IAAP,CAAYQ,MAAZ,CAAP;AACD;;AAnFiC","sourcesContent":["/* eslint-disable no-dupe-class-members */\nimport { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';\nimport type { InternalHmac } from './NativeQuickCrypto/hmac';\nimport {\n Encoding,\n toArrayBuffer,\n BinaryLike,\n binaryLikeToArrayBuffer,\n} from './Utils';\nimport Stream from 'stream-browserify';\nimport { Buffer } from '@craftzdog/react-native-buffer';\n\nconst createInternalHmac = NativeQuickCrypto.createHmac;\n\nexport function createHmac(\n algorithm: string,\n key: BinaryLike,\n options?: Stream.TransformOptions\n) {\n return new Hmac(algorithm, key, options);\n}\n\nclass Hmac extends Stream.Transform {\n private internalHmac: InternalHmac;\n private isFinalized: boolean = false;\n\n constructor(\n algorithm: string,\n key: BinaryLike,\n _options?: Stream.TransformOptions\n ) {\n super();\n let keyAsString = binaryLikeToArrayBuffer(key);\n\n if (keyAsString === undefined) {\n throw 'Wrong key type';\n }\n\n this.internalHmac = createInternalHmac(\n algorithm,\n keyAsString as ArrayBuffer\n );\n }\n\n /**\n * Updates the `Hmac` content with the given `data`, the encoding of which\n * is given in `inputEncoding`.\n * If `encoding` is not provided, and the `data` is a string, an\n * encoding of `'utf8'` is enforced. If `data` is a `Buffer`, `TypedArray`, or`DataView`, then `inputEncoding` is ignored.\n *\n * This can be called many times with new data as it is streamed.\n * @since v0.1.94\n * @param inputEncoding The `encoding` of the `data` string.\n */\n update(data: string | BinaryLike, inputEncoding?: Encoding): Hmac {\n if (data instanceof ArrayBuffer) {\n this.internalHmac.update(data);\n return this;\n }\n if (typeof data === 'string') {\n const buffer = Buffer.from(data, inputEncoding);\n this.internalHmac.update(toArrayBuffer(buffer));\n return this;\n }\n\n this.internalHmac.update(binaryLikeToArrayBuffer(data));\n return this;\n }\n\n _transform(\n chunk: string | BinaryLike,\n encoding: Encoding,\n callback: () => void\n ) {\n this.update(chunk, encoding);\n callback();\n }\n\n _flush(callback: () => void) {\n this.push(this.digest());\n callback();\n }\n\n /**\n * Calculates the HMAC digest of all of the data passed using `hmac.update()`.\n * If `encoding` is\n * provided a string is returned; otherwise a `Buffer` is returned;\n *\n * The `Hmac` object can not be used again after `hmac.digest()` has been\n * called. Multiple calls to `hmac.digest()` will result in an error being thrown.\n * @since v0.1.94\n * @param encoding The `encoding` of the return value.\n */\n digest(): Buffer;\n digest(encoding: 'buffer'): Buffer;\n digest(encoding: Encoding): string;\n digest(encoding?: Encoding | 'buffer'): string | Buffer {\n const result: ArrayBuffer = this.isFinalized\n ? new ArrayBuffer(0)\n : this.internalHmac.digest();\n this.isFinalized = true;\n if (encoding && encoding !== 'buffer') {\n return Buffer.from(result).toString(encoding);\n }\n return Buffer.from(result);\n }\n}\n"]}
|
package/lib/module/index.js
CHANGED
|
@@ -1,4 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
2
|
+
import { QuickCrypto } from './QuickCrypto'; // @ts-expect-error
|
|
3
|
+
|
|
4
|
+
global.Buffer = Buffer; // @ts-expect-error
|
|
5
|
+
|
|
6
|
+
global.crypto = QuickCrypto; // for randombytes https://github.com/crypto-browserify/randombytes/blob/master/browser.js#L16
|
|
7
|
+
|
|
8
|
+
const fallbackCrypto = require('crypto-browserify');
|
|
9
|
+
|
|
10
|
+
const crypto = { ...fallbackCrypto,
|
|
11
|
+
...QuickCrypto
|
|
12
|
+
}; // Maybe use proxy to not load everything?
|
|
13
|
+
|
|
14
|
+
global.crypto = crypto;
|
|
15
|
+
module.exports = crypto;
|
|
16
|
+
export default crypto;
|
|
4
17
|
//# sourceMappingURL=index.js.map
|
package/lib/module/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["index.ts"],"names":["QuickCrypto"],"mappings":"AAAA,SAASA,WAAT,QAA4B,eAA5B
|
|
1
|
+
{"version":3,"sources":["index.ts"],"names":["Buffer","QuickCrypto","global","crypto","fallbackCrypto","require","module","exports"],"mappings":"AAAA,SAASA,MAAT,QAAuB,gCAAvB;AACA,SAASC,WAAT,QAA4B,eAA5B,C,CAEA;;AACAC,MAAM,CAACF,MAAP,GAAgBA,MAAhB,C,CACA;;AACAE,MAAM,CAACC,MAAP,GAAgBF,WAAhB,C,CAA6B;;AAE7B,MAAMG,cAAc,GAAGC,OAAO,CAAC,mBAAD,CAA9B;;AACA,MAAMF,MAAM,GAAG,EAAE,GAAGC,cAAL;AAAqB,KAAGH;AAAxB,CAAf,C,CAAsD;;AACtDC,MAAM,CAACC,MAAP,GAAgBA,MAAhB;AAEAG,MAAM,CAACC,OAAP,GAAiBJ,MAAjB;AACA,eAAeA,MAAf","sourcesContent":["import { Buffer } from '@craftzdog/react-native-buffer';\nimport { QuickCrypto } from './QuickCrypto';\n\n// @ts-expect-error\nglobal.Buffer = Buffer;\n// @ts-expect-error\nglobal.crypto = QuickCrypto; // for randombytes https://github.com/crypto-browserify/randombytes/blob/master/browser.js#L16\n\nconst fallbackCrypto = require('crypto-browserify');\nconst crypto = { ...fallbackCrypto, ...QuickCrypto }; // Maybe use proxy to not load everything?\nglobal.crypto = crypto;\n\nmodule.exports = crypto;\nexport default crypto;\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
|
-
import Stream from 'stream';
|
|
3
|
+
import Stream from 'stream-browserify';
|
|
4
4
|
import { BinaryLike, CipherEncoding, Encoding } from './Utils';
|
|
5
5
|
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
6
6
|
declare class CipherCommon extends Stream.Transform {
|
package/lib/typescript/Hash.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import 'react-native';
|
|
3
3
|
import { Encoding } from './Utils';
|
|
4
|
-
import Stream from 'stream';
|
|
4
|
+
import Stream from 'stream-browserify';
|
|
5
5
|
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
6
6
|
interface HashOptionsBase extends Stream.TransformOptions {
|
|
7
7
|
outputLength?: number | undefined;
|
package/lib/typescript/Hmac.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Encoding, BinaryLike } from './Utils';
|
|
3
|
-
import Stream from 'stream';
|
|
3
|
+
import Stream from 'stream-browserify';
|
|
4
4
|
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
5
5
|
export declare function createHmac(algorithm: string, key: BinaryLike, options?: Stream.TransformOptions): Hmac;
|
|
6
6
|
declare class Hmac extends Stream.Transform {
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
export default QuickCrypto;
|
|
1
|
+
declare const crypto: any;
|
|
2
|
+
export default crypto;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-quick-crypto",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
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",
|
|
@@ -163,6 +163,7 @@
|
|
|
163
163
|
"react-native-quick-base64": "^2.0.2",
|
|
164
164
|
"stream": "^0.0.2",
|
|
165
165
|
"stream-browserify": "^3.0.0",
|
|
166
|
-
"string_decoder": "^1.3.0"
|
|
166
|
+
"string_decoder": "^1.3.0",
|
|
167
|
+
"crypto-browserify": "^3.12.0"
|
|
167
168
|
}
|
|
168
169
|
}
|
package/src/Cipher.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable no-dupe-class-members */
|
|
2
2
|
import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';
|
|
3
|
-
import Stream from 'stream';
|
|
3
|
+
import Stream from 'stream-browserify';
|
|
4
4
|
import {
|
|
5
5
|
BinaryLike,
|
|
6
6
|
binaryLikeToArrayBuffer,
|
|
@@ -749,6 +749,7 @@ export function generateKeyPairSync(
|
|
|
749
749
|
type: string,
|
|
750
750
|
options?: GenerateKeyPairOptions
|
|
751
751
|
): { publicKey: any; privateKey: any } {
|
|
752
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
752
753
|
const [_, publicKey, privateKey] = internalGenerateKeyPair(
|
|
753
754
|
false,
|
|
754
755
|
type,
|
package/src/Hash.ts
CHANGED
|
@@ -3,7 +3,7 @@ import 'react-native';
|
|
|
3
3
|
import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';
|
|
4
4
|
import type { InternalHash } from './NativeQuickCrypto/hash';
|
|
5
5
|
import { Encoding, toArrayBuffer } from './Utils';
|
|
6
|
-
import Stream from 'stream';
|
|
6
|
+
import Stream from 'stream-browserify';
|
|
7
7
|
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
8
8
|
interface HashOptionsBase extends Stream.TransformOptions {
|
|
9
9
|
outputLength?: number | undefined;
|
package/src/Hmac.ts
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
BinaryLike,
|
|
8
8
|
binaryLikeToArrayBuffer,
|
|
9
9
|
} from './Utils';
|
|
10
|
-
import Stream from 'stream';
|
|
10
|
+
import Stream from 'stream-browserify';
|
|
11
11
|
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
12
12
|
|
|
13
13
|
const createInternalHmac = NativeQuickCrypto.createHmac;
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
|
+
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
1
2
|
import { QuickCrypto } from './QuickCrypto';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
// @ts-expect-error
|
|
5
|
+
global.Buffer = Buffer;
|
|
6
|
+
// @ts-expect-error
|
|
7
|
+
global.crypto = QuickCrypto; // for randombytes https://github.com/crypto-browserify/randombytes/blob/master/browser.js#L16
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
const fallbackCrypto = require('crypto-browserify');
|
|
10
|
+
const crypto = { ...fallbackCrypto, ...QuickCrypto }; // Maybe use proxy to not load everything?
|
|
11
|
+
global.crypto = crypto;
|
|
12
|
+
|
|
13
|
+
module.exports = crypto;
|
|
14
|
+
export default crypto;
|