react-native-quick-crypto 0.7.3 → 1.0.0-beta.2
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/QuickCrypto.podspec +34 -0
- package/README.md +13 -46
- package/android/CMakeLists.txt +32 -64
- package/android/build.gradle +52 -125
- package/android/gradle.properties +4 -4
- package/android/src/main/cpp/cpp-adapter.cpp +9 -54
- package/android/src/main/java/com/margelo/nitro/quickcrypto/QuickCryptoPackage.java +38 -0
- package/cpp/random/HybridRandom.cpp +34 -0
- package/cpp/random/HybridRandom.hpp +44 -0
- package/cpp/utils/Utils.hpp +9 -0
- package/ios/QuickCryptoOnLoad.mm +19 -0
- package/lib/commonjs/index.js +39 -39
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/random.js +26 -12
- package/lib/commonjs/random.js.map +1 -1
- package/lib/commonjs/{NativeQuickCrypto/rsa.js → specs/random.nitro.js} +1 -1
- package/lib/commonjs/{NativeQuickCrypto/aes.js.map → specs/random.nitro.js.map} +1 -1
- package/lib/commonjs/utils/conversion.js +22 -0
- package/lib/commonjs/utils/conversion.js.map +1 -0
- package/lib/commonjs/utils/index.js +12 -0
- package/lib/commonjs/utils/index.js.map +1 -0
- package/lib/commonjs/utils/types.js +2 -0
- package/lib/{module/NativeQuickCrypto/aes.js.map → commonjs/utils/types.js.map} +1 -1
- package/lib/module/index.js +38 -38
- package/lib/module/index.js.map +1 -1
- package/lib/module/random.js +26 -12
- package/lib/module/random.js.map +1 -1
- package/lib/module/specs/random.nitro.js +4 -0
- package/lib/module/{NativeQuickCrypto/rsa.js.map → specs/random.nitro.js.map} +1 -1
- package/lib/module/utils/conversion.js +16 -0
- package/lib/module/utils/conversion.js.map +1 -0
- package/lib/module/utils/index.js +8 -0
- package/lib/module/utils/index.js.map +1 -0
- package/lib/module/utils/types.js +2 -0
- package/lib/{commonjs/NativeQuickCrypto/rsa.js.map → module/utils/types.js.map} +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/typescript/index.d.ts +10 -141
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/random.d.ts +4 -5
- package/lib/typescript/random.d.ts.map +1 -1
- package/lib/typescript/specs/random.nitro.d.ts +9 -0
- package/lib/typescript/specs/random.nitro.d.ts.map +1 -0
- package/lib/typescript/utils/conversion.d.ts +4 -0
- package/lib/typescript/utils/conversion.d.ts.map +1 -0
- package/lib/typescript/utils/index.d.ts +6 -0
- package/lib/typescript/utils/index.d.ts.map +1 -0
- package/lib/typescript/utils/types.d.ts +5 -0
- package/lib/typescript/utils/types.d.ts.map +1 -0
- package/nitrogen/generated/android/QuickCrypto+autolinking.cmake +45 -0
- package/nitrogen/generated/android/QuickCrypto+autolinking.gradle +26 -0
- package/nitrogen/generated/ios/QuickCrypto+autolinking.rb +57 -0
- package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Bridge.cpp +9 -0
- package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Bridge.hpp +25 -0
- package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Umbrella.hpp +29 -0
- package/nitrogen/generated/shared/c++/HybridRandomSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridRandomSpec.hpp +62 -0
- package/package.json +50 -106
- package/src/index.ts +38 -48
- package/src/random.ts +51 -48
- package/src/specs/random.nitro.ts +14 -0
- package/src/utils/conversion.ts +16 -0
- package/src/utils/index.ts +6 -0
- package/src/utils/types.ts +15 -0
- package/LICENSE +0 -27
- package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoModule.java +0 -70
- package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoPackage.java +0 -25
- package/cpp/Cipher/MGLCipherHostObject.cpp +0 -663
- package/cpp/Cipher/MGLCipherHostObject.h +0 -90
- package/cpp/Cipher/MGLCreateCipherInstaller.cpp +0 -75
- package/cpp/Cipher/MGLCreateCipherInstaller.h +0 -22
- package/cpp/Cipher/MGLCreateDecipherInstaller.cpp +0 -77
- package/cpp/Cipher/MGLCreateDecipherInstaller.h +0 -17
- package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +0 -134
- package/cpp/Cipher/MGLGenerateKeyPairInstaller.h +0 -35
- package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +0 -63
- package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.h +0 -37
- package/cpp/Cipher/MGLPublicCipher.h +0 -124
- package/cpp/Cipher/MGLPublicCipherInstaller.h +0 -107
- package/cpp/Cipher/MGLRsa.cpp +0 -573
- package/cpp/Cipher/MGLRsa.h +0 -109
- package/cpp/HMAC/MGLHmacHostObject.cpp +0 -95
- package/cpp/HMAC/MGLHmacHostObject.h +0 -39
- package/cpp/HMAC/MGLHmacInstaller.cpp +0 -43
- package/cpp/HMAC/MGLHmacInstaller.h +0 -20
- package/cpp/Hash/MGLHashHostObject.cpp +0 -147
- package/cpp/Hash/MGLHashHostObject.h +0 -53
- package/cpp/Hash/MGLHashInstaller.cpp +0 -44
- package/cpp/Hash/MGLHashInstaller.h +0 -20
- package/cpp/JSIUtils/MGLJSIMacros.h +0 -110
- package/cpp/JSIUtils/MGLJSIUtils.h +0 -41
- package/cpp/JSIUtils/MGLSmartHostObject.cpp +0 -43
- package/cpp/JSIUtils/MGLSmartHostObject.h +0 -46
- package/cpp/JSIUtils/MGLThreadAwareHostObject.cpp +0 -24
- package/cpp/JSIUtils/MGLThreadAwareHostObject.h +0 -43
- package/cpp/JSIUtils/MGLTypedArray.cpp +0 -325
- package/cpp/JSIUtils/MGLTypedArray.h +0 -160
- package/cpp/MGLKeys.cpp +0 -1426
- package/cpp/MGLKeys.h +0 -203
- package/cpp/MGLQuickCryptoHostObject.cpp +0 -127
- package/cpp/MGLQuickCryptoHostObject.h +0 -30
- package/cpp/Random/MGLRandomHostObject.cpp +0 -96
- package/cpp/Random/MGLRandomHostObject.h +0 -27
- package/cpp/Sig/MGLSignHostObjects.cpp +0 -757
- package/cpp/Sig/MGLSignHostObjects.h +0 -128
- package/cpp/Sig/MGLSignInstaller.cpp +0 -24
- package/cpp/Sig/MGLSignInstaller.h +0 -29
- package/cpp/Sig/MGLVerifyInstaller.cpp +0 -24
- package/cpp/Sig/MGLVerifyInstaller.h +0 -22
- package/cpp/Utils/MGLDispatchQueue.cpp +0 -75
- package/cpp/Utils/MGLDispatchQueue.h +0 -55
- package/cpp/Utils/MGLUtils.cpp +0 -295
- package/cpp/Utils/MGLUtils.h +0 -358
- package/cpp/Utils/base64.h +0 -320
- package/cpp/Utils/logs.h +0 -38
- package/cpp/Utils/node.h +0 -13
- package/cpp/fastpbkdf2/MGLPbkdf2HostObject.cpp +0 -164
- package/cpp/fastpbkdf2/MGLPbkdf2HostObject.h +0 -29
- package/cpp/fastpbkdf2/fastpbkdf2.c +0 -352
- package/cpp/fastpbkdf2/fastpbkdf2.h +0 -68
- package/cpp/webcrypto/MGLWebCrypto.cpp +0 -125
- package/cpp/webcrypto/MGLWebCrypto.h +0 -32
- package/cpp/webcrypto/crypto_aes.cpp +0 -516
- package/cpp/webcrypto/crypto_aes.h +0 -79
- package/cpp/webcrypto/crypto_ec.cpp +0 -424
- package/cpp/webcrypto/crypto_ec.h +0 -78
- package/cpp/webcrypto/crypto_keygen.cpp +0 -86
- package/cpp/webcrypto/crypto_keygen.h +0 -38
- package/ios/QuickCrypto.xcodeproj/project.pbxproj +0 -274
- package/ios/QuickCryptoModule.h +0 -5
- package/ios/QuickCryptoModule.mm +0 -42
- package/lib/commonjs/Cipher.js +0 -502
- package/lib/commonjs/Cipher.js.map +0 -1
- package/lib/commonjs/Hash.js +0 -99
- package/lib/commonjs/Hash.js.map +0 -1
- package/lib/commonjs/Hashnames.js +0 -87
- package/lib/commonjs/Hashnames.js.map +0 -1
- package/lib/commonjs/Hmac.js +0 -79
- package/lib/commonjs/Hmac.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/Cipher.js +0 -30
- package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js +0 -52
- package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/aes.js +0 -6
- package/lib/commonjs/NativeQuickCrypto/hash.js +0 -2
- package/lib/commonjs/NativeQuickCrypto/hash.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/hmac.js +0 -2
- package/lib/commonjs/NativeQuickCrypto/hmac.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/keygen.js +0 -6
- package/lib/commonjs/NativeQuickCrypto/keygen.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/pbkdf2.js +0 -2
- package/lib/commonjs/NativeQuickCrypto/pbkdf2.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/random.js +0 -2
- package/lib/commonjs/NativeQuickCrypto/random.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/sig.js +0 -19
- package/lib/commonjs/NativeQuickCrypto/sig.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/webcrypto.js +0 -6
- package/lib/commonjs/NativeQuickCrypto/webcrypto.js.map +0 -1
- package/lib/commonjs/Utils.js +0 -608
- package/lib/commonjs/Utils.js.map +0 -1
- package/lib/commonjs/aes.js +0 -281
- package/lib/commonjs/aes.js.map +0 -1
- package/lib/commonjs/constants.js +0 -85
- package/lib/commonjs/constants.js.map +0 -1
- package/lib/commonjs/ec.js +0 -276
- package/lib/commonjs/ec.js.map +0 -1
- package/lib/commonjs/keygen.js +0 -56
- package/lib/commonjs/keygen.js.map +0 -1
- package/lib/commonjs/keys.js +0 -492
- package/lib/commonjs/keys.js.map +0 -1
- package/lib/commonjs/pbkdf2.js +0 -90
- package/lib/commonjs/pbkdf2.js.map +0 -1
- package/lib/commonjs/rsa.js +0 -248
- package/lib/commonjs/rsa.js.map +0 -1
- package/lib/commonjs/sig.js +0 -129
- package/lib/commonjs/sig.js.map +0 -1
- package/lib/commonjs/subtle.js +0 -448
- package/lib/commonjs/subtle.js.map +0 -1
- package/lib/commonjs/webcrypto.js +0 -14
- package/lib/commonjs/webcrypto.js.map +0 -1
- package/lib/module/Cipher.js +0 -491
- package/lib/module/Cipher.js.map +0 -1
- package/lib/module/Hash.js +0 -93
- package/lib/module/Hash.js.map +0 -1
- package/lib/module/Hashnames.js +0 -85
- package/lib/module/Hashnames.js.map +0 -1
- package/lib/module/Hmac.js +0 -74
- package/lib/module/Hmac.js.map +0 -1
- package/lib/module/NativeQuickCrypto/Cipher.js +0 -26
- package/lib/module/NativeQuickCrypto/Cipher.js.map +0 -1
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js +0 -49
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +0 -1
- package/lib/module/NativeQuickCrypto/aes.js +0 -4
- package/lib/module/NativeQuickCrypto/hash.js +0 -2
- package/lib/module/NativeQuickCrypto/hash.js.map +0 -1
- package/lib/module/NativeQuickCrypto/hmac.js +0 -2
- package/lib/module/NativeQuickCrypto/hmac.js.map +0 -1
- package/lib/module/NativeQuickCrypto/keygen.js +0 -4
- package/lib/module/NativeQuickCrypto/keygen.js.map +0 -1
- package/lib/module/NativeQuickCrypto/pbkdf2.js +0 -2
- package/lib/module/NativeQuickCrypto/pbkdf2.js.map +0 -1
- package/lib/module/NativeQuickCrypto/random.js +0 -2
- package/lib/module/NativeQuickCrypto/random.js.map +0 -1
- package/lib/module/NativeQuickCrypto/rsa.js +0 -4
- package/lib/module/NativeQuickCrypto/sig.js +0 -17
- package/lib/module/NativeQuickCrypto/sig.js.map +0 -1
- package/lib/module/NativeQuickCrypto/webcrypto.js +0 -4
- package/lib/module/NativeQuickCrypto/webcrypto.js.map +0 -1
- package/lib/module/Utils.js +0 -539
- package/lib/module/Utils.js.map +0 -1
- package/lib/module/aes.js +0 -274
- package/lib/module/aes.js.map +0 -1
- package/lib/module/constants.js +0 -81
- package/lib/module/constants.js.map +0 -1
- package/lib/module/ec.js +0 -267
- package/lib/module/ec.js.map +0 -1
- package/lib/module/keygen.js +0 -49
- package/lib/module/keygen.js.map +0 -1
- package/lib/module/keys.js +0 -477
- package/lib/module/keys.js.map +0 -1
- package/lib/module/pbkdf2.js +0 -84
- package/lib/module/pbkdf2.js.map +0 -1
- package/lib/module/rsa.js +0 -242
- package/lib/module/rsa.js.map +0 -1
- package/lib/module/sig.js +0 -124
- package/lib/module/sig.js.map +0 -1
- package/lib/module/subtle.js +0 -443
- package/lib/module/subtle.js.map +0 -1
- package/lib/module/webcrypto.js +0 -10
- package/lib/module/webcrypto.js.map +0 -1
- package/lib/typescript/Cipher.d.ts +0 -81
- package/lib/typescript/Cipher.d.ts.map +0 -1
- package/lib/typescript/Hash.d.ts +0 -44
- package/lib/typescript/Hash.d.ts.map +0 -1
- package/lib/typescript/Hashnames.d.ts +0 -11
- package/lib/typescript/Hashnames.d.ts.map +0 -1
- package/lib/typescript/Hmac.d.ts +0 -37
- package/lib/typescript/Hmac.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +0 -44
- package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +0 -33
- package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/aes.d.ts +0 -5
- package/lib/typescript/NativeQuickCrypto/aes.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/hash.d.ts +0 -7
- package/lib/typescript/NativeQuickCrypto/hash.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/hmac.d.ts +0 -6
- package/lib/typescript/NativeQuickCrypto/hmac.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/keygen.d.ts +0 -4
- package/lib/typescript/NativeQuickCrypto/keygen.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts +0 -5
- package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/random.d.ts +0 -5
- package/lib/typescript/NativeQuickCrypto/random.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/rsa.d.ts +0 -5
- package/lib/typescript/NativeQuickCrypto/rsa.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/sig.d.ts +0 -23
- package/lib/typescript/NativeQuickCrypto/sig.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts +0 -39
- package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +0 -1
- package/lib/typescript/Utils.d.ts +0 -48
- package/lib/typescript/Utils.d.ts.map +0 -1
- package/lib/typescript/aes.d.ts +0 -22
- package/lib/typescript/aes.d.ts.map +0 -1
- package/lib/typescript/constants.d.ts +0 -76
- package/lib/typescript/constants.d.ts.map +0 -1
- package/lib/typescript/ec.d.ts +0 -7
- package/lib/typescript/ec.d.ts.map +0 -1
- package/lib/typescript/keygen.d.ts +0 -6
- package/lib/typescript/keygen.d.ts.map +0 -1
- package/lib/typescript/keys.d.ts +0 -206
- package/lib/typescript/keys.d.ts.map +0 -1
- package/lib/typescript/pbkdf2.d.ts +0 -12
- package/lib/typescript/pbkdf2.d.ts.map +0 -1
- package/lib/typescript/rsa.d.ts +0 -12
- package/lib/typescript/rsa.d.ts.map +0 -1
- package/lib/typescript/sig.d.ts +0 -21
- package/lib/typescript/sig.d.ts.map +0 -1
- package/lib/typescript/subtle.d.ts +0 -15
- package/lib/typescript/subtle.d.ts.map +0 -1
- package/lib/typescript/webcrypto.d.ts +0 -9
- package/lib/typescript/webcrypto.d.ts.map +0 -1
- package/react-native-quick-crypto.podspec +0 -40
- package/src/Cipher.ts +0 -832
- package/src/Hash.ts +0 -132
- package/src/Hashnames.ts +0 -93
- package/src/Hmac.ts +0 -106
- package/src/NativeQuickCrypto/Cipher.ts +0 -102
- package/src/NativeQuickCrypto/NativeQuickCrypto.ts +0 -102
- package/src/NativeQuickCrypto/aes.ts +0 -14
- package/src/NativeQuickCrypto/hash.ts +0 -10
- package/src/NativeQuickCrypto/hmac.ts +0 -9
- package/src/NativeQuickCrypto/keygen.ts +0 -7
- package/src/NativeQuickCrypto/pbkdf2.ts +0 -16
- package/src/NativeQuickCrypto/random.ts +0 -12
- package/src/NativeQuickCrypto/rsa.ts +0 -12
- package/src/NativeQuickCrypto/sig.ts +0 -44
- package/src/NativeQuickCrypto/webcrypto.ts +0 -72
- package/src/Utils.ts +0 -777
- package/src/aes.ts +0 -402
- package/src/constants.ts +0 -79
- package/src/ec.ts +0 -375
- package/src/keygen.ts +0 -80
- package/src/keys.ts +0 -787
- package/src/pbkdf2.ts +0 -169
- package/src/rsa.ts +0 -370
- package/src/sig.ts +0 -164
- package/src/subtle.ts +0 -639
- package/src/webcrypto.ts +0 -8
- /package/android/src/main/{AndroidManifestNew.xml → AndroidManifest.xml} +0 -0
package/src/keys.ts
DELETED
|
@@ -1,787 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type BinaryLike,
|
|
3
|
-
binaryLikeToArrayBuffer,
|
|
4
|
-
isStringOrBuffer,
|
|
5
|
-
type BufferLike,
|
|
6
|
-
type TypedArray,
|
|
7
|
-
} from './Utils';
|
|
8
|
-
import type { KeyObjectHandle } from './NativeQuickCrypto/webcrypto';
|
|
9
|
-
import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';
|
|
10
|
-
import type { KeyPairKey } from './Cipher';
|
|
11
|
-
|
|
12
|
-
export const kNamedCurveAliases = {
|
|
13
|
-
'P-256': 'prime256v1',
|
|
14
|
-
'P-384': 'secp384r1',
|
|
15
|
-
'P-521': 'secp521r1',
|
|
16
|
-
} as const;
|
|
17
|
-
|
|
18
|
-
export type NamedCurve = 'P-256' | 'P-384' | 'P-521';
|
|
19
|
-
|
|
20
|
-
export type ImportFormat = 'raw' | 'pkcs8' | 'spki' | 'jwk';
|
|
21
|
-
|
|
22
|
-
export type AnyAlgorithm =
|
|
23
|
-
| HashAlgorithm
|
|
24
|
-
| KeyPairAlgorithm
|
|
25
|
-
| SecretKeyAlgorithm
|
|
26
|
-
| SignVerifyAlgorithm
|
|
27
|
-
| DeriveBitsAlgorithm
|
|
28
|
-
| EncryptDecryptAlgorithm
|
|
29
|
-
| 'PBKDF2'
|
|
30
|
-
| 'HKDF';
|
|
31
|
-
|
|
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';
|
|
41
|
-
|
|
42
|
-
export type KeyPairType = 'rsa' | 'rsa-pss' | 'ec';
|
|
43
|
-
|
|
44
|
-
export type RSAKeyPairAlgorithm = 'RSASSA-PKCS1-v1_5' | 'RSA-PSS' | 'RSA-OAEP';
|
|
45
|
-
export type ECKeyPairAlgorithm = 'ECDSA' | 'ECDH';
|
|
46
|
-
export type CFRGKeyPairAlgorithm = 'Ed25519' | 'Ed448' | 'X25519' | 'X448';
|
|
47
|
-
export type AESAlgorithm = 'AES-CTR' | 'AES-CBC' | 'AES-GCM' | 'AES-KW';
|
|
48
|
-
|
|
49
|
-
export type KeyPairAlgorithm =
|
|
50
|
-
| RSAKeyPairAlgorithm
|
|
51
|
-
| ECKeyPairAlgorithm
|
|
52
|
-
| CFRGKeyPairAlgorithm;
|
|
53
|
-
|
|
54
|
-
export type SecretKeyAlgorithm = 'HMAC' | AESAlgorithm;
|
|
55
|
-
export type SecretKeyType = 'hmac' | 'aes';
|
|
56
|
-
|
|
57
|
-
export type SignVerifyAlgorithm =
|
|
58
|
-
| 'RSASSA-PKCS1-v1_5'
|
|
59
|
-
| 'RSA-PSS'
|
|
60
|
-
| 'ECDSA'
|
|
61
|
-
| 'HMAC'
|
|
62
|
-
| 'Ed25519'
|
|
63
|
-
| 'Ed448';
|
|
64
|
-
|
|
65
|
-
export type DeriveBitsAlgorithm =
|
|
66
|
-
| 'PBKDF2'
|
|
67
|
-
| 'HKDF'
|
|
68
|
-
| 'ECDH'
|
|
69
|
-
| 'X25519'
|
|
70
|
-
| 'X448';
|
|
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
|
-
|
|
115
|
-
export type EncryptDecryptAlgorithm =
|
|
116
|
-
| 'RSA-OAEP'
|
|
117
|
-
| 'AES-CTR'
|
|
118
|
-
| 'AES-CBC'
|
|
119
|
-
| 'AES-GCM';
|
|
120
|
-
|
|
121
|
-
export type SubtleAlgorithm = {
|
|
122
|
-
name: AnyAlgorithm;
|
|
123
|
-
salt?: string;
|
|
124
|
-
iterations?: number;
|
|
125
|
-
hash?: HashAlgorithm;
|
|
126
|
-
namedCurve?: NamedCurve;
|
|
127
|
-
length?: number;
|
|
128
|
-
modulusLength?: number;
|
|
129
|
-
publicExponent?: any;
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
export type KeyUsage =
|
|
133
|
-
| 'encrypt'
|
|
134
|
-
| 'decrypt'
|
|
135
|
-
| 'sign'
|
|
136
|
-
| 'verify'
|
|
137
|
-
| 'deriveKey'
|
|
138
|
-
| 'deriveBits'
|
|
139
|
-
| 'wrapKey'
|
|
140
|
-
| 'unwrapKey';
|
|
141
|
-
|
|
142
|
-
// On node this value is defined on the native side, for now I'm just creating it here in JS
|
|
143
|
-
// TODO(osp) move this into native side to make sure they always match
|
|
144
|
-
export enum KFormatType {
|
|
145
|
-
kKeyFormatDER,
|
|
146
|
-
kKeyFormatPEM,
|
|
147
|
-
kKeyFormatJWK,
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export type KFormat = 'der' | 'pem' | 'jwk';
|
|
151
|
-
|
|
152
|
-
// Same as KFormatType, this enum needs to be defined on the native side
|
|
153
|
-
export enum KeyType {
|
|
154
|
-
Secret,
|
|
155
|
-
Public,
|
|
156
|
-
Private,
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
export type KTypePrivate = 'pkcs1' | 'pkcs8' | 'sec1';
|
|
160
|
-
export type KTypePublic = 'pkcs1' | 'spki';
|
|
161
|
-
export type KType = KTypePrivate | KTypePublic;
|
|
162
|
-
|
|
163
|
-
// Same as KFormatType, this enum needs to be defined on the native side
|
|
164
|
-
export enum KWebCryptoKeyFormat {
|
|
165
|
-
kWebCryptoKeyFormatRaw,
|
|
166
|
-
kWebCryptoKeyFormatPKCS8,
|
|
167
|
-
kWebCryptoKeyFormatSPKI,
|
|
168
|
-
kWebCryptoKeyFormatJWK,
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
export enum WebCryptoKeyExportStatus {
|
|
172
|
-
OK,
|
|
173
|
-
INVALID_KEY_TYPE,
|
|
174
|
-
FAILED,
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
enum KeyInputContext {
|
|
178
|
-
kConsumePublic,
|
|
179
|
-
kConsumePrivate,
|
|
180
|
-
kCreatePublic,
|
|
181
|
-
kCreatePrivate,
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
export enum KeyEncoding {
|
|
185
|
-
kKeyEncodingPKCS1,
|
|
186
|
-
kKeyEncodingPKCS8,
|
|
187
|
-
kKeyEncodingSPKI,
|
|
188
|
-
kKeyEncodingSEC1,
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
export type DSAEncoding = 'der' | 'ieee-p1363';
|
|
192
|
-
|
|
193
|
-
export type EncodingOptions = {
|
|
194
|
-
key?: any;
|
|
195
|
-
type?: KType;
|
|
196
|
-
encoding?: string;
|
|
197
|
-
dsaEncoding?: DSAEncoding;
|
|
198
|
-
format?: KFormat;
|
|
199
|
-
padding?: number;
|
|
200
|
-
cipher?: string;
|
|
201
|
-
passphrase?: string | ArrayBuffer;
|
|
202
|
-
saltLength?: number;
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
export type AsymmetricKeyType = 'rsa' | 'rsa-pss' | 'dsa' | 'ec' | undefined;
|
|
206
|
-
|
|
207
|
-
export type JWK = {
|
|
208
|
-
'kty'?: 'AES' | 'RSA' | 'EC' | 'oct';
|
|
209
|
-
'use'?: 'sig' | 'enc';
|
|
210
|
-
'key_ops'?: KeyUsage[];
|
|
211
|
-
'alg'?: string; // TODO: enumerate these (RFC-7517)
|
|
212
|
-
'crv'?: string;
|
|
213
|
-
'kid'?: string;
|
|
214
|
-
'x5u'?: string;
|
|
215
|
-
'x5c'?: string[];
|
|
216
|
-
'x5t'?: string;
|
|
217
|
-
'x5t#256'?: string;
|
|
218
|
-
'n'?: string;
|
|
219
|
-
'e'?: string;
|
|
220
|
-
'd'?: string;
|
|
221
|
-
'p'?: string;
|
|
222
|
-
'q'?: string;
|
|
223
|
-
'x'?: string;
|
|
224
|
-
'y'?: string;
|
|
225
|
-
'k'?: string;
|
|
226
|
-
'dp'?: string;
|
|
227
|
-
'dq'?: string;
|
|
228
|
-
'qi'?: string;
|
|
229
|
-
'ext'?: boolean;
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
const encodingNames = {
|
|
233
|
-
[KeyEncoding.kKeyEncodingPKCS1]: 'pkcs1',
|
|
234
|
-
[KeyEncoding.kKeyEncodingPKCS8]: 'pkcs8',
|
|
235
|
-
[KeyEncoding.kKeyEncodingSPKI]: 'spki',
|
|
236
|
-
[KeyEncoding.kKeyEncodingSEC1]: 'sec1',
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
export type CryptoKeyPair = {
|
|
240
|
-
publicKey: KeyPairKey;
|
|
241
|
-
privateKey: KeyPairKey;
|
|
242
|
-
};
|
|
243
|
-
|
|
244
|
-
export enum CipherOrWrapMode {
|
|
245
|
-
kWebCryptoCipherEncrypt,
|
|
246
|
-
kWebCryptoCipherDecrypt,
|
|
247
|
-
// kWebCryptoWrapKey,
|
|
248
|
-
// kWebCryptoUnwrapKey,
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
function option(name: string, objName: string | undefined) {
|
|
252
|
-
return objName === undefined
|
|
253
|
-
? `options.${name}`
|
|
254
|
-
: `options.${objName}.${name}`;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function parseKeyFormat(
|
|
258
|
-
formatStr: string | undefined,
|
|
259
|
-
defaultFormat: KFormatType | undefined,
|
|
260
|
-
optionName?: string
|
|
261
|
-
) {
|
|
262
|
-
if (formatStr === undefined && defaultFormat !== undefined)
|
|
263
|
-
return defaultFormat;
|
|
264
|
-
else if (formatStr === 'pem') return KFormatType.kKeyFormatPEM;
|
|
265
|
-
else if (formatStr === 'der') return KFormatType.kKeyFormatDER;
|
|
266
|
-
else if (formatStr === 'jwk') return KFormatType.kKeyFormatJWK;
|
|
267
|
-
throw new Error(`Invalid key format str: ${optionName}`);
|
|
268
|
-
// throw new ERR_INVALID_ARG_VALUE(optionName, formatStr);
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
function parseKeyType(
|
|
272
|
-
typeStr: string | undefined,
|
|
273
|
-
required: boolean,
|
|
274
|
-
keyType: string | undefined,
|
|
275
|
-
isPublic: boolean | undefined,
|
|
276
|
-
optionName: string
|
|
277
|
-
): KeyEncoding | undefined {
|
|
278
|
-
if (typeStr === undefined && !required) {
|
|
279
|
-
return undefined;
|
|
280
|
-
} else if (typeStr === 'pkcs1') {
|
|
281
|
-
if (keyType !== undefined && keyType !== 'rsa') {
|
|
282
|
-
throw new Error(
|
|
283
|
-
`Crypto incompatible key options: ${typeStr} can only be used for RSA keys`
|
|
284
|
-
);
|
|
285
|
-
}
|
|
286
|
-
return KeyEncoding.kKeyEncodingPKCS1;
|
|
287
|
-
} else if (typeStr === 'spki' && isPublic !== false) {
|
|
288
|
-
return KeyEncoding.kKeyEncodingSPKI;
|
|
289
|
-
} else if (typeStr === 'pkcs8' && isPublic !== true) {
|
|
290
|
-
return KeyEncoding.kKeyEncodingPKCS8;
|
|
291
|
-
} else if (typeStr === 'sec1' && isPublic !== true) {
|
|
292
|
-
if (keyType !== undefined && keyType !== 'ec') {
|
|
293
|
-
throw new Error(
|
|
294
|
-
`Incompatible key options ${typeStr} can only be used for EC keys`
|
|
295
|
-
);
|
|
296
|
-
}
|
|
297
|
-
return KeyEncoding.kKeyEncodingSEC1;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
throw new Error(`Invalid option ${optionName} - ${typeStr}`);
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
function parseKeyFormatAndType(
|
|
304
|
-
enc: EncodingOptions,
|
|
305
|
-
keyType?: string,
|
|
306
|
-
isPublic?: boolean,
|
|
307
|
-
objName?: string
|
|
308
|
-
) {
|
|
309
|
-
const { format: formatStr, type: typeStr } = enc;
|
|
310
|
-
|
|
311
|
-
const isInput = keyType === undefined;
|
|
312
|
-
const format = parseKeyFormat(
|
|
313
|
-
formatStr,
|
|
314
|
-
isInput ? KFormatType.kKeyFormatPEM : undefined,
|
|
315
|
-
option('format', objName)
|
|
316
|
-
);
|
|
317
|
-
|
|
318
|
-
const isRequired =
|
|
319
|
-
(!isInput || format === KFormatType.kKeyFormatDER) &&
|
|
320
|
-
format !== KFormatType.kKeyFormatJWK;
|
|
321
|
-
|
|
322
|
-
const type = parseKeyType(
|
|
323
|
-
typeStr,
|
|
324
|
-
isRequired,
|
|
325
|
-
keyType,
|
|
326
|
-
isPublic,
|
|
327
|
-
option('type', objName)
|
|
328
|
-
);
|
|
329
|
-
return { format, type };
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
function parseKeyEncoding(
|
|
333
|
-
enc: EncodingOptions,
|
|
334
|
-
keyType?: string,
|
|
335
|
-
isPublic?: boolean,
|
|
336
|
-
objName?: string
|
|
337
|
-
) {
|
|
338
|
-
// validateObject(enc, 'options');
|
|
339
|
-
|
|
340
|
-
const isInput = keyType === undefined;
|
|
341
|
-
|
|
342
|
-
const { format, type } = parseKeyFormatAndType(
|
|
343
|
-
enc,
|
|
344
|
-
keyType,
|
|
345
|
-
isPublic,
|
|
346
|
-
objName
|
|
347
|
-
);
|
|
348
|
-
|
|
349
|
-
let cipher, passphrase, encoding;
|
|
350
|
-
if (isPublic !== true) {
|
|
351
|
-
({ cipher, passphrase, encoding } = enc);
|
|
352
|
-
|
|
353
|
-
if (!isInput) {
|
|
354
|
-
if (cipher != null) {
|
|
355
|
-
if (typeof cipher !== 'string')
|
|
356
|
-
throw new Error(
|
|
357
|
-
`Invalid argument ${option('cipher', objName)}: ${cipher}`
|
|
358
|
-
);
|
|
359
|
-
if (
|
|
360
|
-
format === KFormatType.kKeyFormatDER &&
|
|
361
|
-
(type === KeyEncoding.kKeyEncodingPKCS1 ||
|
|
362
|
-
type === KeyEncoding.kKeyEncodingSEC1)
|
|
363
|
-
) {
|
|
364
|
-
throw new Error(
|
|
365
|
-
`Incompatible key options ${encodingNames[type]} does not support encryption`
|
|
366
|
-
);
|
|
367
|
-
}
|
|
368
|
-
} else if (passphrase !== undefined) {
|
|
369
|
-
throw new Error(
|
|
370
|
-
`invalid argument ${option('cipher', objName)}: ${cipher}`
|
|
371
|
-
);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
if (
|
|
376
|
-
(isInput && passphrase !== undefined && !isStringOrBuffer(passphrase)) ||
|
|
377
|
-
(!isInput && cipher != null && !isStringOrBuffer(passphrase))
|
|
378
|
-
) {
|
|
379
|
-
throw new Error(
|
|
380
|
-
`Invalid argument value ${option('passphrase', objName)}: ${passphrase}`
|
|
381
|
-
);
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
if (passphrase !== undefined)
|
|
386
|
-
passphrase = binaryLikeToArrayBuffer(passphrase, encoding);
|
|
387
|
-
|
|
388
|
-
return { format, type, cipher, passphrase };
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
function prepareAsymmetricKey(
|
|
392
|
-
key: BinaryLike | EncodingOptions,
|
|
393
|
-
ctx: KeyInputContext
|
|
394
|
-
): {
|
|
395
|
-
format: KFormatType;
|
|
396
|
-
data: ArrayBuffer;
|
|
397
|
-
type?: KeyEncoding;
|
|
398
|
-
passphrase?: string | ArrayBuffer;
|
|
399
|
-
} {
|
|
400
|
-
// TODO(osp) check, KeyObject some node object
|
|
401
|
-
// if (isKeyObject(key)) {
|
|
402
|
-
// // Best case: A key object, as simple as that.
|
|
403
|
-
// return { data: getKeyObjectHandle(key, ctx) };
|
|
404
|
-
// } else
|
|
405
|
-
// if (isCryptoKey(key)) {
|
|
406
|
-
// return { data: getKeyObjectHandle(key[kKeyObject], ctx) };
|
|
407
|
-
// } else
|
|
408
|
-
if (isStringOrBuffer(key)) {
|
|
409
|
-
// Expect PEM by default, mostly for backward compatibility.
|
|
410
|
-
return {
|
|
411
|
-
format: KFormatType.kKeyFormatPEM,
|
|
412
|
-
data: binaryLikeToArrayBuffer(key),
|
|
413
|
-
};
|
|
414
|
-
} else if (typeof key === 'object') {
|
|
415
|
-
const { key: data, encoding } = key;
|
|
416
|
-
// // The 'key' property can be a KeyObject as well to allow specifying
|
|
417
|
-
// // additional options such as padding along with the key.
|
|
418
|
-
// if (isKeyObject(data)) {
|
|
419
|
-
// return { data: getKeyObjectHandle(data, ctx) };
|
|
420
|
-
// }
|
|
421
|
-
// else if (isCryptoKey(data))
|
|
422
|
-
// return { data: getKeyObjectHandle(data[kKeyObject], ctx) };
|
|
423
|
-
// else if (isJwk(data) && format === 'jwk')
|
|
424
|
-
// return { data: getKeyObjectHandleFromJwk(data, ctx), format: 'jwk' };
|
|
425
|
-
// Either PEM or DER using PKCS#1 or SPKI.
|
|
426
|
-
if (!isStringOrBuffer(data)) {
|
|
427
|
-
throw new Error(
|
|
428
|
-
'prepareAsymmetricKey: key is not a string or ArrayBuffer'
|
|
429
|
-
);
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
const isPublic =
|
|
433
|
-
ctx === KeyInputContext.kConsumePrivate ||
|
|
434
|
-
ctx === KeyInputContext.kCreatePrivate
|
|
435
|
-
? false
|
|
436
|
-
: undefined;
|
|
437
|
-
|
|
438
|
-
return {
|
|
439
|
-
data: binaryLikeToArrayBuffer(data, encoding),
|
|
440
|
-
...parseKeyEncoding(key, undefined, isPublic),
|
|
441
|
-
};
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
throw new Error('[prepareAsymetricKey] Invalid argument key: ${key}');
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
// TODO(osp) any here is a node KeyObject
|
|
448
|
-
export function preparePrivateKey(key: BinaryLike | EncodingOptions) {
|
|
449
|
-
return prepareAsymmetricKey(key, KeyInputContext.kConsumePrivate);
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
// TODO(osp) any here is a node KeyObject
|
|
453
|
-
export function preparePublicOrPrivateKey(key: BinaryLike | EncodingOptions) {
|
|
454
|
-
return prepareAsymmetricKey(key, KeyInputContext.kConsumePublic);
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
// Parses the public key encoding based on an object. keyType must be undefined
|
|
458
|
-
// when this is used to parse an input encoding and must be a valid key type if
|
|
459
|
-
// used to parse an output encoding.
|
|
460
|
-
export function parsePublicKeyEncoding(
|
|
461
|
-
enc: EncodingOptions,
|
|
462
|
-
keyType: string | undefined,
|
|
463
|
-
objName?: string
|
|
464
|
-
) {
|
|
465
|
-
return parseKeyEncoding(enc, keyType, keyType ? true : undefined, objName);
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
// Parses the private key encoding based on an object. keyType must be undefined
|
|
469
|
-
// when this is used to parse an input encoding and must be a valid key type if
|
|
470
|
-
// used to parse an output encoding.
|
|
471
|
-
export function parsePrivateKeyEncoding(
|
|
472
|
-
enc: EncodingOptions,
|
|
473
|
-
keyType: string | undefined,
|
|
474
|
-
objName?: string
|
|
475
|
-
) {
|
|
476
|
-
return parseKeyEncoding(enc, keyType, false, objName);
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
// function getKeyObjectHandle(key: any, ctx: KeyInputContext) {
|
|
480
|
-
// if (ctx === KeyInputContext.kConsumePublic) {
|
|
481
|
-
// throw new Error(
|
|
482
|
-
// 'Invalid argument type for "key". Need ArrayBuffer, TypeArray, KeyObject, CryptoKey, string'
|
|
483
|
-
// );
|
|
484
|
-
// }
|
|
485
|
-
|
|
486
|
-
// if (key.type !== 'private') {
|
|
487
|
-
// if (
|
|
488
|
-
// ctx === KeyInputContext.kConsumePrivate ||
|
|
489
|
-
// ctx === KeyInputContext.kCreatePublic
|
|
490
|
-
// )
|
|
491
|
-
// throw new Error(`Invalid KeyObject type: ${key.type}, expected 'public'`);
|
|
492
|
-
// if (key.type !== 'public') {
|
|
493
|
-
// throw new Error(
|
|
494
|
-
// `Invalid KeyObject type: ${key.type}, expected 'private' or 'public'`
|
|
495
|
-
// );
|
|
496
|
-
// }
|
|
497
|
-
// }
|
|
498
|
-
|
|
499
|
-
// return key.handle;
|
|
500
|
-
// }
|
|
501
|
-
|
|
502
|
-
function prepareSecretKey(
|
|
503
|
-
key: BinaryLike,
|
|
504
|
-
encoding?: string,
|
|
505
|
-
bufferOnly = false
|
|
506
|
-
): any {
|
|
507
|
-
try {
|
|
508
|
-
if (!bufferOnly) {
|
|
509
|
-
// TODO: maybe use `key.constructor.name === 'KeyObject'` ?
|
|
510
|
-
if (key instanceof KeyObject) {
|
|
511
|
-
if (key.type !== 'secret')
|
|
512
|
-
throw new Error(
|
|
513
|
-
`invalid KeyObject type: ${key.type}, expected 'secret'`
|
|
514
|
-
);
|
|
515
|
-
return key.handle;
|
|
516
|
-
}
|
|
517
|
-
// TODO: maybe use `key.constructor.name === 'CryptoKey'` ?
|
|
518
|
-
else if (key instanceof CryptoKey) {
|
|
519
|
-
if (key.type !== 'secret')
|
|
520
|
-
throw new Error(
|
|
521
|
-
`invalid CryptoKey type: ${key.type}, expected 'secret'`
|
|
522
|
-
);
|
|
523
|
-
return key.keyObject.handle;
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
if (key instanceof ArrayBuffer) {
|
|
528
|
-
return key;
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
return binaryLikeToArrayBuffer(key, encoding);
|
|
532
|
-
} catch (error) {
|
|
533
|
-
throw new Error(
|
|
534
|
-
'Invalid argument type for "key". Need ArrayBuffer, TypedArray, KeyObject, CryptoKey, string',
|
|
535
|
-
{ cause: error }
|
|
536
|
-
);
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
export function createSecretKey(key: any, encoding?: string) {
|
|
541
|
-
const k = prepareSecretKey(key, encoding, true);
|
|
542
|
-
const handle = NativeQuickCrypto.webcrypto.createKeyObjectHandle();
|
|
543
|
-
handle.init(KeyType.Secret, k);
|
|
544
|
-
return new SecretKeyObject(handle);
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
export function createPublicKey(
|
|
548
|
-
key: BinaryLike | EncodingOptions
|
|
549
|
-
): PublicKeyObject {
|
|
550
|
-
const { format, type, data, passphrase } = prepareAsymmetricKey(
|
|
551
|
-
key,
|
|
552
|
-
KeyInputContext.kCreatePublic
|
|
553
|
-
);
|
|
554
|
-
const handle = NativeQuickCrypto.webcrypto.createKeyObjectHandle();
|
|
555
|
-
if (format === KFormatType.kKeyFormatJWK) {
|
|
556
|
-
handle.init(KeyType.Public, data);
|
|
557
|
-
} else {
|
|
558
|
-
handle.init(KeyType.Public, data, format, type, passphrase);
|
|
559
|
-
}
|
|
560
|
-
return new PublicKeyObject(handle);
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
export const createPrivateKey = (
|
|
564
|
-
key: BinaryLike | EncodingOptions
|
|
565
|
-
): PrivateKeyObject => {
|
|
566
|
-
const { format, type, data, passphrase } = prepareAsymmetricKey(
|
|
567
|
-
key,
|
|
568
|
-
KeyInputContext.kCreatePrivate
|
|
569
|
-
);
|
|
570
|
-
const handle = NativeQuickCrypto.webcrypto.createKeyObjectHandle();
|
|
571
|
-
if (format === KFormatType.kKeyFormatJWK) {
|
|
572
|
-
handle.init(KeyType.Private, data);
|
|
573
|
-
} else {
|
|
574
|
-
handle.init(KeyType.Private, data, format, type, passphrase);
|
|
575
|
-
}
|
|
576
|
-
return new PrivateKeyObject(handle);
|
|
577
|
-
};
|
|
578
|
-
|
|
579
|
-
// const isKeyObject = (obj: any): obj is KeyObject => {
|
|
580
|
-
// return obj != null && obj.keyType !== undefined;
|
|
581
|
-
// };
|
|
582
|
-
|
|
583
|
-
export class CryptoKey {
|
|
584
|
-
keyObject: KeyObject;
|
|
585
|
-
keyAlgorithm: SubtleAlgorithm;
|
|
586
|
-
keyUsages: KeyUsage[];
|
|
587
|
-
keyExtractable: boolean;
|
|
588
|
-
|
|
589
|
-
constructor(
|
|
590
|
-
keyObject: KeyObject,
|
|
591
|
-
keyAlgorithm: SubtleAlgorithm,
|
|
592
|
-
keyUsages: KeyUsage[],
|
|
593
|
-
keyExtractable: boolean
|
|
594
|
-
) {
|
|
595
|
-
this.keyObject = keyObject;
|
|
596
|
-
this.keyAlgorithm = keyAlgorithm;
|
|
597
|
-
this.keyUsages = keyUsages;
|
|
598
|
-
this.keyExtractable = keyExtractable;
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
inspect(_depth: number, _options: any): any {
|
|
602
|
-
throw new Error('CryptoKey.inspect is not implemented');
|
|
603
|
-
// if (depth < 0) return this;
|
|
604
|
-
|
|
605
|
-
// const opts = {
|
|
606
|
-
// ...options,
|
|
607
|
-
// depth: options.depth == null ? null : options.depth - 1,
|
|
608
|
-
// };
|
|
609
|
-
|
|
610
|
-
// return `CryptoKey ${inspect(
|
|
611
|
-
// {
|
|
612
|
-
// type: this.type,
|
|
613
|
-
// extractable: this.extractable,
|
|
614
|
-
// algorithm: this.algorithm,
|
|
615
|
-
// usages: this.usages,
|
|
616
|
-
// },
|
|
617
|
-
// opts
|
|
618
|
-
// )}`;
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
get type() {
|
|
622
|
-
// if (!(this instanceof CryptoKey)) throw new Error('Invalid CryptoKey');
|
|
623
|
-
return this.keyObject.type;
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
get extractable() {
|
|
627
|
-
return this.keyExtractable;
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
get algorithm() {
|
|
631
|
-
return this.keyAlgorithm;
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
get usages() {
|
|
635
|
-
return this.keyUsages;
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
class KeyObject {
|
|
640
|
-
handle: KeyObjectHandle;
|
|
641
|
-
type: 'public' | 'secret' | 'private' | 'unknown' = 'unknown';
|
|
642
|
-
export(_options?: EncodingOptions): ArrayBuffer {
|
|
643
|
-
return new ArrayBuffer(0);
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
constructor(type: string, handle: KeyObjectHandle) {
|
|
647
|
-
if (type !== 'secret' && type !== 'public' && type !== 'private')
|
|
648
|
-
throw new Error(`invalid KeyObject type: ${type}`);
|
|
649
|
-
this.handle = handle;
|
|
650
|
-
this.type = type;
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
// get type(): string {
|
|
654
|
-
// return this.type;
|
|
655
|
-
// }
|
|
656
|
-
|
|
657
|
-
// static from(key) {
|
|
658
|
-
// if (!isCryptoKey(key))
|
|
659
|
-
// throw new ERR_INVALID_ARG_TYPE('key', 'CryptoKey', key);
|
|
660
|
-
// return key[kKeyObject];
|
|
661
|
-
// }
|
|
662
|
-
|
|
663
|
-
// equals(otherKeyObject) {
|
|
664
|
-
// if (!isKeyObject(otherKeyObject)) {
|
|
665
|
-
// throw new ERR_INVALID_ARG_TYPE(
|
|
666
|
-
// 'otherKeyObject',
|
|
667
|
-
// 'KeyObject',
|
|
668
|
-
// otherKeyObject
|
|
669
|
-
// );
|
|
670
|
-
// }
|
|
671
|
-
|
|
672
|
-
// return (
|
|
673
|
-
// otherKeyObject.type === this.type &&
|
|
674
|
-
// this[kHandle].equals(otherKeyObject[kHandle])
|
|
675
|
-
// );
|
|
676
|
-
// }
|
|
677
|
-
}
|
|
678
|
-
|
|
679
|
-
export class SecretKeyObject extends KeyObject {
|
|
680
|
-
constructor(handle: KeyObjectHandle) {
|
|
681
|
-
super('secret', handle);
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
// get symmetricKeySize() {
|
|
685
|
-
// return this[kHandle].getSymmetricKeySize();
|
|
686
|
-
// }
|
|
687
|
-
|
|
688
|
-
export(options?: EncodingOptions) {
|
|
689
|
-
if (options !== undefined) {
|
|
690
|
-
if (options.format === 'jwk') {
|
|
691
|
-
throw new Error('SecretKey export for jwk is not implemented');
|
|
692
|
-
// return this.handle.exportJwk({}, false);
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
return this.handle.export();
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
// const kAsymmetricKeyType = Symbol('kAsymmetricKeyType');
|
|
700
|
-
// const kAsymmetricKeyDetails = Symbol('kAsymmetricKeyDetails');
|
|
701
|
-
|
|
702
|
-
// function normalizeKeyDetails(details = {}) {
|
|
703
|
-
// if (details.publicExponent !== undefined) {
|
|
704
|
-
// return {
|
|
705
|
-
// ...details,
|
|
706
|
-
// publicExponent: bigIntArrayToUnsignedBigInt(
|
|
707
|
-
// new Uint8Array(details.publicExponent)
|
|
708
|
-
// ),
|
|
709
|
-
// };
|
|
710
|
-
// }
|
|
711
|
-
// return details;
|
|
712
|
-
// }
|
|
713
|
-
|
|
714
|
-
class AsymmetricKeyObject extends KeyObject {
|
|
715
|
-
constructor(type: string, handle: KeyObjectHandle) {
|
|
716
|
-
super(type, handle);
|
|
717
|
-
}
|
|
718
|
-
|
|
719
|
-
private _asymmetricKeyType?: AsymmetricKeyType;
|
|
720
|
-
|
|
721
|
-
get asymmetricKeyType(): AsymmetricKeyType {
|
|
722
|
-
if (!this._asymmetricKeyType) {
|
|
723
|
-
this._asymmetricKeyType = this.handle.getAsymmetricKeyType();
|
|
724
|
-
}
|
|
725
|
-
return this._asymmetricKeyType;
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
// get asymmetricKeyDetails() {
|
|
729
|
-
// switch (this._asymmetricKeyType) {
|
|
730
|
-
// case 'rsa':
|
|
731
|
-
// case 'rsa-pss':
|
|
732
|
-
// case 'dsa':
|
|
733
|
-
// case 'ec':
|
|
734
|
-
// return (
|
|
735
|
-
// this[kAsymmetricKeyDetails] ||
|
|
736
|
-
// (this[kAsymmetricKeyDetails] = normalizeKeyDetails(
|
|
737
|
-
// this[kHandle].keyDetail({})
|
|
738
|
-
// ))
|
|
739
|
-
// );
|
|
740
|
-
// default:
|
|
741
|
-
// return {};
|
|
742
|
-
// }
|
|
743
|
-
// }
|
|
744
|
-
}
|
|
745
|
-
|
|
746
|
-
export class PublicKeyObject extends AsymmetricKeyObject {
|
|
747
|
-
constructor(handle: KeyObjectHandle) {
|
|
748
|
-
super('public', handle);
|
|
749
|
-
}
|
|
750
|
-
|
|
751
|
-
export(options: EncodingOptions) {
|
|
752
|
-
if (options?.format === 'jwk') {
|
|
753
|
-
throw new Error('PublicKey export for jwk is not implemented');
|
|
754
|
-
// return this.handle.exportJwk({}, false);
|
|
755
|
-
}
|
|
756
|
-
const { format, type } = parsePublicKeyEncoding(
|
|
757
|
-
options,
|
|
758
|
-
this.asymmetricKeyType
|
|
759
|
-
);
|
|
760
|
-
return this.handle.export(format, type);
|
|
761
|
-
}
|
|
762
|
-
}
|
|
763
|
-
|
|
764
|
-
export class PrivateKeyObject extends AsymmetricKeyObject {
|
|
765
|
-
constructor(handle: KeyObjectHandle) {
|
|
766
|
-
super('private', handle);
|
|
767
|
-
}
|
|
768
|
-
|
|
769
|
-
export(options: EncodingOptions) {
|
|
770
|
-
if (options?.format === 'jwk') {
|
|
771
|
-
if (options.passphrase !== undefined) {
|
|
772
|
-
throw new Error('jwk does not support encryption');
|
|
773
|
-
}
|
|
774
|
-
throw new Error('PrivateKey export for jwk is not implemented');
|
|
775
|
-
// return this.handle.exportJwk({}, false);
|
|
776
|
-
}
|
|
777
|
-
const { format, type, cipher, passphrase } = parsePrivateKeyEncoding(
|
|
778
|
-
options,
|
|
779
|
-
this.asymmetricKeyType
|
|
780
|
-
);
|
|
781
|
-
return this.handle.export(format, type, cipher, passphrase);
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
export const isCryptoKey = (obj: any): boolean => {
|
|
786
|
-
return obj !== null && obj?.keyObject !== undefined;
|
|
787
|
-
};
|