react-native-quick-crypto 0.7.0 → 0.7.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/android/CMakeLists.txt +2 -0
- package/cpp/Cipher/MGLRsa.cpp +179 -3
- package/cpp/Cipher/MGLRsa.h +40 -0
- package/cpp/JSIUtils/MGLJSIUtils.h +8 -0
- package/cpp/MGLKeys.cpp +41 -43
- package/cpp/MGLKeys.h +9 -2
- package/cpp/MGLQuickCryptoHostObject.cpp +6 -6
- package/cpp/Utils/MGLUtils.cpp +71 -1
- package/cpp/Utils/MGLUtils.h +55 -1
- package/cpp/webcrypto/MGLWebCrypto.cpp +89 -37
- package/cpp/webcrypto/MGLWebCrypto.h +5 -7
- package/cpp/webcrypto/crypto_aes.cpp +516 -0
- package/cpp/webcrypto/crypto_aes.h +79 -0
- package/cpp/webcrypto/crypto_ec.cpp +4 -20
- package/cpp/webcrypto/crypto_ec.h +0 -5
- package/cpp/webcrypto/crypto_keygen.cpp +86 -0
- package/cpp/webcrypto/crypto_keygen.h +38 -0
- package/lib/commonjs/Cipher.js +3 -1
- package/lib/commonjs/Cipher.js.map +1 -1
- package/lib/commonjs/Hashnames.js +20 -8
- package/lib/commonjs/Hashnames.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/Cipher.js +13 -1
- package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/aes.js +6 -0
- package/lib/commonjs/NativeQuickCrypto/aes.js.map +1 -0
- package/lib/commonjs/NativeQuickCrypto/keygen.js +6 -0
- package/lib/commonjs/NativeQuickCrypto/keygen.js.map +1 -0
- package/lib/commonjs/NativeQuickCrypto/rsa.js +6 -0
- package/lib/commonjs/NativeQuickCrypto/rsa.js.map +1 -0
- package/lib/commonjs/Utils.js +30 -6
- package/lib/commonjs/Utils.js.map +1 -1
- package/lib/commonjs/aes.js +184 -227
- package/lib/commonjs/aes.js.map +1 -1
- package/lib/commonjs/index.js +12 -2
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/keygen.js +56 -0
- package/lib/commonjs/keygen.js.map +1 -0
- package/lib/commonjs/keys.js +74 -5
- package/lib/commonjs/keys.js.map +1 -1
- package/lib/commonjs/rsa.js +115 -196
- package/lib/commonjs/rsa.js.map +1 -1
- package/lib/commonjs/sig.js.map +1 -1
- package/lib/commonjs/subtle.js +140 -78
- package/lib/commonjs/subtle.js.map +1 -1
- package/lib/commonjs/webcrypto.js +14 -0
- package/lib/commonjs/webcrypto.js.map +1 -0
- package/lib/module/Cipher.js +3 -1
- package/lib/module/Cipher.js.map +1 -1
- package/lib/module/Hashnames.js +20 -8
- package/lib/module/Hashnames.js.map +1 -1
- package/lib/module/NativeQuickCrypto/Cipher.js +12 -0
- package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
- package/lib/module/NativeQuickCrypto/aes.js +2 -0
- package/lib/module/NativeQuickCrypto/aes.js.map +1 -0
- package/lib/module/NativeQuickCrypto/keygen.js +2 -0
- package/lib/module/NativeQuickCrypto/keygen.js.map +1 -0
- package/lib/module/NativeQuickCrypto/rsa.js +2 -0
- package/lib/module/NativeQuickCrypto/rsa.js.map +1 -0
- package/lib/module/Utils.js +26 -5
- package/lib/module/Utils.js.map +1 -1
- package/lib/module/aes.js +183 -228
- package/lib/module/aes.js.map +1 -1
- package/lib/module/index.js +11 -2
- package/lib/module/index.js.map +1 -1
- package/lib/module/keygen.js +47 -0
- package/lib/module/keygen.js.map +1 -0
- package/lib/module/keys.js +68 -4
- package/lib/module/keys.js.map +1 -1
- package/lib/module/rsa.js +115 -198
- package/lib/module/rsa.js.map +1 -1
- package/lib/module/sig.js.map +1 -1
- package/lib/module/subtle.js +143 -82
- package/lib/module/subtle.js.map +1 -1
- package/lib/module/webcrypto.js +8 -0
- package/lib/module/webcrypto.js.map +1 -0
- package/lib/typescript/Cipher.d.ts +0 -1
- package/lib/typescript/Cipher.d.ts.map +1 -1
- package/lib/typescript/Hash.d.ts.map +1 -1
- package/lib/typescript/Hashnames.d.ts +2 -2
- package/lib/typescript/Hashnames.d.ts.map +1 -1
- package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +5 -0
- package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +1 -1
- package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +4 -1
- package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +1 -1
- package/lib/typescript/NativeQuickCrypto/aes.d.ts +5 -0
- package/lib/typescript/NativeQuickCrypto/aes.d.ts.map +1 -0
- package/lib/typescript/NativeQuickCrypto/keygen.d.ts +4 -0
- package/lib/typescript/NativeQuickCrypto/keygen.d.ts.map +1 -0
- package/lib/typescript/NativeQuickCrypto/rsa.d.ts +5 -0
- package/lib/typescript/NativeQuickCrypto/rsa.d.ts.map +1 -0
- package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts +12 -2
- package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +1 -1
- package/lib/typescript/Utils.d.ts +4 -4
- package/lib/typescript/Utils.d.ts.map +1 -1
- package/lib/typescript/aes.d.ts +18 -1
- package/lib/typescript/aes.d.ts.map +1 -1
- package/lib/typescript/ec.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +27 -24
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/keygen.d.ts +6 -0
- package/lib/typescript/keygen.d.ts.map +1 -0
- package/lib/typescript/keys.d.ts +55 -17
- package/lib/typescript/keys.d.ts.map +1 -1
- package/lib/typescript/rsa.d.ts +9 -1
- package/lib/typescript/rsa.d.ts.map +1 -1
- package/lib/typescript/sig.d.ts +3 -17
- package/lib/typescript/sig.d.ts.map +1 -1
- package/lib/typescript/subtle.d.ts +6 -5
- package/lib/typescript/subtle.d.ts.map +1 -1
- package/lib/typescript/webcrypto.d.ts +9 -0
- package/lib/typescript/webcrypto.d.ts.map +1 -0
- package/package.json +2 -2
- package/src/Cipher.ts +1 -1
- package/src/Hashnames.ts +23 -21
- package/src/NativeQuickCrypto/Cipher.ts +32 -0
- package/src/NativeQuickCrypto/NativeQuickCrypto.ts +6 -0
- package/src/NativeQuickCrypto/aes.ts +14 -0
- package/src/NativeQuickCrypto/keygen.ts +7 -0
- package/src/NativeQuickCrypto/rsa.ts +12 -0
- package/src/NativeQuickCrypto/webcrypto.ts +26 -2
- package/src/Utils.ts +37 -8
- package/src/aes.ts +259 -222
- package/src/index.ts +10 -1
- package/src/keygen.ts +80 -0
- package/src/keys.ts +139 -30
- package/src/rsa.ts +161 -187
- package/src/sig.ts +7 -23
- package/src/subtle.ts +211 -93
- package/src/webcrypto.ts +8 -0
package/src/index.ts
CHANGED
|
@@ -12,15 +12,18 @@ import {
|
|
|
12
12
|
generateKeyPair,
|
|
13
13
|
generateKeyPairSync,
|
|
14
14
|
} from './Cipher';
|
|
15
|
+
import { generateKey, generateKeySync } from './keygen';
|
|
15
16
|
import { createSign, createVerify } from './sig';
|
|
16
17
|
import { createHmac } from './Hmac';
|
|
17
18
|
import { createHash } from './Hash';
|
|
18
19
|
import { constants } from './constants';
|
|
19
20
|
import { subtle } from './subtle';
|
|
20
21
|
import { getCiphers, getHashes } from './Utils';
|
|
22
|
+
import webcrypto from './webcrypto';
|
|
23
|
+
import { createPrivateKey, createPublicKey, createSecretKey } from './keys';
|
|
21
24
|
|
|
22
25
|
/**
|
|
23
|
-
*
|
|
26
|
+
* Loosely matches Node.js {crypto} with some unimplemented functionality
|
|
24
27
|
*/
|
|
25
28
|
const QuickCrypto = {
|
|
26
29
|
createHmac,
|
|
@@ -31,11 +34,16 @@ const QuickCrypto = {
|
|
|
31
34
|
createCipheriv,
|
|
32
35
|
createDecipher,
|
|
33
36
|
createDecipheriv,
|
|
37
|
+
createPublicKey,
|
|
38
|
+
createPrivateKey,
|
|
39
|
+
createSecretKey,
|
|
34
40
|
publicEncrypt,
|
|
35
41
|
publicDecrypt,
|
|
36
42
|
privateDecrypt,
|
|
43
|
+
generateKey,
|
|
37
44
|
generateKeyPair,
|
|
38
45
|
generateKeyPairSync,
|
|
46
|
+
generateKeySync,
|
|
39
47
|
createSign,
|
|
40
48
|
createVerify,
|
|
41
49
|
subtle,
|
|
@@ -44,6 +52,7 @@ const QuickCrypto = {
|
|
|
44
52
|
...random,
|
|
45
53
|
getCiphers,
|
|
46
54
|
getHashes,
|
|
55
|
+
webcrypto,
|
|
47
56
|
};
|
|
48
57
|
|
|
49
58
|
/**
|
package/src/keygen.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';
|
|
2
|
+
import { lazyDOMException, validateFunction } from './Utils';
|
|
3
|
+
import { kAesKeyLengths } from './aes';
|
|
4
|
+
import {
|
|
5
|
+
SecretKeyObject,
|
|
6
|
+
type SecretKeyType,
|
|
7
|
+
type AesKeyGenParams,
|
|
8
|
+
} from './keys';
|
|
9
|
+
|
|
10
|
+
export type KeyGenCallback = (
|
|
11
|
+
err: Error | undefined,
|
|
12
|
+
key?: SecretKeyObject
|
|
13
|
+
) => void;
|
|
14
|
+
|
|
15
|
+
export const generateKeyPromise = (
|
|
16
|
+
type: SecretKeyType,
|
|
17
|
+
options: AesKeyGenParams // | HmacKeyGenParams
|
|
18
|
+
): Promise<[Error | undefined, SecretKeyObject | undefined]> => {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
generateKey(type, options, (err, key) => {
|
|
21
|
+
if (err) {
|
|
22
|
+
reject([err, undefined]);
|
|
23
|
+
}
|
|
24
|
+
resolve([undefined, key]);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const generateKey = (
|
|
30
|
+
type: SecretKeyType,
|
|
31
|
+
options: AesKeyGenParams, // | HmacKeyGenParams,
|
|
32
|
+
callback: KeyGenCallback
|
|
33
|
+
): void => {
|
|
34
|
+
validateLength(type, options.length);
|
|
35
|
+
if (!validateFunction(callback)) {
|
|
36
|
+
throw lazyDOMException('Callback is not a function', 'SyntaxError');
|
|
37
|
+
}
|
|
38
|
+
NativeQuickCrypto.webcrypto
|
|
39
|
+
.generateSecretKey(options.length)
|
|
40
|
+
.then((handle) => {
|
|
41
|
+
callback(undefined, new SecretKeyObject(handle));
|
|
42
|
+
})
|
|
43
|
+
.catch((err) => {
|
|
44
|
+
callback(err, undefined);
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const generateKeySync = (
|
|
49
|
+
type: SecretKeyType,
|
|
50
|
+
options: AesKeyGenParams // | HmacKeyGenParams,
|
|
51
|
+
): SecretKeyObject => {
|
|
52
|
+
validateLength(type, options.length);
|
|
53
|
+
const handle = NativeQuickCrypto.webcrypto.generateSecretKeySync(
|
|
54
|
+
options.length
|
|
55
|
+
);
|
|
56
|
+
return new SecretKeyObject(handle);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const validateLength = (type: SecretKeyType, length: number) => {
|
|
60
|
+
switch (type) {
|
|
61
|
+
case 'aes':
|
|
62
|
+
if (!kAesKeyLengths.includes(length)) {
|
|
63
|
+
throw lazyDOMException(
|
|
64
|
+
'AES key length must be 128, 192, or 256 bits',
|
|
65
|
+
'OperationError'
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
break;
|
|
69
|
+
case 'hmac':
|
|
70
|
+
if (length < 8 || length > 2 ** 31 - 1) {
|
|
71
|
+
throw lazyDOMException(
|
|
72
|
+
'HMAC key length must be between 8 and 2^31 - 1',
|
|
73
|
+
'OperationError'
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
break;
|
|
77
|
+
default:
|
|
78
|
+
throw new Error(`Unsupported key type '${type}' for generateKey()`);
|
|
79
|
+
}
|
|
80
|
+
};
|
package/src/keys.ts
CHANGED
|
@@ -2,6 +2,8 @@ import {
|
|
|
2
2
|
type BinaryLike,
|
|
3
3
|
binaryLikeToArrayBuffer,
|
|
4
4
|
isStringOrBuffer,
|
|
5
|
+
type BufferLike,
|
|
6
|
+
type TypedArray,
|
|
5
7
|
} from './Utils';
|
|
6
8
|
import type { KeyObjectHandle } from './NativeQuickCrypto/webcrypto';
|
|
7
9
|
import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';
|
|
@@ -27,25 +29,30 @@ export type AnyAlgorithm =
|
|
|
27
29
|
| 'PBKDF2'
|
|
28
30
|
| 'HKDF';
|
|
29
31
|
|
|
30
|
-
export type HashAlgorithm =
|
|
32
|
+
export type HashAlgorithm =
|
|
33
|
+
| 'SHA-1'
|
|
34
|
+
| 'SHA-224'
|
|
35
|
+
| 'SHA-256'
|
|
36
|
+
| 'SHA-384'
|
|
37
|
+
| 'SHA-512'
|
|
38
|
+
| 'RIPEMD-160';
|
|
39
|
+
|
|
40
|
+
export type DigestAlgorithm = 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512';
|
|
31
41
|
|
|
32
42
|
export type KeyPairType = 'rsa' | 'rsa-pss' | 'ec';
|
|
33
43
|
|
|
34
44
|
export type RSAKeyPairAlgorithm = 'RSASSA-PKCS1-v1_5' | 'RSA-PSS' | 'RSA-OAEP';
|
|
35
45
|
export type ECKeyPairAlgorithm = 'ECDSA' | 'ECDH';
|
|
36
46
|
export type CFRGKeyPairAlgorithm = 'Ed25519' | 'Ed448' | 'X25519' | 'X448';
|
|
47
|
+
export type AESAlgorithm = 'AES-CTR' | 'AES-CBC' | 'AES-GCM' | 'AES-KW';
|
|
37
48
|
|
|
38
49
|
export type KeyPairAlgorithm =
|
|
39
50
|
| RSAKeyPairAlgorithm
|
|
40
51
|
| ECKeyPairAlgorithm
|
|
41
52
|
| CFRGKeyPairAlgorithm;
|
|
42
53
|
|
|
43
|
-
export type SecretKeyAlgorithm =
|
|
44
|
-
|
|
45
|
-
| 'AES-CTR'
|
|
46
|
-
| 'AES-CBC'
|
|
47
|
-
| 'AES-GCM'
|
|
48
|
-
| 'AES-KW';
|
|
54
|
+
export type SecretKeyAlgorithm = 'HMAC' | AESAlgorithm;
|
|
55
|
+
export type SecretKeyType = 'hmac' | 'aes';
|
|
49
56
|
|
|
50
57
|
export type SignVerifyAlgorithm =
|
|
51
58
|
| 'RSASSA-PKCS1-v1_5'
|
|
@@ -62,6 +69,49 @@ export type DeriveBitsAlgorithm =
|
|
|
62
69
|
| 'X25519'
|
|
63
70
|
| 'X448';
|
|
64
71
|
|
|
72
|
+
export type RsaOaepParams = {
|
|
73
|
+
name: 'RSA-OAEP';
|
|
74
|
+
label?: BufferLike;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export type AesCbcParams = {
|
|
78
|
+
name: 'AES-CBC';
|
|
79
|
+
iv: BufferLike;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export type AesCtrParams = {
|
|
83
|
+
name: 'AES-CTR';
|
|
84
|
+
counter: TypedArray;
|
|
85
|
+
length: number;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export type AesGcmParams = {
|
|
89
|
+
name: 'AES-GCM';
|
|
90
|
+
iv: BufferLike;
|
|
91
|
+
tagLength?: TagLength;
|
|
92
|
+
additionalData?: BufferLike;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export type AesKwParams = {
|
|
96
|
+
name: 'AES-KW';
|
|
97
|
+
wrappingKey?: BufferLike;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export type AesKeyGenParams = {
|
|
101
|
+
length: AESLength;
|
|
102
|
+
name?: AESAlgorithm;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export type TagLength = 32 | 64 | 96 | 104 | 112 | 120 | 128;
|
|
106
|
+
|
|
107
|
+
export type AESLength = 128 | 192 | 256;
|
|
108
|
+
|
|
109
|
+
export type EncryptDecryptParams =
|
|
110
|
+
| AesCbcParams
|
|
111
|
+
| AesCtrParams
|
|
112
|
+
| AesGcmParams
|
|
113
|
+
| RsaOaepParams;
|
|
114
|
+
|
|
65
115
|
export type EncryptDecryptAlgorithm =
|
|
66
116
|
| 'RSA-OAEP'
|
|
67
117
|
| 'AES-CTR'
|
|
@@ -97,6 +147,8 @@ export enum KFormatType {
|
|
|
97
147
|
kKeyFormatJWK,
|
|
98
148
|
}
|
|
99
149
|
|
|
150
|
+
export type KFormat = 'der' | 'pem' | 'jwk';
|
|
151
|
+
|
|
100
152
|
// Same as KFormatType, this enum needs to be defined on the native side
|
|
101
153
|
export enum KeyType {
|
|
102
154
|
Secret,
|
|
@@ -104,6 +156,10 @@ export enum KeyType {
|
|
|
104
156
|
Private,
|
|
105
157
|
}
|
|
106
158
|
|
|
159
|
+
export type KTypePrivate = 'pkcs1' | 'pkcs8' | 'sec1';
|
|
160
|
+
export type KTypePublic = 'pkcs1' | 'spki';
|
|
161
|
+
export type KType = KTypePrivate | KTypePublic;
|
|
162
|
+
|
|
107
163
|
// Same as KFormatType, this enum needs to be defined on the native side
|
|
108
164
|
export enum KWebCryptoKeyFormat {
|
|
109
165
|
kWebCryptoKeyFormatRaw,
|
|
@@ -133,10 +189,10 @@ export enum KeyEncoding {
|
|
|
133
189
|
}
|
|
134
190
|
|
|
135
191
|
export type EncodingOptions = {
|
|
136
|
-
key
|
|
137
|
-
type?:
|
|
192
|
+
key?: any;
|
|
193
|
+
type?: KType;
|
|
138
194
|
encoding?: string;
|
|
139
|
-
format?:
|
|
195
|
+
format?: KFormat;
|
|
140
196
|
padding?: number;
|
|
141
197
|
cipher?: string;
|
|
142
198
|
passphrase?: string | ArrayBuffer;
|
|
@@ -181,6 +237,13 @@ export type CryptoKeyPair = {
|
|
|
181
237
|
privateKey: KeyPairKey;
|
|
182
238
|
};
|
|
183
239
|
|
|
240
|
+
export enum CipherOrWrapMode {
|
|
241
|
+
kWebCryptoCipherEncrypt,
|
|
242
|
+
kWebCryptoCipherDecrypt,
|
|
243
|
+
// kWebCryptoWrapKey,
|
|
244
|
+
// kWebCryptoUnwrapKey,
|
|
245
|
+
}
|
|
246
|
+
|
|
184
247
|
function option(name: string, objName: string | undefined) {
|
|
185
248
|
return objName === undefined
|
|
186
249
|
? `options.${name}`
|
|
@@ -322,14 +385,7 @@ function parseKeyEncoding(
|
|
|
322
385
|
}
|
|
323
386
|
|
|
324
387
|
function prepareAsymmetricKey(
|
|
325
|
-
key:
|
|
326
|
-
| BinaryLike
|
|
327
|
-
| {
|
|
328
|
-
key: any;
|
|
329
|
-
encoding?: string;
|
|
330
|
-
format?: any;
|
|
331
|
-
passphrase?: string | ArrayBuffer;
|
|
332
|
-
},
|
|
388
|
+
key: BinaryLike | EncodingOptions,
|
|
333
389
|
ctx: KeyInputContext
|
|
334
390
|
): {
|
|
335
391
|
format: KFormatType;
|
|
@@ -352,14 +408,12 @@ function prepareAsymmetricKey(
|
|
|
352
408
|
data: binaryLikeToArrayBuffer(key),
|
|
353
409
|
};
|
|
354
410
|
} else if (typeof key === 'object') {
|
|
355
|
-
const {
|
|
356
|
-
key: data,
|
|
357
|
-
encoding,
|
|
358
|
-
// format
|
|
359
|
-
} = key;
|
|
411
|
+
const { key: data, encoding } = key;
|
|
360
412
|
// // The 'key' property can be a KeyObject as well to allow specifying
|
|
361
413
|
// // additional options such as padding along with the key.
|
|
362
|
-
// if (isKeyObject(data))
|
|
414
|
+
// if (isKeyObject(data)) {
|
|
415
|
+
// return { data: getKeyObjectHandle(data, ctx) };
|
|
416
|
+
// }
|
|
363
417
|
// else if (isCryptoKey(data))
|
|
364
418
|
// return { data: getKeyObjectHandle(data[kKeyObject], ctx) };
|
|
365
419
|
// else if (isJwk(data) && format === 'jwk')
|
|
@@ -392,11 +446,7 @@ export function preparePrivateKey(key: BinaryLike | EncodingOptions) {
|
|
|
392
446
|
}
|
|
393
447
|
|
|
394
448
|
// TODO(osp) any here is a node KeyObject
|
|
395
|
-
export function preparePublicOrPrivateKey(
|
|
396
|
-
key:
|
|
397
|
-
| BinaryLike
|
|
398
|
-
| { key: any; encoding?: string; format?: any; padding?: number }
|
|
399
|
-
) {
|
|
449
|
+
export function preparePublicOrPrivateKey(key: BinaryLike | EncodingOptions) {
|
|
400
450
|
return prepareAsymmetricKey(key, KeyInputContext.kConsumePublic);
|
|
401
451
|
}
|
|
402
452
|
|
|
@@ -422,6 +472,29 @@ export function parsePrivateKeyEncoding(
|
|
|
422
472
|
return parseKeyEncoding(enc, keyType, false, objName);
|
|
423
473
|
}
|
|
424
474
|
|
|
475
|
+
// function getKeyObjectHandle(key: any, ctx: KeyInputContext) {
|
|
476
|
+
// if (ctx === KeyInputContext.kConsumePublic) {
|
|
477
|
+
// throw new Error(
|
|
478
|
+
// 'Invalid argument type for "key". Need ArrayBuffer, TypeArray, KeyObject, CryptoKey, string'
|
|
479
|
+
// );
|
|
480
|
+
// }
|
|
481
|
+
|
|
482
|
+
// if (key.type !== 'private') {
|
|
483
|
+
// if (
|
|
484
|
+
// ctx === KeyInputContext.kConsumePrivate ||
|
|
485
|
+
// ctx === KeyInputContext.kCreatePublic
|
|
486
|
+
// )
|
|
487
|
+
// throw new Error(`Invalid KeyObject type: ${key.type}, expected 'public'`);
|
|
488
|
+
// if (key.type !== 'public') {
|
|
489
|
+
// throw new Error(
|
|
490
|
+
// `Invalid KeyObject type: ${key.type}, expected 'private' or 'public'`
|
|
491
|
+
// );
|
|
492
|
+
// }
|
|
493
|
+
// }
|
|
494
|
+
|
|
495
|
+
// return key.handle;
|
|
496
|
+
// }
|
|
497
|
+
|
|
425
498
|
function prepareSecretKey(
|
|
426
499
|
key: BinaryLike,
|
|
427
500
|
encoding?: string,
|
|
@@ -467,6 +540,42 @@ export function createSecretKey(key: any, encoding?: string) {
|
|
|
467
540
|
return new SecretKeyObject(handle);
|
|
468
541
|
}
|
|
469
542
|
|
|
543
|
+
export function createPublicKey(
|
|
544
|
+
key: BinaryLike | EncodingOptions
|
|
545
|
+
): PublicKeyObject {
|
|
546
|
+
const { format, type, data, passphrase } = prepareAsymmetricKey(
|
|
547
|
+
key,
|
|
548
|
+
KeyInputContext.kCreatePublic
|
|
549
|
+
);
|
|
550
|
+
const handle = NativeQuickCrypto.webcrypto.createKeyObjectHandle();
|
|
551
|
+
if (format === KFormatType.kKeyFormatJWK) {
|
|
552
|
+
handle.init(KeyType.Public, data);
|
|
553
|
+
} else {
|
|
554
|
+
handle.init(KeyType.Public, data, format, type, passphrase);
|
|
555
|
+
}
|
|
556
|
+
return new PublicKeyObject(handle);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
export const createPrivateKey = (
|
|
560
|
+
key: BinaryLike | EncodingOptions
|
|
561
|
+
): PrivateKeyObject => {
|
|
562
|
+
const { format, type, data, passphrase } = prepareAsymmetricKey(
|
|
563
|
+
key,
|
|
564
|
+
KeyInputContext.kCreatePrivate
|
|
565
|
+
);
|
|
566
|
+
const handle = NativeQuickCrypto.webcrypto.createKeyObjectHandle();
|
|
567
|
+
if (format === KFormatType.kKeyFormatJWK) {
|
|
568
|
+
handle.init(KeyType.Private, data);
|
|
569
|
+
} else {
|
|
570
|
+
handle.init(KeyType.Private, data, format, type, passphrase);
|
|
571
|
+
}
|
|
572
|
+
return new PrivateKeyObject(handle);
|
|
573
|
+
};
|
|
574
|
+
|
|
575
|
+
// const isKeyObject = (obj: any): obj is KeyObject => {
|
|
576
|
+
// return obj != null && obj.keyType !== undefined;
|
|
577
|
+
// };
|
|
578
|
+
|
|
470
579
|
export class CryptoKey {
|
|
471
580
|
keyObject: KeyObject;
|
|
472
581
|
keyAlgorithm: SubtleAlgorithm;
|
|
@@ -572,7 +681,7 @@ export class SecretKeyObject extends KeyObject {
|
|
|
572
681
|
// return this[kHandle].getSymmetricKeySize();
|
|
573
682
|
// }
|
|
574
683
|
|
|
575
|
-
export(options
|
|
684
|
+
export(options?: EncodingOptions) {
|
|
576
685
|
if (options !== undefined) {
|
|
577
686
|
if (options.format === 'jwk') {
|
|
578
687
|
throw new Error('SecretKey export for jwk is not implemented');
|