react-native-quick-crypto 1.0.0-beta.5 → 1.0.0-beta.7

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.
Files changed (107) hide show
  1. package/README.md +1 -1
  2. package/android/CMakeLists.txt +2 -4
  3. package/android/build.gradle +33 -4
  4. package/cpp/ed25519/HybridEdKeyPair.cpp +268 -0
  5. package/cpp/ed25519/HybridEdKeyPair.hpp +85 -0
  6. package/cpp/pbkdf2/HybridPbkdf2.cpp +6 -5
  7. package/cpp/pbkdf2/HybridPbkdf2.hpp +4 -4
  8. package/cpp/random/HybridRandom.cpp +6 -5
  9. package/cpp/random/HybridRandom.hpp +8 -4
  10. package/lib/commonjs/ed.js +41 -0
  11. package/lib/commonjs/ed.js.map +1 -0
  12. package/lib/commonjs/index.js +37 -5
  13. package/lib/commonjs/index.js.map +1 -1
  14. package/lib/commonjs/keys/classes.js +191 -0
  15. package/lib/commonjs/keys/classes.js.map +1 -0
  16. package/lib/commonjs/keys/generateKeyPair.js +148 -0
  17. package/lib/commonjs/keys/generateKeyPair.js.map +1 -0
  18. package/lib/commonjs/keys/index.js +48 -72
  19. package/lib/commonjs/keys/index.js.map +1 -1
  20. package/lib/commonjs/keys/signVerify.js +41 -0
  21. package/lib/commonjs/keys/signVerify.js.map +1 -0
  22. package/lib/commonjs/keys/utils.js +118 -0
  23. package/lib/commonjs/keys/utils.js.map +1 -0
  24. package/lib/commonjs/specs/edKeyPair.nitro.js +6 -0
  25. package/lib/commonjs/specs/edKeyPair.nitro.js.map +1 -0
  26. package/lib/commonjs/utils/conversion.js +2 -0
  27. package/lib/commonjs/utils/conversion.js.map +1 -1
  28. package/lib/commonjs/utils/index.js +11 -0
  29. package/lib/commonjs/utils/index.js.map +1 -1
  30. package/lib/commonjs/utils/types.js +12 -1
  31. package/lib/commonjs/utils/types.js.map +1 -1
  32. package/lib/commonjs/utils/validation.js +25 -0
  33. package/lib/commonjs/utils/validation.js.map +1 -0
  34. package/lib/module/ed.js +36 -0
  35. package/lib/module/ed.js.map +1 -0
  36. package/lib/module/index.js +7 -5
  37. package/lib/module/index.js.map +1 -1
  38. package/lib/module/keys/classes.js +182 -0
  39. package/lib/module/keys/classes.js.map +1 -0
  40. package/lib/module/keys/generateKeyPair.js +148 -0
  41. package/lib/module/keys/generateKeyPair.js.map +1 -0
  42. package/lib/module/keys/index.js +25 -77
  43. package/lib/module/keys/index.js.map +1 -1
  44. package/lib/module/keys/signVerify.js +41 -0
  45. package/lib/module/keys/signVerify.js.map +1 -0
  46. package/lib/module/keys/utils.js +110 -0
  47. package/lib/module/keys/utils.js.map +1 -0
  48. package/lib/module/specs/edKeyPair.nitro.js +4 -0
  49. package/lib/module/specs/edKeyPair.nitro.js.map +1 -0
  50. package/lib/module/utils/conversion.js +1 -0
  51. package/lib/module/utils/conversion.js.map +1 -1
  52. package/lib/module/utils/index.js +1 -0
  53. package/lib/module/utils/index.js.map +1 -1
  54. package/lib/module/utils/types.js +14 -0
  55. package/lib/module/utils/types.js.map +1 -1
  56. package/lib/module/utils/validation.js +19 -0
  57. package/lib/module/utils/validation.js.map +1 -0
  58. package/lib/tsconfig.tsbuildinfo +1 -1
  59. package/lib/typescript/ed.d.ts +17 -0
  60. package/lib/typescript/ed.d.ts.map +1 -0
  61. package/lib/typescript/index.d.ts +29 -5
  62. package/lib/typescript/index.d.ts.map +1 -1
  63. package/lib/typescript/keys/classes.d.ts +38 -0
  64. package/lib/typescript/keys/classes.d.ts.map +1 -0
  65. package/lib/typescript/keys/generateKeyPair.d.ts +1 -0
  66. package/lib/typescript/keys/generateKeyPair.d.ts.map +1 -0
  67. package/lib/typescript/keys/index.d.ts +3 -21
  68. package/lib/typescript/keys/index.d.ts.map +1 -1
  69. package/lib/typescript/keys/signVerify.d.ts +1 -0
  70. package/lib/typescript/keys/signVerify.d.ts.map +1 -0
  71. package/lib/typescript/keys/utils.d.ts +32 -0
  72. package/lib/typescript/keys/utils.d.ts.map +1 -0
  73. package/lib/typescript/specs/edKeyPair.nitro.d.ts +16 -0
  74. package/lib/typescript/specs/edKeyPair.nitro.d.ts.map +1 -0
  75. package/lib/typescript/utils/conversion.d.ts +1 -0
  76. package/lib/typescript/utils/conversion.d.ts.map +1 -1
  77. package/lib/typescript/utils/index.d.ts +1 -0
  78. package/lib/typescript/utils/index.d.ts.map +1 -1
  79. package/lib/typescript/utils/types.d.ts +60 -4
  80. package/lib/typescript/utils/types.d.ts.map +1 -1
  81. package/lib/typescript/utils/validation.d.ts +8 -0
  82. package/lib/typescript/utils/validation.d.ts.map +1 -0
  83. package/nitrogen/generated/android/QuickCrypto+autolinking.cmake +1 -0
  84. package/nitrogen/generated/android/QuickCrypto+autolinking.gradle +2 -0
  85. package/nitrogen/generated/android/QuickCryptoOnLoad.cpp +10 -0
  86. package/nitrogen/generated/ios/QuickCrypto+autolinking.rb +1 -1
  87. package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Umbrella.hpp +4 -1
  88. package/nitrogen/generated/ios/QuickCryptoAutolinking.mm +10 -0
  89. package/nitrogen/generated/shared/c++/{AsymmetricKeyType.hpp → CFRGKeyPairType.hpp} +25 -25
  90. package/nitrogen/generated/shared/c++/HybridEdKeyPairSpec.cpp +29 -0
  91. package/nitrogen/generated/shared/c++/HybridEdKeyPairSpec.hpp +74 -0
  92. package/nitrogen/generated/shared/c++/HybridKeyObjectHandleSpec.hpp +4 -4
  93. package/nitrogen/generated/shared/c++/HybridPbkdf2Spec.hpp +2 -2
  94. package/nitrogen/generated/shared/c++/HybridRandomSpec.hpp +2 -2
  95. package/package.json +37 -21
  96. package/src/ed.ts +76 -0
  97. package/src/index.ts +7 -5
  98. package/src/keys/classes.ts +211 -0
  99. package/src/keys/generateKeyPair.ts +146 -0
  100. package/src/keys/index.ts +42 -99
  101. package/src/keys/signVerify.ts +39 -0
  102. package/src/keys/utils.ts +184 -0
  103. package/src/specs/edKeyPair.nitro.ts +41 -0
  104. package/src/utils/conversion.ts +2 -0
  105. package/src/utils/index.ts +1 -0
  106. package/src/utils/types.ts +84 -4
  107. package/src/utils/validation.ts +35 -0
