react-native-quick-crypto 1.0.0-beta.4 → 1.0.0-beta.6
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 -4
- package/android/build.gradle +33 -4
- package/cpp/ed25519/HybridEdKeyPair.cpp +233 -0
- package/cpp/ed25519/HybridEdKeyPair.hpp +72 -0
- package/cpp/pbkdf2/HybridPbkdf2.cpp +6 -5
- package/cpp/pbkdf2/HybridPbkdf2.hpp +4 -4
- package/cpp/random/HybridRandom.cpp +6 -5
- package/cpp/random/HybridRandom.hpp +8 -4
- package/lib/commonjs/ed.js +38 -0
- package/lib/commonjs/ed.js.map +1 -0
- package/lib/commonjs/index.js +15 -5
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/keys/classes.js +191 -0
- package/lib/commonjs/keys/classes.js.map +1 -0
- package/lib/commonjs/keys/generateKeyPair.js +148 -0
- package/lib/commonjs/keys/generateKeyPair.js.map +1 -0
- package/lib/commonjs/keys/index.js +48 -72
- package/lib/commonjs/keys/index.js.map +1 -1
- package/lib/commonjs/keys/signVerify.js +41 -0
- package/lib/commonjs/keys/signVerify.js.map +1 -0
- package/lib/commonjs/keys/utils.js +118 -0
- package/lib/commonjs/keys/utils.js.map +1 -0
- package/lib/commonjs/specs/edKeyPair.nitro.js +6 -0
- package/lib/commonjs/specs/edKeyPair.nitro.js.map +1 -0
- package/lib/commonjs/utils/conversion.js +2 -0
- package/lib/commonjs/utils/conversion.js.map +1 -1
- package/lib/commonjs/utils/index.js +11 -0
- package/lib/commonjs/utils/index.js.map +1 -1
- package/lib/commonjs/utils/types.js +12 -1
- package/lib/commonjs/utils/types.js.map +1 -1
- package/lib/commonjs/utils/validation.js +25 -0
- package/lib/commonjs/utils/validation.js.map +1 -0
- package/lib/module/ed.js +33 -0
- package/lib/module/ed.js.map +1 -0
- package/lib/module/index.js +5 -5
- package/lib/module/index.js.map +1 -1
- package/lib/module/keys/classes.js +182 -0
- package/lib/module/keys/classes.js.map +1 -0
- package/lib/module/keys/generateKeyPair.js +148 -0
- package/lib/module/keys/generateKeyPair.js.map +1 -0
- package/lib/module/keys/index.js +25 -77
- package/lib/module/keys/index.js.map +1 -1
- package/lib/module/keys/signVerify.js +41 -0
- package/lib/module/keys/signVerify.js.map +1 -0
- package/lib/module/keys/utils.js +110 -0
- package/lib/module/keys/utils.js.map +1 -0
- package/lib/module/specs/edKeyPair.nitro.js +4 -0
- package/lib/module/specs/edKeyPair.nitro.js.map +1 -0
- package/lib/module/utils/conversion.js +1 -0
- package/lib/module/utils/conversion.js.map +1 -1
- package/lib/module/utils/index.js +1 -0
- package/lib/module/utils/index.js.map +1 -1
- package/lib/module/utils/types.js +14 -0
- package/lib/module/utils/types.js.map +1 -1
- package/lib/module/utils/validation.js +19 -0
- package/lib/module/utils/validation.js.map +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/typescript/ed.d.ts +16 -0
- package/lib/typescript/ed.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +27 -5
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/keys/classes.d.ts +38 -0
- package/lib/typescript/keys/classes.d.ts.map +1 -0
- package/lib/typescript/keys/generateKeyPair.d.ts +1 -0
- package/lib/typescript/keys/generateKeyPair.d.ts.map +1 -0
- package/lib/typescript/keys/index.d.ts +3 -21
- package/lib/typescript/keys/index.d.ts.map +1 -1
- package/lib/typescript/keys/signVerify.d.ts +1 -0
- package/lib/typescript/keys/signVerify.d.ts.map +1 -0
- package/lib/typescript/keys/utils.d.ts +32 -0
- package/lib/typescript/keys/utils.d.ts.map +1 -0
- package/lib/typescript/specs/edKeyPair.nitro.d.ts +15 -0
- package/lib/typescript/specs/edKeyPair.nitro.d.ts.map +1 -0
- package/lib/typescript/utils/conversion.d.ts +1 -0
- package/lib/typescript/utils/conversion.d.ts.map +1 -1
- package/lib/typescript/utils/index.d.ts +1 -0
- package/lib/typescript/utils/index.d.ts.map +1 -1
- package/lib/typescript/utils/types.d.ts +60 -4
- package/lib/typescript/utils/types.d.ts.map +1 -1
- package/lib/typescript/utils/validation.d.ts +8 -0
- package/lib/typescript/utils/validation.d.ts.map +1 -0
- package/nitrogen/generated/android/QuickCrypto+autolinking.cmake +1 -0
- package/nitrogen/generated/android/QuickCrypto+autolinking.gradle +2 -0
- package/nitrogen/generated/android/QuickCryptoOnLoad.cpp +10 -0
- package/nitrogen/generated/ios/QuickCrypto+autolinking.rb +1 -1
- package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Umbrella.hpp +4 -1
- package/nitrogen/generated/ios/QuickCryptoAutolinking.mm +10 -0
- package/nitrogen/generated/shared/c++/{AsymmetricKeyType.hpp → CFRGKeyPairType.hpp} +25 -25
- package/nitrogen/generated/shared/c++/HybridEdKeyPairSpec.cpp +28 -0
- package/nitrogen/generated/shared/c++/HybridEdKeyPairSpec.hpp +73 -0
- package/nitrogen/generated/shared/c++/HybridKeyObjectHandleSpec.hpp +4 -4
- package/nitrogen/generated/shared/c++/HybridPbkdf2Spec.hpp +2 -2
- package/nitrogen/generated/shared/c++/HybridRandomSpec.hpp +2 -2
- package/package.json +25 -21
- package/src/ed.ts +58 -0
- package/src/index.ts +5 -5
- package/src/keys/classes.ts +211 -0
- package/src/keys/generateKeyPair.ts +146 -0
- package/src/keys/index.ts +42 -99
- package/src/keys/signVerify.ts +39 -0
- package/src/keys/utils.ts +184 -0
- package/src/specs/edKeyPair.nitro.ts +32 -0
- package/src/utils/conversion.ts +2 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/types.ts +84 -4
- package/src/utils/validation.ts +35 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-quick-crypto",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.6",
|
|
4
4
|
"description": "A fast implementation of Node's `crypto` module written in C/C++ JSI",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/commonjs/index",
|
|
@@ -8,6 +8,19 @@
|
|
|
8
8
|
"types": "lib/typescript/index.d.ts",
|
|
9
9
|
"react-native": "src/index",
|
|
10
10
|
"source": "src/index",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"clean": "del-cli android/build lib",
|
|
13
|
+
"tsc": "tsc --noEmit",
|
|
14
|
+
"typescript": "tsc --noEmit",
|
|
15
|
+
"lint": "eslint \"**/*.{js,ts,tsx}\"",
|
|
16
|
+
"lint:fix": "eslint \"**/*.{js,ts,tsx}\" --fix",
|
|
17
|
+
"format": "prettier --check \"**/*.{js,ts,tsx}\"",
|
|
18
|
+
"format:fix": "prettier --write \"**/*.{js,ts,tsx}\"",
|
|
19
|
+
"prepare": "bun clean && bun tsc && bob build",
|
|
20
|
+
"release": "release-it",
|
|
21
|
+
"specs": "nitro-codegen",
|
|
22
|
+
"test": "jest"
|
|
23
|
+
},
|
|
11
24
|
"files": [
|
|
12
25
|
"src",
|
|
13
26
|
"lib",
|
|
@@ -27,19 +40,6 @@
|
|
|
27
40
|
"!**/__fixtures__",
|
|
28
41
|
"!**/__mocks__"
|
|
29
42
|
],
|
|
30
|
-
"scripts": {
|
|
31
|
-
"clean": "del-cli android/build lib",
|
|
32
|
-
"tsc": "tsc --noEmit",
|
|
33
|
-
"typescript": "tsc --noEmit",
|
|
34
|
-
"lint": "eslint \"**/*.{js,ts,tsx}\"",
|
|
35
|
-
"lint:fix": "eslint \"**/*.{js,ts,tsx}\" --fix",
|
|
36
|
-
"format": "prettier --check \"**/*.{js,ts,tsx}\"",
|
|
37
|
-
"format:fix": "prettier --write \"**/*.{js,ts,tsx}\"",
|
|
38
|
-
"prepare": "bun clean && bob build",
|
|
39
|
-
"release-it": "bun prepare && release-it",
|
|
40
|
-
"specs": "nitro-codegen",
|
|
41
|
-
"test": "jest"
|
|
42
|
-
},
|
|
43
43
|
"keywords": [
|
|
44
44
|
"react-native",
|
|
45
45
|
"ios",
|
|
@@ -56,7 +56,8 @@
|
|
|
56
56
|
},
|
|
57
57
|
"authors": [
|
|
58
58
|
"Szymon Kapała <szymon20000@gmail.com>",
|
|
59
|
-
"Marc Rousavy <me@mrousavy.com> (https://github.com/mrousavy)"
|
|
59
|
+
"Marc Rousavy <me@mrousavy.com> (https://github.com/mrousavy)",
|
|
60
|
+
"Brad Anderson <brad@sankatygroup.com> (https://github.com/boorad)"
|
|
60
61
|
],
|
|
61
62
|
"license": "MIT",
|
|
62
63
|
"bugs": {
|
|
@@ -69,7 +70,7 @@
|
|
|
69
70
|
"dependencies": {
|
|
70
71
|
"@craftzdog/react-native-buffer": "6.0.5",
|
|
71
72
|
"events": "3.3.0",
|
|
72
|
-
"react-native-nitro-modules": "0.
|
|
73
|
+
"react-native-nitro-modules": "0.18.1",
|
|
73
74
|
"react-native-quick-base64": "2.1.2",
|
|
74
75
|
"readable-stream": "4.5.2",
|
|
75
76
|
"string_decoder": "1.3.0",
|
|
@@ -77,19 +78,19 @@
|
|
|
77
78
|
},
|
|
78
79
|
"devDependencies": {
|
|
79
80
|
"@eslint/compat": "^1.1.1",
|
|
80
|
-
"@eslint/js": "
|
|
81
|
+
"@eslint/js": "9.16.0",
|
|
81
82
|
"@release-it/bumper": "6.0.1",
|
|
82
83
|
"@types/jest": "29.5.11",
|
|
83
84
|
"@types/node": "22.0.0",
|
|
84
85
|
"@types/react": "18.3.3",
|
|
85
86
|
"@types/readable-stream": "4.0.11",
|
|
86
|
-
"del-cli": "
|
|
87
|
+
"del-cli": "6.0.0",
|
|
87
88
|
"eslint": "9.9.0",
|
|
88
89
|
"eslint-plugin-react-native": "^4.1.0",
|
|
89
90
|
"jest": "29.7.0",
|
|
90
|
-
"nitro-codegen": "0.
|
|
91
|
+
"nitro-codegen": "0.18.1",
|
|
91
92
|
"prettier": "3.3.3",
|
|
92
|
-
"react-native-builder-bob": "0.
|
|
93
|
+
"react-native-builder-bob": "0.33.3",
|
|
93
94
|
"release-it": "17.6.0",
|
|
94
95
|
"typescript": "5.1.6",
|
|
95
96
|
"typescript-eslint": "^8.1.0"
|
|
@@ -106,7 +107,10 @@
|
|
|
106
107
|
"github": {
|
|
107
108
|
"release": false
|
|
108
109
|
},
|
|
109
|
-
"
|
|
110
|
+
"hooks": {
|
|
111
|
+
"before:init": "bun tsc && bun lint && bun format",
|
|
112
|
+
"after:bump": "bun prepare"
|
|
113
|
+
}
|
|
110
114
|
},
|
|
111
115
|
"react-native-builder-bob": {
|
|
112
116
|
"source": "src",
|
package/src/ed.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { NitroModules } from 'react-native-nitro-modules';
|
|
2
|
+
import type { EdKeyPair } from './specs/edKeyPair.nitro';
|
|
3
|
+
import type { CFRGKeyPairType, KeyPairGenConfig } from './utils';
|
|
4
|
+
|
|
5
|
+
export class Ed {
|
|
6
|
+
type: CFRGKeyPairType;
|
|
7
|
+
config: KeyPairGenConfig;
|
|
8
|
+
native: EdKeyPair;
|
|
9
|
+
|
|
10
|
+
constructor(type: CFRGKeyPairType, config: KeyPairGenConfig) {
|
|
11
|
+
this.type = type;
|
|
12
|
+
this.config = config;
|
|
13
|
+
this.native = NitroModules.createHybridObject<EdKeyPair>('EdKeyPair');
|
|
14
|
+
this.native.setCurve(type);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async generateKeyPair(): Promise<void> {
|
|
18
|
+
this.native.generateKeyPair(
|
|
19
|
+
this.config.publicFormat || (-1 as number),
|
|
20
|
+
this.config.publicType || (-1 as number),
|
|
21
|
+
this.config.privateFormat || (-1 as number),
|
|
22
|
+
this.config.privateType || (-1 as number),
|
|
23
|
+
this.config.cipher as string,
|
|
24
|
+
this.config.passphrase as ArrayBuffer,
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
generateKeyPairSync(): void {
|
|
29
|
+
this.native.generateKeyPairSync(
|
|
30
|
+
this.config.publicFormat || (-1 as number),
|
|
31
|
+
this.config.publicType || (-1 as number),
|
|
32
|
+
this.config.privateFormat || (-1 as number),
|
|
33
|
+
this.config.privateType || (-1 as number),
|
|
34
|
+
this.config.cipher as string,
|
|
35
|
+
this.config.passphrase as ArrayBuffer,
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
getPublicKey(): ArrayBuffer {
|
|
40
|
+
return this.native.getPublicKey();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async sign(message: ArrayBuffer): Promise<ArrayBuffer> {
|
|
44
|
+
return this.native.sign(message);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
signSync(message: ArrayBuffer): ArrayBuffer {
|
|
48
|
+
return this.native.signSync(message);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async verify(signature: ArrayBuffer, message: ArrayBuffer): Promise<boolean> {
|
|
52
|
+
return this.native.verify(signature, message);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
verifySync(signature: ArrayBuffer, message: ArrayBuffer): boolean {
|
|
56
|
+
return this.native.verifySync(signature, message);
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
3
3
|
|
|
4
4
|
// API imports
|
|
5
|
+
import * as keys from './keys';
|
|
6
|
+
import * as ed from './ed';
|
|
5
7
|
import * as pbkdf2 from './pbkdf2';
|
|
6
8
|
import * as random from './random';
|
|
7
9
|
|
|
@@ -21,20 +23,17 @@ const QuickCrypto = {
|
|
|
21
23
|
// createCipheriv,
|
|
22
24
|
// createDecipher,
|
|
23
25
|
// createDecipheriv,
|
|
24
|
-
// createPublicKey,
|
|
25
|
-
// createPrivateKey,
|
|
26
|
-
// createSecretKey,
|
|
27
26
|
// publicEncrypt,
|
|
28
27
|
// publicDecrypt,
|
|
29
28
|
// privateDecrypt,
|
|
30
29
|
// generateKey,
|
|
31
|
-
// generateKeyPair,
|
|
32
|
-
// generateKeyPairSync,
|
|
33
30
|
// generateKeySync,
|
|
34
31
|
// createSign,
|
|
35
32
|
// createVerify,
|
|
36
33
|
// subtle,
|
|
37
34
|
// constants,
|
|
35
|
+
...keys,
|
|
36
|
+
...ed,
|
|
38
37
|
...pbkdf2,
|
|
39
38
|
...random,
|
|
40
39
|
// getCiphers,
|
|
@@ -60,6 +59,7 @@ global.process.nextTick = setImmediate;
|
|
|
60
59
|
|
|
61
60
|
// exports
|
|
62
61
|
export default QuickCrypto;
|
|
62
|
+
export * from './ed';
|
|
63
63
|
export * from './utils';
|
|
64
64
|
|
|
65
65
|
// Additional exports for CommonJS compatibility
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import type { KeyObjectHandle } from '../specs/keyObjectHandle.nitro';
|
|
2
|
+
import type {
|
|
3
|
+
AsymmetricKeyType,
|
|
4
|
+
EncodingOptions,
|
|
5
|
+
KeyUsage,
|
|
6
|
+
SubtleAlgorithm,
|
|
7
|
+
} from '../utils';
|
|
8
|
+
import { parsePrivateKeyEncoding, parsePublicKeyEncoding } from './utils';
|
|
9
|
+
|
|
10
|
+
export class CryptoKey {
|
|
11
|
+
keyObject: KeyObject;
|
|
12
|
+
keyAlgorithm: SubtleAlgorithm;
|
|
13
|
+
keyUsages: KeyUsage[];
|
|
14
|
+
keyExtractable: boolean;
|
|
15
|
+
|
|
16
|
+
constructor(
|
|
17
|
+
keyObject: KeyObject,
|
|
18
|
+
keyAlgorithm: SubtleAlgorithm,
|
|
19
|
+
keyUsages: KeyUsage[],
|
|
20
|
+
keyExtractable: boolean,
|
|
21
|
+
) {
|
|
22
|
+
this.keyObject = keyObject;
|
|
23
|
+
this.keyAlgorithm = keyAlgorithm;
|
|
24
|
+
this.keyUsages = keyUsages;
|
|
25
|
+
this.keyExtractable = keyExtractable;
|
|
26
|
+
}
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
28
|
+
inspect(_depth: number, _options: unknown): unknown {
|
|
29
|
+
throw new Error('CryptoKey.inspect is not implemented');
|
|
30
|
+
// if (depth < 0) return this;
|
|
31
|
+
|
|
32
|
+
// const opts = {
|
|
33
|
+
// ...options,
|
|
34
|
+
// depth: options.depth == null ? null : options.depth - 1,
|
|
35
|
+
// };
|
|
36
|
+
|
|
37
|
+
// return `CryptoKey ${inspect(
|
|
38
|
+
// {
|
|
39
|
+
// type: this.type,
|
|
40
|
+
// extractable: this.extractable,
|
|
41
|
+
// algorithm: this.algorithm,
|
|
42
|
+
// usages: this.usages,
|
|
43
|
+
// },
|
|
44
|
+
// opts
|
|
45
|
+
// )}`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get type() {
|
|
49
|
+
// if (!(this instanceof CryptoKey)) throw new Error('Invalid CryptoKey');
|
|
50
|
+
return this.keyObject.type;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
get extractable() {
|
|
54
|
+
return this.keyExtractable;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
get algorithm() {
|
|
58
|
+
return this.keyAlgorithm;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
get usages() {
|
|
62
|
+
return this.keyUsages;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export class KeyObject {
|
|
67
|
+
handle: KeyObjectHandle;
|
|
68
|
+
type: 'public' | 'secret' | 'private' | 'unknown' = 'unknown';
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
70
|
+
export(_options?: EncodingOptions): ArrayBuffer {
|
|
71
|
+
return new ArrayBuffer(0);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
constructor(type: string, handle: KeyObjectHandle) {
|
|
75
|
+
if (type !== 'secret' && type !== 'public' && type !== 'private')
|
|
76
|
+
throw new Error(`invalid KeyObject type: ${type}`);
|
|
77
|
+
this.handle = handle;
|
|
78
|
+
this.type = type;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// get type(): string {
|
|
82
|
+
// return this.type;
|
|
83
|
+
// }
|
|
84
|
+
|
|
85
|
+
// static from(key) {
|
|
86
|
+
// if (!isCryptoKey(key))
|
|
87
|
+
// throw new ERR_INVALID_ARG_TYPE('key', 'CryptoKey', key);
|
|
88
|
+
// return key[kKeyObject];
|
|
89
|
+
// }
|
|
90
|
+
|
|
91
|
+
// equals(otherKeyObject) {
|
|
92
|
+
// if (!isKeyObject(otherKeyObject)) {
|
|
93
|
+
// throw new ERR_INVALID_ARG_TYPE(
|
|
94
|
+
// 'otherKeyObject',
|
|
95
|
+
// 'KeyObject',
|
|
96
|
+
// otherKeyObject
|
|
97
|
+
// );
|
|
98
|
+
// }
|
|
99
|
+
|
|
100
|
+
// return (
|
|
101
|
+
// otherKeyObject.type === this.type &&
|
|
102
|
+
// this[kHandle].equals(otherKeyObject[kHandle])
|
|
103
|
+
// );
|
|
104
|
+
// }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export class SecretKeyObject extends KeyObject {
|
|
108
|
+
constructor(handle: KeyObjectHandle) {
|
|
109
|
+
super('secret', handle);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// get symmetricKeySize() {
|
|
113
|
+
// return this[kHandle].getSymmetricKeySize();
|
|
114
|
+
// }
|
|
115
|
+
|
|
116
|
+
export(options?: EncodingOptions) {
|
|
117
|
+
if (options !== undefined) {
|
|
118
|
+
if (options.format === 'jwk') {
|
|
119
|
+
throw new Error('SecretKey export for jwk is not implemented');
|
|
120
|
+
// return this.handle.exportJwk({}, false);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return this.handle.exportKey();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// const kAsymmetricKeyType = Symbol('kAsymmetricKeyType');
|
|
128
|
+
// const kAsymmetricKeyDetails = Symbol('kAsymmetricKeyDetails');
|
|
129
|
+
|
|
130
|
+
// function normalizeKeyDetails(details = {}) {
|
|
131
|
+
// if (details.publicExponent !== undefined) {
|
|
132
|
+
// return {
|
|
133
|
+
// ...details,
|
|
134
|
+
// publicExponent: bigIntArrayToUnsignedBigInt(
|
|
135
|
+
// new Uint8Array(details.publicExponent)
|
|
136
|
+
// ),
|
|
137
|
+
// };
|
|
138
|
+
// }
|
|
139
|
+
// return details;
|
|
140
|
+
// }
|
|
141
|
+
|
|
142
|
+
export class AsymmetricKeyObject extends KeyObject {
|
|
143
|
+
constructor(type: string, handle: KeyObjectHandle) {
|
|
144
|
+
super(type, handle);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private _asymmetricKeyType?: AsymmetricKeyType;
|
|
148
|
+
|
|
149
|
+
get asymmetricKeyType(): AsymmetricKeyType {
|
|
150
|
+
if (!this._asymmetricKeyType) {
|
|
151
|
+
this._asymmetricKeyType = this.handle.getAsymmetricKeyType();
|
|
152
|
+
}
|
|
153
|
+
return this._asymmetricKeyType;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// get asymmetricKeyDetails() {
|
|
157
|
+
// switch (this._asymmetricKeyType) {
|
|
158
|
+
// case 'rsa':
|
|
159
|
+
// case 'rsa-pss':
|
|
160
|
+
// case 'dsa':
|
|
161
|
+
// case 'ec':
|
|
162
|
+
// return (
|
|
163
|
+
// this[kAsymmetricKeyDetails] ||
|
|
164
|
+
// (this[kAsymmetricKeyDetails] = normalizeKeyDetails(
|
|
165
|
+
// this[kHandle].keyDetail({})
|
|
166
|
+
// ))
|
|
167
|
+
// );
|
|
168
|
+
// default:
|
|
169
|
+
// return {};
|
|
170
|
+
// }
|
|
171
|
+
// }
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export class PublicKeyObject extends AsymmetricKeyObject {
|
|
175
|
+
constructor(handle: KeyObjectHandle) {
|
|
176
|
+
super('public', handle);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export(options: EncodingOptions) {
|
|
180
|
+
if (options?.format === 'jwk') {
|
|
181
|
+
throw new Error('PublicKey export for jwk is not implemented');
|
|
182
|
+
// return this.handle.exportJwk({}, false);
|
|
183
|
+
}
|
|
184
|
+
const { format, type } = parsePublicKeyEncoding(
|
|
185
|
+
options,
|
|
186
|
+
this.asymmetricKeyType,
|
|
187
|
+
);
|
|
188
|
+
return this.handle.exportKey(format, type);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export class PrivateKeyObject extends AsymmetricKeyObject {
|
|
193
|
+
constructor(handle: KeyObjectHandle) {
|
|
194
|
+
super('private', handle);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export(options: EncodingOptions) {
|
|
198
|
+
if (options?.format === 'jwk') {
|
|
199
|
+
if (options.passphrase !== undefined) {
|
|
200
|
+
throw new Error('jwk does not support encryption');
|
|
201
|
+
}
|
|
202
|
+
throw new Error('PrivateKey export for jwk is not implemented');
|
|
203
|
+
// return this.handle.exportJwk({}, false);
|
|
204
|
+
}
|
|
205
|
+
const { format, type, cipher, passphrase } = parsePrivateKeyEncoding(
|
|
206
|
+
options,
|
|
207
|
+
this.asymmetricKeyType,
|
|
208
|
+
);
|
|
209
|
+
return this.handle.exportKey(format, type, cipher, passphrase);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// import { ed25519 } from '../ed25519';
|
|
2
|
+
// import {
|
|
3
|
+
// kEmptyObject,
|
|
4
|
+
// validateFunction,
|
|
5
|
+
// type CryptoKeyPair,
|
|
6
|
+
// type GenerateKeyPairCallback,
|
|
7
|
+
// type GenerateKeyPairOptions,
|
|
8
|
+
// type GenerateKeyPairPromiseReturn,
|
|
9
|
+
// type GenerateKeyPairReturn,
|
|
10
|
+
// type KeyPairGenConfig,
|
|
11
|
+
// type KeyPairType,
|
|
12
|
+
// } from '../utils';
|
|
13
|
+
// import { parsePrivateKeyEncoding, parsePublicKeyEncoding } from './utils';
|
|
14
|
+
|
|
15
|
+
// export const generateKeyPair = (
|
|
16
|
+
// type: KeyPairType,
|
|
17
|
+
// options: GenerateKeyPairOptions,
|
|
18
|
+
// callback: GenerateKeyPairCallback,
|
|
19
|
+
// ): void => {
|
|
20
|
+
// validateFunction(callback);
|
|
21
|
+
// internalGenerateKeyPair(true, type, options, callback);
|
|
22
|
+
// };
|
|
23
|
+
|
|
24
|
+
// // Promisify generateKeyPair
|
|
25
|
+
// // (attempted to use util.promisify, to no avail)
|
|
26
|
+
// export const generateKeyPairPromise = (
|
|
27
|
+
// type: KeyPairType,
|
|
28
|
+
// options: GenerateKeyPairOptions,
|
|
29
|
+
// ): Promise<GenerateKeyPairPromiseReturn> => {
|
|
30
|
+
// return new Promise((resolve, reject) => {
|
|
31
|
+
// generateKeyPair(type, options, (err, publicKey, privateKey) => {
|
|
32
|
+
// if (err) {
|
|
33
|
+
// reject([err, undefined]);
|
|
34
|
+
// } else {
|
|
35
|
+
// resolve([undefined, { publicKey, privateKey }]);
|
|
36
|
+
// }
|
|
37
|
+
// });
|
|
38
|
+
// });
|
|
39
|
+
// };
|
|
40
|
+
|
|
41
|
+
// // generateKeyPairSync
|
|
42
|
+
// export function generateKeyPairSync(type: KeyPairType): CryptoKeyPair;
|
|
43
|
+
// export function generateKeyPairSync(
|
|
44
|
+
// type: KeyPairType,
|
|
45
|
+
// options: GenerateKeyPairOptions,
|
|
46
|
+
// ): CryptoKeyPair;
|
|
47
|
+
// export function generateKeyPairSync(
|
|
48
|
+
// type: KeyPairType,
|
|
49
|
+
// options?: GenerateKeyPairOptions,
|
|
50
|
+
// ): CryptoKeyPair {
|
|
51
|
+
// const [err, publicKey, privateKey] = internalGenerateKeyPair(
|
|
52
|
+
// false,
|
|
53
|
+
// type,
|
|
54
|
+
// options,
|
|
55
|
+
// undefined,
|
|
56
|
+
// )!;
|
|
57
|
+
|
|
58
|
+
// if (err) {
|
|
59
|
+
// throw err;
|
|
60
|
+
// }
|
|
61
|
+
|
|
62
|
+
// return {
|
|
63
|
+
// publicKey,
|
|
64
|
+
// privateKey,
|
|
65
|
+
// };
|
|
66
|
+
// }
|
|
67
|
+
|
|
68
|
+
// function parseKeyPairEncoding(
|
|
69
|
+
// keyType: string,
|
|
70
|
+
// options: GenerateKeyPairOptions = kEmptyObject,
|
|
71
|
+
// ): KeyPairGenConfig {
|
|
72
|
+
// const { publicKeyEncoding, privateKeyEncoding } = options;
|
|
73
|
+
|
|
74
|
+
// let publicFormat, publicType;
|
|
75
|
+
// if (publicKeyEncoding == null) {
|
|
76
|
+
// publicFormat = publicType = -1;
|
|
77
|
+
// } else if (typeof publicKeyEncoding === 'object') {
|
|
78
|
+
// ({ format: publicFormat, type: publicType } = parsePublicKeyEncoding(
|
|
79
|
+
// publicKeyEncoding,
|
|
80
|
+
// keyType,
|
|
81
|
+
// 'publicKeyEncoding',
|
|
82
|
+
// ));
|
|
83
|
+
// } else {
|
|
84
|
+
// throw new Error(
|
|
85
|
+
// 'Invalid argument options.publicKeyEncoding',
|
|
86
|
+
// publicKeyEncoding,
|
|
87
|
+
// );
|
|
88
|
+
// }
|
|
89
|
+
|
|
90
|
+
// let privateFormat, privateType, cipher, passphrase;
|
|
91
|
+
// if (privateKeyEncoding == null) {
|
|
92
|
+
// privateFormat = privateType = -1;
|
|
93
|
+
// } else if (typeof privateKeyEncoding === 'object') {
|
|
94
|
+
// ({
|
|
95
|
+
// format: privateFormat,
|
|
96
|
+
// type: privateType,
|
|
97
|
+
// cipher,
|
|
98
|
+
// passphrase,
|
|
99
|
+
// } = parsePrivateKeyEncoding(
|
|
100
|
+
// privateKeyEncoding,
|
|
101
|
+
// keyType,
|
|
102
|
+
// 'privateKeyEncoding',
|
|
103
|
+
// ));
|
|
104
|
+
// } else {
|
|
105
|
+
// throw new Error(
|
|
106
|
+
// 'Invalid argument options.privateKeyEncoding',
|
|
107
|
+
// publicKeyEncoding as ErrorOptions,
|
|
108
|
+
// );
|
|
109
|
+
// }
|
|
110
|
+
|
|
111
|
+
// return {
|
|
112
|
+
// publicFormat,
|
|
113
|
+
// publicType,
|
|
114
|
+
// privateFormat,
|
|
115
|
+
// privateType,
|
|
116
|
+
// cipher,
|
|
117
|
+
// passphrase,
|
|
118
|
+
// };
|
|
119
|
+
// }
|
|
120
|
+
|
|
121
|
+
// function internalGenerateKeyPair(
|
|
122
|
+
// isAsync: boolean,
|
|
123
|
+
// type: KeyPairType,
|
|
124
|
+
// options: GenerateKeyPairOptions | undefined,
|
|
125
|
+
// callback: GenerateKeyPairCallback | undefined,
|
|
126
|
+
// ): GenerateKeyPairReturn | void {
|
|
127
|
+
// const encoding = parseKeyPairEncoding(type, options);
|
|
128
|
+
|
|
129
|
+
// switch (type) {
|
|
130
|
+
// case 'ed25519':
|
|
131
|
+
// case 'ed448':
|
|
132
|
+
// case 'x25519':
|
|
133
|
+
// case 'x448': {
|
|
134
|
+
// return ed25519.utils.generateKeyPair(isAsync, type, encoding, callback);
|
|
135
|
+
// }
|
|
136
|
+
// default:
|
|
137
|
+
// // Fall through
|
|
138
|
+
// }
|
|
139
|
+
|
|
140
|
+
// const err = new Error(`
|
|
141
|
+
// Invalid Argument options: '${type}' scheme not supported for
|
|
142
|
+
// generateKeyPair(). Currently not all encryption methods are supported in
|
|
143
|
+
// this library. Check docs/implementation_coverage.md for status.
|
|
144
|
+
// `);
|
|
145
|
+
// return [err, undefined, undefined];
|
|
146
|
+
// }
|
package/src/keys/index.ts
CHANGED
|
@@ -1,99 +1,42 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
//
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
// if (!(this instanceof CryptoKey)) throw new Error('Invalid CryptoKey');
|
|
44
|
-
return this.keyObject.type;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
get extractable() {
|
|
48
|
-
return this.keyExtractable;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
get algorithm() {
|
|
52
|
-
return this.keyAlgorithm;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
get usages() {
|
|
56
|
-
return this.keyUsages;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
class KeyObject {
|
|
61
|
-
handle: KeyObjectHandle;
|
|
62
|
-
type: 'public' | 'secret' | 'private' | 'unknown' = 'unknown';
|
|
63
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
64
|
-
export(_options?: EncodingOptions): ArrayBuffer {
|
|
65
|
-
return new ArrayBuffer(0);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
constructor(type: string, handle: KeyObjectHandle) {
|
|
69
|
-
if (type !== 'secret' && type !== 'public' && type !== 'private')
|
|
70
|
-
throw new Error(`invalid KeyObject type: ${type}`);
|
|
71
|
-
this.handle = handle;
|
|
72
|
-
this.type = type;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// get type(): string {
|
|
76
|
-
// return this.type;
|
|
77
|
-
// }
|
|
78
|
-
|
|
79
|
-
// static from(key) {
|
|
80
|
-
// if (!isCryptoKey(key))
|
|
81
|
-
// throw new ERR_INVALID_ARG_TYPE('key', 'CryptoKey', key);
|
|
82
|
-
// return key[kKeyObject];
|
|
83
|
-
// }
|
|
84
|
-
|
|
85
|
-
// equals(otherKeyObject) {
|
|
86
|
-
// if (!isKeyObject(otherKeyObject)) {
|
|
87
|
-
// throw new ERR_INVALID_ARG_TYPE(
|
|
88
|
-
// 'otherKeyObject',
|
|
89
|
-
// 'KeyObject',
|
|
90
|
-
// otherKeyObject
|
|
91
|
-
// );
|
|
92
|
-
// }
|
|
93
|
-
|
|
94
|
-
// return (
|
|
95
|
-
// otherKeyObject.type === this.type &&
|
|
96
|
-
// this[kHandle].equals(otherKeyObject[kHandle])
|
|
97
|
-
// );
|
|
98
|
-
// }
|
|
99
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
CryptoKey,
|
|
3
|
+
KeyObject,
|
|
4
|
+
SecretKeyObject,
|
|
5
|
+
PublicKeyObject,
|
|
6
|
+
PrivateKeyObject,
|
|
7
|
+
} from './classes';
|
|
8
|
+
// import { generateKeyPair } from './generateKeyPair';
|
|
9
|
+
// import { sign, verify } from './signVerify';
|
|
10
|
+
import {
|
|
11
|
+
isCryptoKey,
|
|
12
|
+
parseKeyEncoding,
|
|
13
|
+
parsePrivateKeyEncoding,
|
|
14
|
+
parsePublicKeyEncoding,
|
|
15
|
+
} from './utils';
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
// Node Public API
|
|
19
|
+
// createSecretKey,
|
|
20
|
+
// createPublicKey,
|
|
21
|
+
// createPrivateKey,
|
|
22
|
+
CryptoKey,
|
|
23
|
+
// generateKeyPair,
|
|
24
|
+
KeyObject,
|
|
25
|
+
// InternalCryptoKey,
|
|
26
|
+
// sign,
|
|
27
|
+
// verify,
|
|
28
|
+
|
|
29
|
+
// Node Internal API
|
|
30
|
+
parsePublicKeyEncoding,
|
|
31
|
+
parsePrivateKeyEncoding,
|
|
32
|
+
parseKeyEncoding,
|
|
33
|
+
// preparePrivateKey,
|
|
34
|
+
// preparePublicOrPrivateKey,
|
|
35
|
+
// prepareSecretKey,
|
|
36
|
+
SecretKeyObject,
|
|
37
|
+
PublicKeyObject,
|
|
38
|
+
PrivateKeyObject,
|
|
39
|
+
// isKeyObject,
|
|
40
|
+
isCryptoKey,
|
|
41
|
+
// importGenericSecretKey,
|
|
42
|
+
};
|