react-native-quick-crypto 0.6.0 → 0.7.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -18
- package/android/CMakeLists.txt +58 -61
- package/android/build.gradle +105 -53
- package/android/gradle/wrapper/gradle-wrapper.properties +1 -1
- package/android/gradle.properties +5 -5
- package/android/src/main/{AndroidManifest.xml → AndroidManifestNew.xml} +1 -2
- package/cpp/Cipher/MGLCipherHostObject.cpp +31 -5
- package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +21 -31
- package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +4 -17
- package/cpp/Cipher/MGLPublicCipher.h +1 -1
- package/cpp/Cipher/MGLPublicCipherInstaller.h +6 -6
- package/cpp/Cipher/MGLRsa.cpp +224 -11
- package/cpp/Cipher/MGLRsa.h +13 -3
- package/cpp/Hash/MGLHashHostObject.cpp +1 -1
- package/cpp/Hash/MGLHashInstaller.cpp +2 -4
- package/cpp/JSIUtils/MGLJSIMacros.h +10 -0
- package/cpp/JSIUtils/MGLThreadAwareHostObject.h +1 -1
- package/cpp/MGLKeys.cpp +415 -471
- package/cpp/MGLKeys.h +70 -2
- package/cpp/MGLQuickCryptoHostObject.cpp +9 -0
- package/cpp/Random/MGLRandomHostObject.cpp +9 -2
- package/cpp/Sig/MGLSignHostObjects.cpp +1 -1
- package/cpp/Utils/MGLUtils.cpp +104 -32
- package/cpp/Utils/MGLUtils.h +172 -143
- package/cpp/Utils/node.h +13 -0
- package/cpp/webcrypto/MGLWebCrypto.cpp +63 -0
- package/cpp/webcrypto/MGLWebCrypto.h +34 -0
- package/cpp/webcrypto/crypto_ec.cpp +334 -0
- package/cpp/webcrypto/crypto_ec.h +65 -0
- package/ios/QuickCrypto.xcodeproj/project.pbxproj +4 -4
- package/lib/commonjs/@types/crypto-browserify.d.js.map +1 -1
- package/lib/commonjs/Cipher.js +53 -194
- package/lib/commonjs/Cipher.js.map +1 -1
- package/lib/commonjs/Hash.js +29 -29
- package/lib/commonjs/Hash.js.map +1 -1
- package/lib/commonjs/Hashnames.js +75 -0
- package/lib/commonjs/Hashnames.js.map +1 -0
- package/lib/commonjs/Hmac.js +6 -29
- package/lib/commonjs/Hmac.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/Cipher.js +3 -5
- package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js +11 -20
- package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/hash.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/hmac.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/pbkdf2.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/random.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/sig.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/webcrypto.js +6 -0
- package/lib/commonjs/NativeQuickCrypto/webcrypto.js.map +1 -0
- package/lib/commonjs/QuickCrypto.js +5 -14
- package/lib/commonjs/QuickCrypto.js.map +1 -1
- package/lib/commonjs/Utils.js +360 -48
- package/lib/commonjs/Utils.js.map +1 -1
- package/lib/commonjs/aes.js +324 -0
- package/lib/commonjs/aes.js.map +1 -0
- package/lib/commonjs/constants.js +1 -2
- package/lib/commonjs/constants.js.map +1 -1
- package/lib/commonjs/ec.js +288 -0
- package/lib/commonjs/ec.js.map +1 -0
- package/lib/commonjs/index.js +6 -10
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/keys.js +280 -48
- package/lib/commonjs/keys.js.map +1 -1
- package/lib/commonjs/pbkdf2.js +44 -18
- package/lib/commonjs/pbkdf2.js.map +1 -1
- package/lib/commonjs/random.js +49 -68
- package/lib/commonjs/random.js.map +1 -1
- package/lib/commonjs/rsa.js +329 -0
- package/lib/commonjs/rsa.js.map +1 -0
- package/lib/commonjs/sig.js +13 -54
- package/lib/commonjs/sig.js.map +1 -1
- package/lib/commonjs/subtle.js +271 -0
- package/lib/commonjs/subtle.js.map +1 -0
- package/lib/module/@types/crypto-browserify.d.js.map +1 -1
- package/lib/module/Cipher.js +53 -188
- package/lib/module/Cipher.js.map +1 -1
- package/lib/module/Hash.js +27 -21
- package/lib/module/Hash.js.map +1 -1
- package/lib/module/Hashnames.js +71 -0
- package/lib/module/Hashnames.js.map +1 -0
- package/lib/module/Hmac.js +4 -21
- package/lib/module/Hmac.js.map +1 -1
- package/lib/module/NativeQuickCrypto/Cipher.js +3 -4
- package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js +11 -17
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
- package/lib/module/NativeQuickCrypto/hash.js.map +1 -1
- package/lib/module/NativeQuickCrypto/hmac.js.map +1 -1
- package/lib/module/NativeQuickCrypto/pbkdf2.js.map +1 -1
- package/lib/module/NativeQuickCrypto/random.js.map +1 -1
- package/lib/module/NativeQuickCrypto/sig.js.map +1 -1
- package/lib/module/NativeQuickCrypto/webcrypto.js +2 -0
- package/lib/module/NativeQuickCrypto/webcrypto.js.map +1 -0
- package/lib/module/QuickCrypto.js +2 -0
- package/lib/module/QuickCrypto.js.map +1 -1
- package/lib/module/Utils.js +319 -33
- package/lib/module/Utils.js.map +1 -1
- package/lib/module/aes.js +317 -0
- package/lib/module/aes.js.map +1 -0
- package/lib/module/constants.js.map +1 -1
- package/lib/module/ec.js +282 -0
- package/lib/module/ec.js.map +1 -0
- package/lib/module/index.js +7 -4
- package/lib/module/index.js.map +1 -1
- package/lib/module/keys.js +279 -43
- package/lib/module/keys.js.map +1 -1
- package/lib/module/pbkdf2.js +44 -13
- package/lib/module/pbkdf2.js.map +1 -1
- package/lib/module/random.js +46 -54
- package/lib/module/random.js.map +1 -1
- package/lib/module/rsa.js +323 -0
- package/lib/module/rsa.js.map +1 -0
- package/lib/module/sig.js +13 -46
- package/lib/module/sig.js.map +1 -1
- package/lib/module/subtle.js +265 -0
- package/lib/module/subtle.js.map +1 -0
- package/lib/typescript/src/Cipher.d.ts +72 -0
- package/lib/typescript/src/Cipher.d.ts.map +1 -0
- package/lib/typescript/{Hash.d.ts → src/Hash.d.ts} +8 -7
- package/lib/typescript/src/Hash.d.ts.map +1 -0
- package/lib/typescript/src/Hashnames.d.ts +11 -0
- package/lib/typescript/src/Hashnames.d.ts.map +1 -0
- package/lib/typescript/{Hmac.d.ts → src/Hmac.d.ts} +3 -3
- package/lib/typescript/src/Hmac.d.ts.map +1 -0
- package/lib/typescript/src/NativeQuickCrypto/Cipher.d.ts +34 -0
- package/lib/typescript/src/NativeQuickCrypto/Cipher.d.ts.map +1 -0
- package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/NativeQuickCrypto.d.ts +3 -0
- package/lib/typescript/src/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +1 -0
- package/lib/typescript/src/NativeQuickCrypto/hash.d.ts +7 -0
- package/lib/typescript/src/NativeQuickCrypto/hash.d.ts.map +1 -0
- package/lib/typescript/src/NativeQuickCrypto/hmac.d.ts +6 -0
- package/lib/typescript/src/NativeQuickCrypto/hmac.d.ts.map +1 -0
- package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/pbkdf2.d.ts +2 -1
- package/lib/typescript/src/NativeQuickCrypto/pbkdf2.d.ts.map +1 -0
- package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/random.d.ts +2 -1
- package/lib/typescript/src/NativeQuickCrypto/random.d.ts.map +1 -0
- package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/sig.d.ts +5 -4
- package/lib/typescript/src/NativeQuickCrypto/sig.d.ts.map +1 -0
- package/lib/typescript/src/NativeQuickCrypto/webcrypto.d.ts +27 -0
- package/lib/typescript/src/NativeQuickCrypto/webcrypto.d.ts.map +1 -0
- package/lib/typescript/{QuickCrypto.d.ts → src/QuickCrypto.d.ts} +16 -8
- package/lib/typescript/src/QuickCrypto.d.ts.map +1 -0
- package/lib/typescript/src/Utils.d.ts +44 -0
- package/lib/typescript/src/Utils.d.ts.map +1 -0
- package/lib/typescript/src/aes.d.ts +5 -0
- package/lib/typescript/src/aes.d.ts.map +1 -0
- package/lib/typescript/{constants.d.ts → src/constants.d.ts} +1 -0
- package/lib/typescript/src/constants.d.ts.map +1 -0
- package/lib/typescript/src/ec.d.ts +5 -0
- package/lib/typescript/src/ec.d.ts.map +1 -0
- package/lib/typescript/{index.d.ts → src/index.d.ts} +33 -27
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/keys.d.ts +154 -0
- package/lib/typescript/src/keys.d.ts.map +1 -0
- package/lib/typescript/src/pbkdf2.d.ts +12 -0
- package/lib/typescript/src/pbkdf2.d.ts.map +1 -0
- package/lib/typescript/{random.d.ts → src/random.d.ts} +7 -6
- package/lib/typescript/src/random.d.ts.map +1 -0
- package/lib/typescript/src/rsa.d.ts +4 -0
- package/lib/typescript/src/rsa.d.ts.map +1 -0
- package/lib/typescript/{sig.d.ts → src/sig.d.ts} +3 -3
- package/lib/typescript/src/sig.d.ts.map +1 -0
- package/lib/typescript/src/subtle.d.ts +11 -0
- package/lib/typescript/src/subtle.d.ts.map +1 -0
- package/package.json +35 -30
- package/react-native-quick-crypto.podspec +5 -4
- package/src/Cipher.ts +103 -100
- package/src/Hash.ts +42 -6
- package/src/Hashnames.ts +91 -0
- package/src/Hmac.ts +3 -3
- package/src/NativeQuickCrypto/Cipher.ts +1 -0
- package/src/NativeQuickCrypto/NativeQuickCrypto.ts +2 -0
- package/src/NativeQuickCrypto/webcrypto.ts +46 -0
- package/src/QuickCrypto.ts +2 -0
- package/src/Utils.ts +409 -5
- package/src/aes.ts +365 -0
- package/src/ec.ts +351 -0
- package/src/keys.ts +428 -54
- package/src/pbkdf2.ts +84 -11
- package/src/random.ts +37 -24
- package/src/rsa.ts +396 -0
- package/src/sig.ts +3 -2
- package/src/subtle.ts +358 -0
- package/lib/commonjs/@types/stream-browserify.d.js +0 -2
- package/lib/commonjs/@types/stream-browserify.d.js.map +0 -1
- package/lib/module/@types/stream-browserify.d.js +0 -2
- package/lib/module/@types/stream-browserify.d.js.map +0 -1
- package/lib/typescript/Cipher.d.ts +0 -87
- package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +0 -32
- package/lib/typescript/NativeQuickCrypto/hash.d.ts +0 -6
- package/lib/typescript/NativeQuickCrypto/hmac.d.ts +0 -5
- package/lib/typescript/Utils.d.ts +0 -23
- package/lib/typescript/keys.d.ts +0 -60
- package/lib/typescript/pbkdf2.d.ts +0 -9
- package/src/@types/stream-browserify.d.ts +0 -4
package/lib/module/Cipher.js
CHANGED
|
@@ -1,108 +1,74 @@
|
|
|
1
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
2
|
-
|
|
3
1
|
/* eslint-disable no-dupe-class-members */
|
|
4
2
|
import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';
|
|
5
|
-
import Stream from 'stream
|
|
3
|
+
import Stream from 'readable-stream';
|
|
6
4
|
import { binaryLikeToArrayBuffer, getDefaultEncoding, kEmptyObject, validateFunction, validateObject, validateString, validateUint32, validateInt32 } from './Utils';
|
|
7
|
-
import { RSAKeyVariant } from './NativeQuickCrypto/Cipher';
|
|
8
|
-
//
|
|
9
|
-
// import type {
|
|
10
|
-
// CipherCCMOptions,
|
|
11
|
-
// CipherCCMTypes,
|
|
12
|
-
// CipherGCMTypes,
|
|
13
|
-
// CipherGCMOptions,
|
|
14
|
-
// // CipherKey,
|
|
15
|
-
// // KeyObject,
|
|
16
|
-
// // TODO(Szymon) This types seem to be missing? Where did you get this definitions from?
|
|
17
|
-
// // CipherOCBTypes,
|
|
18
|
-
// // CipherOCBOptions,
|
|
19
|
-
// } from 'crypto'; // Node crypto typings
|
|
20
|
-
|
|
5
|
+
import { RSAKeyVariant } from './NativeQuickCrypto/Cipher';
|
|
6
|
+
// @types/node
|
|
21
7
|
import { StringDecoder } from 'string_decoder';
|
|
22
8
|
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
23
9
|
import { Buffer as SBuffer } from 'safe-buffer';
|
|
24
10
|
import { constants } from './constants';
|
|
25
|
-
import { parsePrivateKeyEncoding, parsePublicKeyEncoding, preparePrivateKey, preparePublicOrPrivateKey } from './keys';
|
|
11
|
+
import { parsePrivateKeyEncoding, parsePublicKeyEncoding, preparePrivateKey, preparePublicOrPrivateKey } from './keys';
|
|
26
12
|
|
|
13
|
+
// make sure that nextTick is there
|
|
27
14
|
global.process.nextTick = setImmediate;
|
|
28
15
|
const createInternalCipher = NativeQuickCrypto.createCipher;
|
|
29
16
|
const createInternalDecipher = NativeQuickCrypto.createDecipher;
|
|
30
17
|
const _publicEncrypt = NativeQuickCrypto.publicEncrypt;
|
|
31
18
|
const _publicDecrypt = NativeQuickCrypto.publicDecrypt;
|
|
32
19
|
const _privateDecrypt = NativeQuickCrypto.privateDecrypt;
|
|
33
|
-
|
|
34
20
|
function getUIntOption(options, key) {
|
|
35
21
|
let value;
|
|
36
|
-
|
|
37
22
|
if (options && (value = options[key]) != null) {
|
|
38
23
|
// >>> Turns any type into a positive integer (also sets the sign bit to 0)
|
|
39
24
|
// eslint-disable-next-line no-bitwise
|
|
40
25
|
if (value >>> 0 !== value) throw new Error(`options.${key}: ${value}`);
|
|
41
26
|
return value;
|
|
42
27
|
}
|
|
43
|
-
|
|
44
28
|
return -1;
|
|
45
29
|
}
|
|
46
|
-
|
|
47
30
|
function normalizeEncoding(enc) {
|
|
48
31
|
if (!enc) return 'utf8';
|
|
49
32
|
var retried;
|
|
50
|
-
|
|
51
33
|
while (true) {
|
|
52
34
|
switch (enc) {
|
|
53
35
|
case 'utf8':
|
|
54
36
|
case 'utf-8':
|
|
55
37
|
return 'utf8';
|
|
56
|
-
|
|
57
38
|
case 'ucs2':
|
|
58
39
|
case 'ucs-2':
|
|
59
40
|
case 'utf16le':
|
|
60
41
|
case 'utf-16le':
|
|
61
42
|
return 'utf16le';
|
|
62
|
-
|
|
63
43
|
case 'latin1':
|
|
64
44
|
case 'binary':
|
|
65
45
|
return 'latin1';
|
|
66
|
-
|
|
67
46
|
case 'base64':
|
|
68
47
|
case 'ascii':
|
|
69
48
|
case 'hex':
|
|
70
49
|
return enc;
|
|
71
|
-
|
|
72
50
|
default:
|
|
73
51
|
if (retried) return; // undefined
|
|
74
|
-
|
|
75
52
|
enc = ('' + enc).toLowerCase();
|
|
76
53
|
retried = true;
|
|
77
54
|
}
|
|
78
55
|
}
|
|
79
56
|
}
|
|
80
|
-
|
|
81
57
|
function validateEncoding(data, encoding) {
|
|
82
58
|
const normalizedEncoding = normalizeEncoding(encoding);
|
|
83
59
|
const length = data.length;
|
|
84
|
-
|
|
85
60
|
if (normalizedEncoding === 'hex' && length % 2 !== 0) {
|
|
86
61
|
throw new Error(`Encoding ${encoding} not valid for data length ${length}`);
|
|
87
62
|
}
|
|
88
63
|
}
|
|
89
|
-
|
|
90
64
|
function getDecoder(decoder, encoding) {
|
|
91
|
-
return decoder
|
|
65
|
+
return decoder ?? new StringDecoder(encoding);
|
|
92
66
|
}
|
|
93
|
-
|
|
94
67
|
class CipherCommon extends Stream.Transform {
|
|
95
|
-
constructor(cipherType, cipherKey, isCipher) {
|
|
96
|
-
let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
97
|
-
let iv = arguments.length > 4 ? arguments[4] : undefined;
|
|
68
|
+
constructor(cipherType, cipherKey, isCipher, options = {}, iv) {
|
|
98
69
|
super(options);
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
_defineProperty(this, "decoder", void 0);
|
|
103
|
-
|
|
104
|
-
const cipherKeyBuffer = binaryLikeToArrayBuffer(cipherKey); // TODO(osp) This might not be smart, check again after release
|
|
105
|
-
|
|
70
|
+
const cipherKeyBuffer = binaryLikeToArrayBuffer(cipherKey);
|
|
71
|
+
// TODO(osp) This might not be smart, check again after release
|
|
106
72
|
const authTagLength = getUIntOption(options, 'authTagLength');
|
|
107
73
|
const args = {
|
|
108
74
|
cipher_type: cipherType,
|
|
@@ -113,20 +79,15 @@ class CipherCommon extends Stream.Transform {
|
|
|
113
79
|
};
|
|
114
80
|
this.internal = isCipher ? createInternalCipher(args) : createInternalDecipher(args);
|
|
115
81
|
}
|
|
116
|
-
|
|
117
82
|
update(data, inputEncoding, outputEncoding) {
|
|
118
|
-
var _inputEncoding, _outputEncoding;
|
|
119
|
-
|
|
120
83
|
const defaultEncoding = getDefaultEncoding();
|
|
121
|
-
inputEncoding =
|
|
122
|
-
outputEncoding =
|
|
123
|
-
|
|
84
|
+
inputEncoding = inputEncoding ?? defaultEncoding;
|
|
85
|
+
outputEncoding = outputEncoding ?? defaultEncoding;
|
|
124
86
|
if (typeof data === 'string') {
|
|
125
87
|
validateEncoding(data, inputEncoding);
|
|
126
88
|
} else if (!ArrayBuffer.isView(data)) {
|
|
127
89
|
throw new Error('Invalid data argument');
|
|
128
90
|
}
|
|
129
|
-
|
|
130
91
|
if (typeof data === 'string') {
|
|
131
92
|
// On node this is handled on the native side
|
|
132
93
|
// on our case we need to correctly send the arraybuffer to the jsi side
|
|
@@ -135,167 +96,81 @@ class CipherCommon extends Stream.Transform {
|
|
|
135
96
|
} else {
|
|
136
97
|
data = binaryLikeToArrayBuffer(data, inputEncoding);
|
|
137
98
|
}
|
|
138
|
-
|
|
139
99
|
const ret = this.internal.update(data);
|
|
140
|
-
|
|
141
100
|
if (outputEncoding && outputEncoding !== 'buffer') {
|
|
142
101
|
this.decoder = getDecoder(this.decoder, outputEncoding);
|
|
143
102
|
return this.decoder.write(SBuffer.from(ret));
|
|
144
103
|
}
|
|
145
|
-
|
|
146
104
|
return ret;
|
|
147
105
|
}
|
|
148
|
-
|
|
149
106
|
final(outputEncoding) {
|
|
150
107
|
const ret = this.internal.final();
|
|
151
|
-
|
|
152
108
|
if (outputEncoding && outputEncoding !== 'buffer') {
|
|
153
109
|
this.decoder = getDecoder(this.decoder, outputEncoding);
|
|
154
110
|
return this.decoder.end(SBuffer.from(ret));
|
|
155
111
|
}
|
|
156
|
-
|
|
157
112
|
return ret;
|
|
158
113
|
}
|
|
159
|
-
|
|
160
114
|
_transform(chunk, encoding, callback) {
|
|
161
115
|
this.push(this.update(chunk, encoding));
|
|
162
116
|
callback();
|
|
163
117
|
}
|
|
164
|
-
|
|
165
118
|
_flush(callback) {
|
|
166
119
|
this.push(this.final());
|
|
167
120
|
callback();
|
|
168
121
|
}
|
|
169
|
-
|
|
170
122
|
setAutoPadding(autoPadding) {
|
|
171
123
|
this.internal.setAutoPadding(!!autoPadding);
|
|
172
124
|
return this;
|
|
173
125
|
}
|
|
174
|
-
|
|
175
126
|
setAAD(buffer, options) {
|
|
176
127
|
this.internal.setAAD({
|
|
177
128
|
data: buffer.buffer,
|
|
178
|
-
plaintextLength: options
|
|
129
|
+
plaintextLength: options?.plaintextLength
|
|
179
130
|
});
|
|
180
131
|
return this;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
132
|
+
}
|
|
133
|
+
getAuthTag() {
|
|
134
|
+
return this.internal.getAuthTag();
|
|
135
|
+
}
|
|
186
136
|
setAuthTag(tag) {
|
|
187
|
-
this.internal.setAuthTag(tag
|
|
137
|
+
this.internal.setAuthTag(binaryLikeToArrayBuffer(tag));
|
|
188
138
|
return this;
|
|
189
139
|
}
|
|
190
|
-
|
|
191
140
|
}
|
|
192
|
-
|
|
193
141
|
class Cipher extends CipherCommon {
|
|
194
|
-
constructor(cipherType, cipherKey) {
|
|
195
|
-
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
196
|
-
let iv = arguments.length > 3 ? arguments[3] : undefined;
|
|
197
|
-
|
|
142
|
+
constructor(cipherType, cipherKey, options = {}, iv) {
|
|
198
143
|
if (iv != null) {
|
|
199
144
|
iv = binaryLikeToArrayBuffer(iv);
|
|
200
145
|
}
|
|
201
|
-
|
|
202
146
|
super(cipherType, cipherKey, true, options, iv);
|
|
203
147
|
}
|
|
204
|
-
|
|
205
148
|
}
|
|
206
|
-
|
|
207
149
|
class Decipher extends CipherCommon {
|
|
208
|
-
constructor(cipherType, cipherKey) {
|
|
209
|
-
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
210
|
-
let iv = arguments.length > 3 ? arguments[3] : undefined;
|
|
211
|
-
|
|
150
|
+
constructor(cipherType, cipherKey, options = {}, iv) {
|
|
212
151
|
if (iv != null) {
|
|
213
152
|
iv = binaryLikeToArrayBuffer(iv);
|
|
214
153
|
}
|
|
215
|
-
|
|
216
154
|
super(cipherType, cipherKey, false, options, iv);
|
|
217
155
|
}
|
|
218
|
-
|
|
219
|
-
} // TODO(osp) This definitions cause typescript errors when using the API
|
|
220
|
-
// export function createDecipher(
|
|
221
|
-
// algorithm: CipherCCMTypes,
|
|
222
|
-
// password: BinaryLike,
|
|
223
|
-
// options: CipherCCMOptions
|
|
224
|
-
// ): Decipher;
|
|
225
|
-
// export function createDecipher(
|
|
226
|
-
// algorithm: CipherGCMTypes,
|
|
227
|
-
// password: BinaryLike,
|
|
228
|
-
// options?: CipherGCMOptions
|
|
229
|
-
// ): Decipher;
|
|
230
|
-
|
|
231
|
-
|
|
156
|
+
}
|
|
232
157
|
export function createDecipher(algorithm, password, options) {
|
|
233
158
|
return new Decipher(algorithm, password, options);
|
|
234
|
-
}
|
|
235
|
-
// export function createDecipheriv(
|
|
236
|
-
// algorithm: CipherCCMTypes,
|
|
237
|
-
// key: BinaryLike,
|
|
238
|
-
// iv: BinaryLike,
|
|
239
|
-
// options: CipherCCMOptions
|
|
240
|
-
// ): Decipher;
|
|
241
|
-
// export function createDecipheriv(
|
|
242
|
-
// algorithm: CipherOCBTypes,
|
|
243
|
-
// key: BinaryLike,
|
|
244
|
-
// iv: BinaryLike,
|
|
245
|
-
// options: CipherOCBOptions
|
|
246
|
-
// ): DecipherOCB;
|
|
247
|
-
// export function createDecipheriv(
|
|
248
|
-
// algorithm: CipherGCMTypes,
|
|
249
|
-
// key: BinaryLike,
|
|
250
|
-
// iv: BinaryLike,
|
|
251
|
-
// options?: CipherGCMOptions
|
|
252
|
-
// ): Decipher;
|
|
253
|
-
|
|
159
|
+
}
|
|
254
160
|
export function createDecipheriv(algorithm, key, iv, options) {
|
|
255
161
|
return new Decipher(algorithm, key, options, iv);
|
|
256
|
-
}
|
|
257
|
-
// commenting them out for now
|
|
258
|
-
// export function createCipher(
|
|
259
|
-
// algorithm: CipherCCMTypes,
|
|
260
|
-
// password: BinaryLike,
|
|
261
|
-
// options: CipherCCMOptions
|
|
262
|
-
// ): Cipher;
|
|
263
|
-
// export function createCipher(
|
|
264
|
-
// algorithm: CipherGCMTypes,
|
|
265
|
-
// password: BinaryLike,
|
|
266
|
-
// options?: CipherGCMOptions
|
|
267
|
-
// ): Cipher;
|
|
268
|
-
|
|
162
|
+
}
|
|
269
163
|
export function createCipher(algorithm, password, options) {
|
|
270
164
|
return new Cipher(algorithm, password, options);
|
|
271
|
-
}
|
|
272
|
-
// object that creates keys and what not. Not sure if we should support it.
|
|
273
|
-
// Fow now I replaced all of them to BinaryLike
|
|
274
|
-
// export function createCipheriv(
|
|
275
|
-
// algorithm: CipherCCMTypes,
|
|
276
|
-
// key: BinaryLike,
|
|
277
|
-
// iv: BinaryLike,
|
|
278
|
-
// options: CipherCCMOptions
|
|
279
|
-
// ): Cipher;
|
|
280
|
-
// export function createCipheriv(
|
|
281
|
-
// algorithm: CipherOCBTypes,
|
|
282
|
-
// key: BinaryLike,
|
|
283
|
-
// iv: BinaryLike,
|
|
284
|
-
// options: CipherOCBOptions
|
|
285
|
-
// ): CipherOCB;
|
|
286
|
-
// export function createCipheriv(
|
|
287
|
-
// algorithm: CipherGCMTypes,
|
|
288
|
-
// key: BinaryLike,
|
|
289
|
-
// iv: BinaryLike,
|
|
290
|
-
// options?: CipherGCMOptions
|
|
291
|
-
// ): Cipher;
|
|
292
|
-
|
|
165
|
+
}
|
|
293
166
|
export function createCipheriv(algorithm, key, iv, options) {
|
|
294
167
|
return new Cipher(algorithm, key, options, iv);
|
|
295
|
-
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// RSA Functions
|
|
296
171
|
// Follows closely the model implemented in node
|
|
297
|
-
// TODO(osp) types...
|
|
298
172
|
|
|
173
|
+
// TODO(osp) types...
|
|
299
174
|
function rsaFunctionFor(method, defaultPadding, keyType) {
|
|
300
175
|
return (options, buffer) => {
|
|
301
176
|
const {
|
|
@@ -319,12 +194,13 @@ function rsaFunctionFor(method, defaultPadding, keyType) {
|
|
|
319
194
|
return Buffer.from(rawRes);
|
|
320
195
|
};
|
|
321
196
|
}
|
|
322
|
-
|
|
323
197
|
export const publicEncrypt = rsaFunctionFor(_publicEncrypt, constants.RSA_PKCS1_OAEP_PADDING, 'public');
|
|
324
|
-
export const publicDecrypt = rsaFunctionFor(_publicDecrypt, constants.RSA_PKCS1_PADDING, 'public');
|
|
198
|
+
export const publicDecrypt = rsaFunctionFor(_publicDecrypt, constants.RSA_PKCS1_PADDING, 'public');
|
|
199
|
+
// const privateEncrypt = rsaFunctionFor(_privateEncrypt, constants.RSA_PKCS1_PADDING,
|
|
325
200
|
// 'private');
|
|
201
|
+
export const privateDecrypt = rsaFunctionFor(_privateDecrypt, constants.RSA_PKCS1_OAEP_PADDING, 'private');
|
|
326
202
|
|
|
327
|
-
|
|
203
|
+
// _ _ __ _____ _
|
|
328
204
|
// | | | |/ / | __ \ (_)
|
|
329
205
|
// __ _ ___ _ __ ___ _ __ __ _| |_ ___| ' / ___ _ _| |__) |_ _ _ _ __
|
|
330
206
|
// / _` |/ _ \ '_ \ / _ \ '__/ _` | __/ _ \ < / _ \ | | | ___/ _` | | '__|
|
|
@@ -333,14 +209,12 @@ export const privateDecrypt = rsaFunctionFor(_privateDecrypt, constants.RSA_PKCS
|
|
|
333
209
|
// __/ | __/ |
|
|
334
210
|
// |___/ |___/
|
|
335
211
|
|
|
336
|
-
function parseKeyEncoding(keyType) {
|
|
337
|
-
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : kEmptyObject;
|
|
212
|
+
function parseKeyEncoding(keyType, options = kEmptyObject) {
|
|
338
213
|
const {
|
|
339
214
|
publicKeyEncoding,
|
|
340
215
|
privateKeyEncoding
|
|
341
216
|
} = options;
|
|
342
217
|
let publicFormat, publicType;
|
|
343
|
-
|
|
344
218
|
if (publicKeyEncoding == null) {
|
|
345
219
|
publicFormat = publicType = undefined;
|
|
346
220
|
} else if (typeof publicKeyEncoding === 'object') {
|
|
@@ -351,9 +225,7 @@ function parseKeyEncoding(keyType) {
|
|
|
351
225
|
} else {
|
|
352
226
|
throw new Error('Invalid argument options.publicKeyEncoding', publicKeyEncoding);
|
|
353
227
|
}
|
|
354
|
-
|
|
355
228
|
let privateFormat, privateType, cipher, passphrase;
|
|
356
|
-
|
|
357
229
|
if (privateKeyEncoding == null) {
|
|
358
230
|
privateFormat = privateType = undefined;
|
|
359
231
|
} else if (typeof privateKeyEncoding === 'object') {
|
|
@@ -366,14 +238,14 @@ function parseKeyEncoding(keyType) {
|
|
|
366
238
|
} else {
|
|
367
239
|
throw new Error('Invalid argument options.privateKeyEncoding', publicKeyEncoding);
|
|
368
240
|
}
|
|
369
|
-
|
|
370
241
|
return [publicFormat, publicType, privateFormat, privateType, cipher, passphrase];
|
|
371
242
|
}
|
|
372
|
-
|
|
373
243
|
function internalGenerateKeyPair(isAsync, type, options, callback) {
|
|
374
244
|
// On node a very complex "job" chain is created, we are going for a far simpler approach and calling
|
|
375
245
|
// an internal function that basically executes the same byte shuffling on the native side
|
|
376
|
-
const encoding = parseKeyEncoding(type, options);
|
|
246
|
+
const encoding = parseKeyEncoding(type, options);
|
|
247
|
+
|
|
248
|
+
// if (options !== undefined)
|
|
377
249
|
// validateObject(options, 'options');
|
|
378
250
|
|
|
379
251
|
switch (type) {
|
|
@@ -388,59 +260,50 @@ function internalGenerateKeyPair(isAsync, type, options, callback) {
|
|
|
388
260
|
let {
|
|
389
261
|
publicExponent
|
|
390
262
|
} = options;
|
|
391
|
-
|
|
392
263
|
if (publicExponent == null) {
|
|
393
264
|
publicExponent = 0x10001;
|
|
394
265
|
} else {
|
|
395
266
|
validateUint32(publicExponent, 'options.publicExponent');
|
|
396
267
|
}
|
|
397
|
-
|
|
398
268
|
if (type === 'rsa') {
|
|
399
269
|
if (isAsync) {
|
|
400
|
-
NativeQuickCrypto.generateKeyPair(RSAKeyVariant.kKeyVariantRSA_SSA_PKCS1_v1_5, modulusLength, publicExponent, ...encoding).then(
|
|
401
|
-
let [err, publicKey, privateKey] = _ref;
|
|
402
|
-
|
|
270
|
+
NativeQuickCrypto.generateKeyPair(RSAKeyVariant.kKeyVariantRSA_SSA_PKCS1_v1_5, modulusLength, publicExponent, ...encoding).then(([err, publicKey, privateKey]) => {
|
|
403
271
|
if (typeof publicKey === 'object') {
|
|
404
272
|
publicKey = Buffer.from(publicKey);
|
|
405
273
|
}
|
|
406
|
-
|
|
407
274
|
if (typeof privateKey === 'object') {
|
|
408
275
|
privateKey = Buffer.from(privateKey);
|
|
409
276
|
}
|
|
410
|
-
|
|
411
|
-
callback === null || callback === void 0 ? void 0 : callback(err, publicKey, privateKey);
|
|
277
|
+
callback?.(err, publicKey, privateKey);
|
|
412
278
|
}).catch(err => {
|
|
413
|
-
callback
|
|
279
|
+
callback?.(err, undefined, undefined);
|
|
414
280
|
});
|
|
415
281
|
return;
|
|
416
282
|
} else {
|
|
417
283
|
let [err, publicKey, privateKey] = NativeQuickCrypto.generateKeyPairSync(RSAKeyVariant.kKeyVariantRSA_SSA_PKCS1_v1_5, modulusLength, publicExponent, ...encoding);
|
|
418
|
-
|
|
419
284
|
if (typeof publicKey === 'object') {
|
|
420
285
|
publicKey = Buffer.from(publicKey);
|
|
421
286
|
}
|
|
422
|
-
|
|
423
287
|
if (typeof privateKey === 'object') {
|
|
424
288
|
privateKey = Buffer.from(privateKey);
|
|
425
289
|
}
|
|
426
|
-
|
|
427
290
|
return [err, publicKey, privateKey];
|
|
428
291
|
}
|
|
429
292
|
}
|
|
430
|
-
|
|
431
293
|
const {
|
|
432
294
|
hash,
|
|
433
295
|
mgf1Hash,
|
|
434
296
|
hashAlgorithm,
|
|
435
297
|
mgf1HashAlgorithm,
|
|
436
298
|
saltLength
|
|
437
|
-
} = options;
|
|
299
|
+
} = options;
|
|
300
|
+
|
|
301
|
+
// // We don't have a process object on RN
|
|
438
302
|
// // const pendingDeprecation = getOptionValue('--pending-deprecation');
|
|
439
303
|
|
|
440
304
|
if (saltLength !== undefined) validateInt32(saltLength, 'options.saltLength', 0);
|
|
441
305
|
if (hashAlgorithm !== undefined) validateString(hashAlgorithm, 'options.hashAlgorithm');
|
|
442
306
|
if (mgf1HashAlgorithm !== undefined) validateString(mgf1HashAlgorithm, 'options.mgf1HashAlgorithm');
|
|
443
|
-
|
|
444
307
|
if (hash !== undefined) {
|
|
445
308
|
// pendingDeprecation && process.emitWarning(
|
|
446
309
|
// '"options.hash" is deprecated, ' +
|
|
@@ -448,12 +311,10 @@ function internalGenerateKeyPair(isAsync, type, options, callback) {
|
|
|
448
311
|
// 'DeprecationWarning',
|
|
449
312
|
// 'DEP0154');
|
|
450
313
|
validateString(hash, 'options.hash');
|
|
451
|
-
|
|
452
314
|
if (hashAlgorithm && hash !== hashAlgorithm) {
|
|
453
315
|
throw new Error(`Invalid Argument options.hash ${hash}`);
|
|
454
316
|
}
|
|
455
317
|
}
|
|
456
|
-
|
|
457
318
|
if (mgf1Hash !== undefined) {
|
|
458
319
|
// pendingDeprecation && process.emitWarning(
|
|
459
320
|
// '"options.mgf1Hash" is deprecated, ' +
|
|
@@ -461,22 +322,22 @@ function internalGenerateKeyPair(isAsync, type, options, callback) {
|
|
|
461
322
|
// 'DeprecationWarning',
|
|
462
323
|
// 'DEP0154');
|
|
463
324
|
validateString(mgf1Hash, 'options.mgf1Hash');
|
|
464
|
-
|
|
465
325
|
if (mgf1HashAlgorithm && mgf1Hash !== mgf1HashAlgorithm) {
|
|
466
326
|
throw new Error(`Invalid Argument options.mgf1Hash ${mgf1Hash}`);
|
|
467
327
|
}
|
|
468
328
|
}
|
|
469
|
-
|
|
470
329
|
return NativeQuickCrypto.generateKeyPairSync(RSAKeyVariant.kKeyVariantRSA_PSS, modulusLength, publicExponent, hashAlgorithm || hash, mgf1HashAlgorithm || mgf1Hash, saltLength, ...encoding);
|
|
471
330
|
}
|
|
472
331
|
// case 'dsa': {
|
|
473
332
|
// validateObject(options, 'options');
|
|
474
333
|
// const { modulusLength } = options!;
|
|
475
334
|
// validateUint32(modulusLength, 'options.modulusLength');
|
|
335
|
+
|
|
476
336
|
// let { divisorLength } = options!;
|
|
477
337
|
// if (divisorLength == null) {
|
|
478
338
|
// divisorLength = -1;
|
|
479
339
|
// } else validateInt32(divisorLength, 'options.divisorLength', 0);
|
|
340
|
+
|
|
480
341
|
// // return new DsaKeyPairGenJob(
|
|
481
342
|
// // mode,
|
|
482
343
|
// // modulusLength,
|
|
@@ -495,6 +356,7 @@ function internalGenerateKeyPair(isAsync, type, options, callback) {
|
|
|
495
356
|
// else
|
|
496
357
|
// throw new Error(`Invalid Argument options.paramEncoding ${paramEncoding}`);
|
|
497
358
|
// // throw new ERR_INVALID_ARG_VALUE('options.paramEncoding', paramEncoding);
|
|
359
|
+
|
|
498
360
|
// // return new EcKeyPairGenJob(mode, namedCurve, paramEncoding, ...encoding);
|
|
499
361
|
// }
|
|
500
362
|
// case 'ed25519':
|
|
@@ -528,12 +390,16 @@ function internalGenerateKeyPair(isAsync, type, options, callback) {
|
|
|
528
390
|
// throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'primeLength');
|
|
529
391
|
// if (generator != null)
|
|
530
392
|
// throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'generator');
|
|
393
|
+
|
|
531
394
|
// validateString(group, 'options.group');
|
|
395
|
+
|
|
532
396
|
// return new DhKeyPairGenJob(mode, group, ...encoding);
|
|
533
397
|
// }
|
|
398
|
+
|
|
534
399
|
// if (prime != null) {
|
|
535
400
|
// if (primeLength != null)
|
|
536
401
|
// throw new ERR_INCOMPATIBLE_OPTION_PAIR('prime', 'primeLength');
|
|
402
|
+
|
|
537
403
|
// validateBuffer(prime, 'options.prime');
|
|
538
404
|
// } else if (primeLength != null) {
|
|
539
405
|
// validateInt32(primeLength, 'options.primeLength', 0);
|
|
@@ -542,6 +408,7 @@ function internalGenerateKeyPair(isAsync, type, options, callback) {
|
|
|
542
408
|
// 'At least one of the group, prime, or primeLength options'
|
|
543
409
|
// );
|
|
544
410
|
// }
|
|
411
|
+
|
|
545
412
|
// if (generator != null) {
|
|
546
413
|
// validateInt32(generator, 'options.generator', 0);
|
|
547
414
|
// }
|
|
@@ -552,21 +419,19 @@ function internalGenerateKeyPair(isAsync, type, options, callback) {
|
|
|
552
419
|
// ...encoding
|
|
553
420
|
// );
|
|
554
421
|
// }
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
422
|
+
default:
|
|
423
|
+
// Fall through
|
|
558
424
|
}
|
|
559
|
-
|
|
560
425
|
throw new Error(`Invalid Argument options: ${type} scheme not supported. Currently not all encryption methods are supported in quick-crypto!`);
|
|
561
|
-
}
|
|
426
|
+
}
|
|
562
427
|
|
|
428
|
+
// TODO(osp) put correct types (e.g. type -> 'rsa', etc..)
|
|
563
429
|
|
|
564
430
|
export function generateKeyPair(type, options, callback) {
|
|
565
431
|
if (typeof options === 'function') {
|
|
566
432
|
callback = options;
|
|
567
433
|
options = undefined;
|
|
568
434
|
}
|
|
569
|
-
|
|
570
435
|
validateFunction(callback);
|
|
571
436
|
internalGenerateKeyPair(true, type, options, callback);
|
|
572
437
|
}
|