package/src/keys/index.ts CHANGED
@@ -1,99 +1,42 @@
1
- import type { KeyObjectHandle } from '../specs/keyObjectHandle.nitro';
2
- import type { EncodingOptions, KeyUsage, SubtleAlgorithm } from '../utils';
3
-
4
- export class CryptoKey {
5
- keyObject: KeyObject;
6
- keyAlgorithm: SubtleAlgorithm;
7
- keyUsages: KeyUsage[];
8
- keyExtractable: boolean;
9
-
10
- constructor(
11
- keyObject: KeyObject,
12
- keyAlgorithm: SubtleAlgorithm,
13
- keyUsages: KeyUsage[],
14
- keyExtractable: boolean,
15
- ) {
16
- this.keyObject = keyObject;
17
- this.keyAlgorithm = keyAlgorithm;
18
- this.keyUsages = keyUsages;
19
- this.keyExtractable = keyExtractable;
20
- }
21
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
22
- inspect(_depth: number, _options: unknown): unknown {
23
- throw new Error('CryptoKey.inspect is not implemented');
24
- // if (depth < 0) return this;
25
-
26
- // const opts = {
27
- // ...options,
28
- // depth: options.depth == null ? null : options.depth - 1,
29
- // };
30
-
31
- // return `CryptoKey ${inspect(
32
- // {
33
- // type: this.type,
34
- // extractable: this.extractable,
35
- // algorithm: this.algorithm,
36
- // usages: this.usages,
37
- // },
38
- // opts
39
- // )}`;
40
- }
41
-
42
- get type() {
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
+ };
@@ -0,0 +1,39 @@
1
+ // import { KeyObject, PublicKeyObject, PrivateKeyObject } from '.';
2
+ // import { ed25519 } from '../ed25519';
3
+ // import type {
4
+ // BinaryLike,
5
+ // BinaryLikeNode,
6
+ // SignCallback,
7
+ // VerifyCallback,
8
+ // } from '../utils';
9
+
10
+ // export function sign(
11
+ // algorithm: string | null | undefined,
12
+ // data: BinaryLike,
13
+ // key: BinaryLikeNode | KeyObject,
14
+ // callback: SignCallback,
15
+ // ): ArrayBuffer {
16
+ // console.log('sign ', algorithm, data, key, callback);
17
+ // return new ArrayBuffer(32);
18
+ // }
19
+
20
+ // export function verify(
21
+ // algorithm: string | null | undefined,
22
+ // data: BinaryLike,
23
+ // key: BinaryLikeNode | KeyObject,
24
+ // signature: ArrayBuffer,
25
+ // callback: VerifyCallback,
26
+ // ): boolean {
27
+ // if (!algorithm) {
28
+ // if (key instanceof PublicKeyObject) {
29
+ // switch (key.asymmetricKeyType) {
30
+ // case 'ed25519':
31
+ // case 'ed448':
32
+ // case 'x25519':
33
+ // case 'x448': {
34
+ // return ed25519.verify(signature, data, key);
35
+ // }
36
+ // }
37
+ // }
38
+ // throw new Error('Verify not implemented', algorithm, data, key, signature, callback);
39
+ // }
@@ -0,0 +1,184 @@
1
+ import {
2
+ binaryLikeToArrayBuffer,
3
+ isStringOrBuffer,
4
+ KeyEncoding,
5
+ KFormatType,
6
+ } from '../utils';
7
+ import type { EncodingOptions } from '../utils';
8
+
9
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
+ export const isCryptoKey = (obj: any): boolean => {
11
+ return obj !== null && obj?.keyObject !== undefined;
12
+ };
13
+
14
+ /**
15
+ * Parses the public key encoding based on an object. keyType must be undefined
16
+ * when this is used to parse an input encoding and must be a valid key type if
17
+ * used to parse an output encoding.
18
+ */
19
+ export function parsePublicKeyEncoding(
20
+ enc: EncodingOptions,
21
+ keyType: string | undefined,
22
+ objName?: string,
23
+ ) {
24
+ return parseKeyEncoding(enc, keyType, keyType ? true : undefined, objName);
25
+ }
26
+
27
+ /**
28
+ * Parses the private key encoding based on an object. keyType must be undefined
29
+ * when this is used to parse an input encoding and must be a valid key type if
30
+ * used to parse an output encoding.
31
+ */
32
+ export function parsePrivateKeyEncoding(
33
+ enc: EncodingOptions,
34
+ keyType: string | undefined,
35
+ objName?: string,
36
+ ) {
37
+ return parseKeyEncoding(enc, keyType, false, objName);
38
+ }
39
+
40
+ export function parseKeyEncoding(
41
+ enc: EncodingOptions,
42
+ keyType?: string,
43
+ isPublic?: boolean,
44
+ objName?: string,
45
+ ) {
46
+ // validateObject(enc, 'options');
47
+
48
+ const isInput = keyType === undefined;
49
+
50
+ const { format, type } = parseKeyFormatAndType(
51
+ enc,
52
+ keyType,
53
+ isPublic,
54
+ objName,
55
+ );
56
+
57
+ let cipher, passphrase, encoding;
58
+ if (isPublic !== true) {
59
+ ({ cipher, passphrase, encoding } = enc);
60
+
61
+ if (!isInput) {
62
+ if (cipher != null) {
63
+ if (typeof cipher !== 'string')
64
+ throw new Error(
65
+ `Invalid argument ${option('cipher', objName)}: ${cipher}`,
66
+ );
67
+ if (
68
+ format === KFormatType.kKeyFormatDER &&
69
+ (type === KeyEncoding.kKeyEncodingPKCS1 ||
70
+ type === KeyEncoding.kKeyEncodingSEC1)
71
+ ) {
72
+ throw new Error(
73
+ `Incompatible key options ${encodingNames[type]} does not support encryption`,
74
+ );
75
+ }
76
+ } else if (passphrase !== undefined) {
77
+ throw new Error(
78
+ `invalid argument ${option('cipher', objName)}: ${cipher}`,
79
+ );
80
+ }
81
+ }
82
+
83
+ if (
84
+ (isInput && passphrase !== undefined && !isStringOrBuffer(passphrase)) ||
85
+ (!isInput && cipher != null && !isStringOrBuffer(passphrase))
86
+ ) {
87
+ throw new Error(
88
+ `Invalid argument value ${option('passphrase', objName)}: ${passphrase}`,
89
+ );
90
+ }
91
+ }
92
+
93
+ if (passphrase !== undefined)
94
+ passphrase = binaryLikeToArrayBuffer(passphrase, encoding);
95
+
96
+ return { format, type, cipher, passphrase };
97
+ }
98
+
99
+ const encodingNames = {
100
+ [KeyEncoding.kKeyEncodingPKCS1]: 'pkcs1',
101
+ [KeyEncoding.kKeyEncodingPKCS8]: 'pkcs8',
102
+ [KeyEncoding.kKeyEncodingSPKI]: 'spki',
103
+ [KeyEncoding.kKeyEncodingSEC1]: 'sec1',
104
+ };
105
+
106
+ function option(name: string, objName?: string) {
107
+ return objName === undefined
108
+ ? `options.${name}`
109
+ : `options.${objName}.${name}`;
110
+ }
111
+
112
+ function parseKeyFormat(
113
+ formatStr?: string,
114
+ defaultFormat?: KFormatType,
115
+ optionName?: string,
116
+ ) {
117
+ if (formatStr === undefined && defaultFormat !== undefined)
118
+ return defaultFormat;
119
+ else if (formatStr === 'pem') return KFormatType.kKeyFormatPEM;
120
+ else if (formatStr === 'der') return KFormatType.kKeyFormatDER;
121
+ else if (formatStr === 'jwk') return KFormatType.kKeyFormatJWK;
122
+ throw new Error(`Invalid key format str: ${optionName}`);
123
+ }
124
+
125
+ function parseKeyType(
126
+ typeStr: string | undefined,
127
+ required: boolean,
128
+ keyType: string | undefined,
129
+ isPublic: boolean | undefined,
130
+ optionName: string,
131
+ ): KeyEncoding | undefined {
132
+ if (typeStr === undefined && !required) {
133
+ return undefined;
134
+ } else if (typeStr === 'pkcs1') {
135
+ if (keyType !== undefined && keyType !== 'rsa') {
136
+ throw new Error(
137
+ `Crypto incompatible key options: ${typeStr} can only be used for RSA keys`,
138
+ );
139
+ }
140
+ return KeyEncoding.kKeyEncodingPKCS1;
141
+ } else if (typeStr === 'spki' && isPublic !== false) {
142
+ return KeyEncoding.kKeyEncodingSPKI;
143
+ } else if (typeStr === 'pkcs8' && isPublic !== true) {
144
+ return KeyEncoding.kKeyEncodingPKCS8;
145
+ } else if (typeStr === 'sec1' && isPublic !== true) {
146
+ if (keyType !== undefined && keyType !== 'ec') {
147
+ throw new Error(
148
+ `Incompatible key options ${typeStr} can only be used for EC keys`,
149
+ );
150
+ }
151
+ return KeyEncoding.kKeyEncodingSEC1;
152
+ }
153
+
154
+ throw new Error(`Invalid option ${optionName} - ${typeStr}`);
155
+ }
156
+
157
+ function parseKeyFormatAndType(
158
+ enc: EncodingOptions,
159
+ keyType?: string,
160
+ isPublic?: boolean,
161
+ objName?: string,
162
+ ) {
163
+ const { format: formatStr, type: typeStr } = enc;
164
+
165
+ const isInput = keyType === undefined;
166
+ const format = parseKeyFormat(
167
+ formatStr,
168
+ isInput ? KFormatType.kKeyFormatPEM : undefined,
169
+ option('format', objName),
170
+ );
171
+
172
+ const isRequired =
173
+ (!isInput || format === KFormatType.kKeyFormatDER) &&
174
+ format !== KFormatType.kKeyFormatJWK;
175
+
176
+ const type = parseKeyType(
177
+ typeStr,
178
+ isRequired,
179
+ keyType,
180
+ isPublic,
181
+ option('type', objName),
182
+ );
183
+ return { format, type };
184
+ }
@@ -0,0 +1,41 @@
1
+ import type { HybridObject } from 'react-native-nitro-modules';
2
+
3
+ export interface EdKeyPair
4
+ extends HybridObject<{ ios: 'c++'; android: 'c++' }> {
5
+ generateKeyPair(
6
+ publicFormat: number,
7
+ publicType: number,
8
+ privateFormat: number,
9
+ privateType: number,
10
+ cipher?: string,
11
+ passphrase?: ArrayBuffer,
12
+ ): Promise<void>;
13
+
14
+ generateKeyPairSync(
15
+ publicFormat: number,
16
+ publicType: number,
17
+ privateFormat: number,
18
+ privateType: number,
19
+ cipher?: string,
20
+ passphrase?: ArrayBuffer,
21
+ ): void;
22
+
23
+ getPublicKey(): ArrayBuffer;
24
+ getPrivateKey(): ArrayBuffer;
25
+
26
+ sign(message: ArrayBuffer, key?: ArrayBuffer): Promise<ArrayBuffer>;
27
+ signSync(message: ArrayBuffer, key?: ArrayBuffer): ArrayBuffer;
28
+
29
+ verify(
30
+ message: ArrayBuffer,
31
+ signature: ArrayBuffer,
32
+ key?: ArrayBuffer,
33
+ ): Promise<boolean>;
34
+ verifySync(
35
+ message: ArrayBuffer,
36
+ signature: ArrayBuffer,
37
+ key?: ArrayBuffer,
38
+ ): boolean;
39
+
40
+ setCurve(curve: string): void;
41
+ }
@@ -96,3 +96,5 @@ export function binaryLikeToArrayBuffer(
96
96
  export function ab2str(buf: ArrayBuffer, encoding: string = 'hex') {
97
97
  return Buffer.from(buf).toString(encoding);
98
98
  }
99
+
100
+ export const kEmptyObject = Object.freeze(Object.create(null));
@@ -2,3 +2,4 @@ export * from './conversion';
2
2
  export * from './errors';
3
3
  export * from './hashnames';
4
4
  export * from './types';
5
+ export * from './validation';
@@ -1,6 +1,7 @@
1
- import { type Buffer } from '@craftzdog/react-native-buffer';
2
- import { type Buffer as SBuffer } from 'safe-buffer';
3
- import { type CipherKey } from 'crypto'; // @types/node
1
+ import type { Buffer } from '@craftzdog/react-native-buffer';
2
+ import type { Buffer as SBuffer } from 'safe-buffer';
3
+ import type { CipherKey } from 'crypto'; // @types/node
4
+ import type { KeyObjectHandle } from '../specs/keyObjectHandle.nitro';
4
5
 
5
6
  export type ArrayBufferView = TypedArray | DataView | ArrayBufferLike | Buffer;
6
7
 
@@ -38,6 +39,7 @@ export type RSAKeyPairAlgorithm = 'RSASSA-PKCS1-v1_5' | 'RSA-PSS' | 'RSA-OAEP';
38
39
  export type ECKeyPairAlgorithm = 'ECDSA' | 'ECDH';
39
40
 
40
41
  export type CFRGKeyPairAlgorithm = 'Ed25519' | 'Ed448' | 'X25519' | 'X448';
42
+ export type CFRGKeyPairType = 'ed25519' | 'ed448' | 'x25519' | 'x448';
41
43
 
42
44
  export type KeyPairAlgorithm =
43
45
  | RSAKeyPairAlgorithm
@@ -95,6 +97,8 @@ export type SubtleAlgorithm = {
95
97
  publicExponent?: number | Uint8Array;
96
98
  };
97
99
 
100
+ export type KeyPairType = CFRGKeyPairType;
101
+
98
102
  export type KeyUsage =
99
103
  | 'encrypt'
100
104
  | 'decrypt'
@@ -127,7 +131,22 @@ export enum KeyEncoding {
127
131
  kKeyEncodingSEC1,
128
132
  }
129
133
 
130
- export type AsymmetricKeyType = 'rsa' | 'rsa-pss' | 'dsa' | 'ec';
134
+ export type KeyPairGenConfig = {
135
+ publicFormat?: KFormatType;
136
+ publicType?: KeyEncoding;
137
+ privateFormat?: KFormatType;
138
+ privateType?: KeyEncoding;
139
+ cipher?: string;
140
+ passphrase?: ArrayBuffer;
141
+ };
142
+
143
+ export type AsymmetricKeyType =
144
+ // 'rsa' |
145
+ // 'rsa-pss' |
146
+ // 'dsa' |
147
+ // 'ec' |
148
+ // 'dh' |
149
+ CFRGKeyPairType;
131
150
 
132
151
  type JWKkty = 'AES' | 'RSA' | 'EC' | 'oct';
133
152
  type JWKuse = 'sig' | 'enc';
@@ -189,3 +208,64 @@ export interface KeyDetail {
189
208
  saltLength?: number;
190
209
  namedCurve?: string;
191
210
  }
211
+
212
+ export type GenerateKeyPairOptions = {
213
+ modulusLength?: number; // Key size in bits (RSA, DSA).
214
+ publicExponent?: number; // Public exponent (RSA). Default: 0x10001.
215
+ hashAlgorithm?: string; // Name of the message digest (RSA-PSS).
216
+ mgf1HashAlgorithm?: string; // string Name of the message digest used by MGF1 (RSA-PSS).
217
+ saltLength?: number; // Minimal salt length in bytes (RSA-PSS).
218
+ divisorLength?: number; // Size of q in bits (DSA).
219
+ namedCurve?: string; // Name of the curve to use (EC).
220
+ prime?: Buffer; // The prime parameter (DH).
221
+ primeLength?: number; // Prime length in bits (DH).
222
+ generator?: number; // Custom generator (DH). Default: 2.
223
+ groupName?: string; // Diffie-Hellman group name (DH). See crypto.getDiffieHellman().
224
+ publicKeyEncoding?: EncodingOptions; // See keyObject.export().
225
+ privateKeyEncoding?: EncodingOptions; // See keyObject.export().
226
+ paramEncoding?: string;
227
+ hash?: string;
228
+ mgf1Hash?: string;
229
+ };
230
+
231
+ // Note: removed CryptoKey class from this type (from 0.x) because Nitro doesn't
232
+ // handle custom JS objects. We might need to make it a JS object.
233
+ export type KeyPairKey = ArrayBuffer | KeyObjectHandle | undefined;
234
+
235
+ export type GenerateKeyPairReturn = [
236
+ error?: Error,
237
+ privateKey?: KeyPairKey,
238
+ publicKey?: KeyPairKey,
239
+ ];
240
+
241
+ export type GenerateKeyPairCallback = (
242
+ error?: Error,
243
+ publicKey?: KeyPairKey,
244
+ privateKey?: KeyPairKey,
245
+ ) => GenerateKeyPairReturn | void;
246
+
247
+ export type KeyPair = {
248
+ publicKey?: KeyPairKey;
249
+ privateKey?: KeyPairKey;
250
+ };
251
+
252
+ export type GenerateKeyPairPromiseReturn = [error?: Error, keypair?: KeyPair];
253
+
254
+ export type CryptoKeyPair = {
255
+ publicKey: KeyPairKey;
256
+ privateKey: KeyPairKey;
257
+ };
258
+
259
+ export enum KeyVariant {
260
+ RSA_SSA_PKCS1_v1_5,
261
+ RSA_PSS,
262
+ RSA_OAEP,
263
+ DSA,
264
+ EC,
265
+ NID,
266
+ DH,
267
+ }
268
+
269
+ export type SignCallback = (err: Error | null, signature?: ArrayBuffer) => void;
270
+
271
+ export type VerifyCallback = (err: Error | null, valid?: boolean) => void;
@@ -0,0 +1,35 @@
1
+ export function validateFunction(f: unknown): boolean {
2
+ return f !== null && typeof f === 'function';
3
+ }
4
+
5
+ export function isStringOrBuffer(val: unknown): val is string | ArrayBuffer {
6
+ return (
7
+ typeof val === 'string' ||
8
+ ArrayBuffer.isView(val) ||
9
+ val instanceof ArrayBuffer
10
+ );
11
+ }
12
+
13
+ export function validateObject<T>(
14
+ value: unknown,
15
+ name: string,
16
+ options?: {
17
+ allowArray: boolean;
18
+ allowFunction: boolean;
19
+ nullable: boolean;
20
+ } | null,
21
+ ): value is T {
22
+ const useDefaultOptions = options == null;
23
+ const allowArray = useDefaultOptions ? false : options.allowArray;
24
+ const allowFunction = useDefaultOptions ? false : options.allowFunction;
25
+ const nullable = useDefaultOptions ? false : options.nullable;
26
+ if (
27
+ (!nullable && value === null) ||
28
+ (!allowArray && Array.isArray(value)) ||
29
+ (typeof value !== 'object' &&
30
+ (!allowFunction || typeof value !== 'function'))
31
+ ) {
32
+ throw new Error(`${name} is not a valid object $${value}`);
33
+ }
34
+ return true;
35
+ }