simple-crypto-utils 1.0.1 → 1.0.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/dist/index.d.cts +781 -0
- package/dist/index.d.ts +781 -0
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { KeyObject } from 'crypto';
|
|
2
2
|
|
|
3
3
|
interface PasswordOptions {
|
|
4
|
+
/** Length of the password (default: 16) */
|
|
4
5
|
length?: number;
|
|
6
|
+
/** Include letters in the password (default: true) */
|
|
5
7
|
letters?: boolean;
|
|
8
|
+
/** Include numbers in the password (default: true) */
|
|
6
9
|
numbers?: boolean;
|
|
10
|
+
/** Include symbols in the password (default: true) */
|
|
7
11
|
symbols?: boolean;
|
|
8
12
|
}
|
|
9
13
|
declare function generatePassword(length?: number): string;
|
|
@@ -12,8 +16,66 @@ declare function generatePassword(options: PasswordOptions & {
|
|
|
12
16
|
hash: true;
|
|
13
17
|
}): Promise<string>;
|
|
14
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Generates a secure scrypt hash of a password.
|
|
21
|
+
*
|
|
22
|
+
* The hash is salted with 16 random bytes and the result is encoded in Base64.
|
|
23
|
+
* The output format is:
|
|
24
|
+
* ```
|
|
25
|
+
* scrypt$16$<saltBase64>$<hashBase64>
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @param password - The password to hash.
|
|
29
|
+
* @returns A Promise resolving to the hashed password string.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { hashPassword } from "./password";
|
|
34
|
+
*
|
|
35
|
+
* async function main() {
|
|
36
|
+
* const hashed = await hashPassword("mySecretPassword");
|
|
37
|
+
* console.log(hashed);
|
|
38
|
+
* // Example output:
|
|
39
|
+
* // scrypt$16$3q2+7w==$pX9n0V5gK2v7r6Y3h8Zs2I3cL0y7hGqL8v9pN7l0K5Q=
|
|
40
|
+
* }
|
|
41
|
+
*
|
|
42
|
+
* main();
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
15
45
|
declare function hashPassword(password: string): Promise<string>;
|
|
16
46
|
|
|
47
|
+
/**
|
|
48
|
+
* Validates a password against a stored scrypt hash.
|
|
49
|
+
*
|
|
50
|
+
* The stored hash must be in the format:
|
|
51
|
+
* ```
|
|
52
|
+
* scrypt$<saltLength>$<saltBase64>$<hashBase64>
|
|
53
|
+
* ```
|
|
54
|
+
* The function uses a constant-time comparison to prevent timing attacks.
|
|
55
|
+
*
|
|
56
|
+
* @param password - The password to verify.
|
|
57
|
+
* @param storedHash - The stored hash string to validate against.
|
|
58
|
+
* @returns `true` if the password matches the hash, `false` otherwise.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* import { hash } from "./password";
|
|
63
|
+
* import { verifyPassword } from "./validate";
|
|
64
|
+
*
|
|
65
|
+
* async function main() {
|
|
66
|
+
* const password = "mySecretPassword";
|
|
67
|
+
* const hashed = await hash(password);
|
|
68
|
+
*
|
|
69
|
+
* const isValid = await verifyPassword("mySecretPassword", hashed);
|
|
70
|
+
* console.log(isValid); // true
|
|
71
|
+
*
|
|
72
|
+
* const isInvalid = await verifyPassword("wrongPassword", hashed);
|
|
73
|
+
* console.log(isInvalid); // false
|
|
74
|
+
* }
|
|
75
|
+
*
|
|
76
|
+
* main();
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
17
79
|
declare function verifyPassword(password: string, storedHash: string): Promise<boolean>;
|
|
18
80
|
|
|
19
81
|
declare const index$5_generatePassword: typeof generatePassword;
|
|
@@ -23,6 +85,19 @@ declare namespace index$5 {
|
|
|
23
85
|
export { index$5_generatePassword as generatePassword, index$5_hashPassword as hashPassword, index$5_verifyPassword as verifyPassword };
|
|
24
86
|
}
|
|
25
87
|
|
|
88
|
+
/**
|
|
89
|
+
* Generates a cryptographically secure UUID (version 4).
|
|
90
|
+
*
|
|
91
|
+
* @returns A string representing the UUID (e.g., "3b241101-e2bb-4255-8caf-4136c566a962").
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* import { generateUUID } from "./uuid";
|
|
96
|
+
*
|
|
97
|
+
* const id = generateUUID();
|
|
98
|
+
* console.log(id); // e.g., "3b241101-e2bb-4255-8caf-4136c566a962"
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
26
101
|
declare function generateUUID(): string;
|
|
27
102
|
|
|
28
103
|
declare const index$4_generateUUID: typeof generateUUID;
|
|
@@ -32,10 +107,15 @@ declare namespace index$4 {
|
|
|
32
107
|
|
|
33
108
|
type SerializationStrategy = "canonical" | "raw" | "selective";
|
|
34
109
|
interface SignOptions {
|
|
110
|
+
/** Serialization strategy for complex objects */
|
|
35
111
|
strategy?: SerializationStrategy;
|
|
112
|
+
/** Fields to sign when using 'selective' strategy */
|
|
36
113
|
fields?: string[];
|
|
114
|
+
/** Hash algorithm (default: 'SHA256') */
|
|
37
115
|
algorithm?: "SHA256" | "SHA384" | "SHA512";
|
|
116
|
+
/** Output encoding (default: 'base64') */
|
|
38
117
|
encoding?: "base64" | "hex";
|
|
118
|
+
/** Pre-hash large data before signing (recommended for >1MB) */
|
|
39
119
|
preHash?: boolean;
|
|
40
120
|
}
|
|
41
121
|
interface VerifyOptions extends SignOptions {
|
|
@@ -53,15 +133,83 @@ declare function verify(data: any, signature: string, publicKey: string, options
|
|
|
53
133
|
encoding?: "base64" | "hex";
|
|
54
134
|
}): boolean;
|
|
55
135
|
|
|
136
|
+
/**
|
|
137
|
+
* Class for signing and verifying data with digital signatures.
|
|
138
|
+
*
|
|
139
|
+
* Supports:
|
|
140
|
+
* - Signing data
|
|
141
|
+
* - Verifying signatures
|
|
142
|
+
* - Creating signed envelopes (data + signature)
|
|
143
|
+
* - Opening envelopes and verifying validity
|
|
144
|
+
*/
|
|
56
145
|
declare class Signer {
|
|
57
146
|
private defaultOptions;
|
|
147
|
+
/**
|
|
148
|
+
* Creates a new Signer instance with optional default options.
|
|
149
|
+
*
|
|
150
|
+
* @param defaultOptions - Partial default options to override
|
|
151
|
+
* serialization strategy, fields, algorithm, encoding, or preHash.
|
|
152
|
+
*/
|
|
58
153
|
constructor(defaultOptions?: Partial<SignOptions>);
|
|
154
|
+
/**
|
|
155
|
+
* Sign data with a private key (static method).
|
|
156
|
+
*
|
|
157
|
+
* @param data - The data to sign.
|
|
158
|
+
* @param privateKey - The private key to use for signing.
|
|
159
|
+
* @param options - Optional signing options.
|
|
160
|
+
* @returns The digital signature as a string.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* const signature = Signer.sign({ message: "Hello" }, privateKey);
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
59
167
|
static sign(data: any, privateKey: string, options?: SignOptions): string;
|
|
168
|
+
/**
|
|
169
|
+
* Verify a signature with a public key (static method).
|
|
170
|
+
*
|
|
171
|
+
* @param data - The original data.
|
|
172
|
+
* @param signature - The signature to verify.
|
|
173
|
+
* @param publicKey - The public key corresponding to the signer.
|
|
174
|
+
* @param options - Optional verification options.
|
|
175
|
+
* @returns `true` if the signature is valid, `false` otherwise.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* const isValid = Signer.verify({ message: "Hello" }, signature, publicKey);
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
60
182
|
static verify(data: any, signature: string, publicKey: string, options?: VerifyOptions): boolean;
|
|
183
|
+
/**
|
|
184
|
+
* Create a signed envelope containing the data and its signature (static method).
|
|
185
|
+
*
|
|
186
|
+
* @param data - The data to include in the envelope.
|
|
187
|
+
* @param privateKey - The private key for signing.
|
|
188
|
+
* @param options - Optional signing options.
|
|
189
|
+
* @returns An object containing `{ data, signature }`.
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```ts
|
|
193
|
+
* const envelope = Signer.envelope({ message: "Hello" }, privateKey);
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
61
196
|
static envelope(data: any, privateKey: string, options?: SignOptions): {
|
|
62
197
|
data: any;
|
|
63
198
|
signature: string;
|
|
64
199
|
};
|
|
200
|
+
/**
|
|
201
|
+
* Verify and extract data from a signed envelope (static method).
|
|
202
|
+
*
|
|
203
|
+
* @param envelope - The envelope object `{ data, signature }`.
|
|
204
|
+
* @param publicKey - The public key to verify the signature.
|
|
205
|
+
* @param options - Optional verification options.
|
|
206
|
+
* @returns An object `{ valid, data }` indicating whether the signature is valid.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```ts
|
|
210
|
+
* const result = Signer.openEnvelope(envelope, publicKey);
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
65
213
|
static openEnvelope(envelope: {
|
|
66
214
|
data: any;
|
|
67
215
|
signature: string;
|
|
@@ -69,12 +217,66 @@ declare class Signer {
|
|
|
69
217
|
valid: boolean;
|
|
70
218
|
data: any;
|
|
71
219
|
};
|
|
220
|
+
/**
|
|
221
|
+
* Sign data with a private key (instance method).
|
|
222
|
+
*
|
|
223
|
+
* @param data - The data to sign.
|
|
224
|
+
* @param privateKey - The private key to use for signing.
|
|
225
|
+
* @param options - Optional signing options.
|
|
226
|
+
* @returns The digital signature as a string.
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* ```ts
|
|
230
|
+
* const signer = new Signer();
|
|
231
|
+
* const signature = signer.sign({ message: "Hello" }, privateKey);
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
72
234
|
sign(data: any, privateKey: string, options?: SignOptions): string;
|
|
235
|
+
/**
|
|
236
|
+
* Verify a signature with a public key (instance method).
|
|
237
|
+
*
|
|
238
|
+
* @param data - The original data.
|
|
239
|
+
* @param signature - The signature to verify.
|
|
240
|
+
* @param publicKey - The public key corresponding to the signer.
|
|
241
|
+
* @param options - Optional verification options.
|
|
242
|
+
* @returns `true` if the signature is valid, `false` otherwise.
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
* ```ts
|
|
246
|
+
* const isValid = signer.verify({ message: "Hello" }, signature, publicKey);
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
73
249
|
verify(data: any, signature: string, publicKey: string, options?: VerifyOptions): boolean;
|
|
250
|
+
/**
|
|
251
|
+
* Create a signed envelope containing the data and its signature (instance method).
|
|
252
|
+
*
|
|
253
|
+
* @param data - The data to include in the envelope.
|
|
254
|
+
* @param privateKey - The private key for signing.
|
|
255
|
+
* @param options - Optional signing options.
|
|
256
|
+
* @returns An object containing `{ data, signature }`.
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* ```ts
|
|
260
|
+
* const envelope = signer.envelope({ message: "Hello" }, privateKey);
|
|
261
|
+
* ```
|
|
262
|
+
*/
|
|
74
263
|
envelope(data: any, privateKey: string, options?: SignOptions): {
|
|
75
264
|
data: any;
|
|
76
265
|
signature: string;
|
|
77
266
|
};
|
|
267
|
+
/**
|
|
268
|
+
* Verify and extract data from a signed envelope (instance method).
|
|
269
|
+
*
|
|
270
|
+
* @param envelope - The envelope object `{ data, signature }`.
|
|
271
|
+
* @param publicKey - The public key to verify the signature.
|
|
272
|
+
* @param options - Optional verification options.
|
|
273
|
+
* @returns An object `{ valid, data }` indicating whether the signature is valid.
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
* ```ts
|
|
277
|
+
* const result = signer.openEnvelope(envelope, publicKey);
|
|
278
|
+
* ```
|
|
279
|
+
*/
|
|
78
280
|
openEnvelope(envelope: {
|
|
79
281
|
data: any;
|
|
80
282
|
signature: string;
|
|
@@ -84,6 +286,7 @@ declare class Signer {
|
|
|
84
286
|
};
|
|
85
287
|
}
|
|
86
288
|
|
|
289
|
+
/** Convenience functions using the default signer */
|
|
87
290
|
declare const envelope: (data: any, privateKey: string, options?: SignOptions) => {
|
|
88
291
|
data: any;
|
|
89
292
|
signature: string;
|
|
@@ -109,10 +312,71 @@ declare namespace index$3 {
|
|
|
109
312
|
export { type index$3_SerializationStrategy as SerializationStrategy, type index$3_SignOptions as SignOptions, index$3_Signer as Signer, type index$3_VerifyOptions as VerifyOptions, Signer as default, index$3_envelope as envelope, index$3_openEnvelope as openEnvelope, index$3_sign as sign, index$3_verify as verify };
|
|
110
313
|
}
|
|
111
314
|
|
|
315
|
+
/**
|
|
316
|
+
* Computes the SHA-256 hash of a given string.
|
|
317
|
+
*
|
|
318
|
+
* @param data - The input string to hash.
|
|
319
|
+
* @returns The SHA-256 hash of the input as a hexadecimal string.
|
|
320
|
+
*
|
|
321
|
+
* @example
|
|
322
|
+
* ```ts
|
|
323
|
+
* import { hash } from "./hash";
|
|
324
|
+
*
|
|
325
|
+
* const data = "Hello, world!";
|
|
326
|
+
* const hashed = hash(data);
|
|
327
|
+
*
|
|
328
|
+
* console.log(hashed); // e.g., "64ec88ca00b268e5ba1a35678a1b5316d212f4f366b247724e5b3f6d0f8c13f0"
|
|
329
|
+
* ```
|
|
330
|
+
*/
|
|
112
331
|
declare function hash(data: string): string;
|
|
113
332
|
|
|
333
|
+
/**
|
|
334
|
+
* Computes a SHA-256 HMAC for a given string using a secret key.
|
|
335
|
+
*
|
|
336
|
+
* HMAC (Hash-based Message Authentication Code) ensures both
|
|
337
|
+
* data integrity and authenticity.
|
|
338
|
+
*
|
|
339
|
+
* @param secret - The secret key used to compute the HMAC.
|
|
340
|
+
* @param data - The input string to hash.
|
|
341
|
+
* @returns The HMAC as a hexadecimal string.
|
|
342
|
+
*
|
|
343
|
+
* @example
|
|
344
|
+
* ```ts
|
|
345
|
+
* import { hashHmac } from "./hmac";
|
|
346
|
+
*
|
|
347
|
+
* const secret = "mysecretkey";
|
|
348
|
+
* const message = "Hello, world!";
|
|
349
|
+
* const hmac = hashHmac(secret, message);
|
|
350
|
+
*
|
|
351
|
+
* console.log(hmac); // e.g., "a6f5c3b2e4d1..."
|
|
352
|
+
* ```
|
|
353
|
+
*/
|
|
114
354
|
declare function hashHmac(secret: string, data: string): string;
|
|
115
355
|
|
|
356
|
+
/**
|
|
357
|
+
* Verifies a SHA-256 HMAC for a given string using a secret key.
|
|
358
|
+
*
|
|
359
|
+
* This function uses a **timing-safe comparison** to prevent
|
|
360
|
+
* timing attacks when comparing the expected HMAC to the actual one.
|
|
361
|
+
*
|
|
362
|
+
* @param secret - The secret key used to compute the HMAC.
|
|
363
|
+
* @param data - The input string to hash.
|
|
364
|
+
* @param expectedHex - The expected HMAC in hexadecimal format.
|
|
365
|
+
* @returns `true` if the computed HMAC matches the expected one, `false` otherwise.
|
|
366
|
+
*
|
|
367
|
+
* @example
|
|
368
|
+
* ```ts
|
|
369
|
+
* import { hashHmac, verifyHmac } from "./hmac";
|
|
370
|
+
*
|
|
371
|
+
* const secret = "mysecretkey";
|
|
372
|
+
* const message = "Hello, world!";
|
|
373
|
+
*
|
|
374
|
+
* const hmac = hashHmac(secret, message);
|
|
375
|
+
* const isValid = verifyHmac(secret, message, hmac);
|
|
376
|
+
*
|
|
377
|
+
* console.log(isValid); // true
|
|
378
|
+
* ```
|
|
379
|
+
*/
|
|
116
380
|
declare function verifyHmac(secret: string, data: string, expectedHex: string): boolean;
|
|
117
381
|
|
|
118
382
|
declare const index$2_hash: typeof hash;
|
|
@@ -122,22 +386,110 @@ declare namespace index$2 {
|
|
|
122
386
|
export { index$2_hash as hash, index$2_hashHmac as hashHmac, index$2_verifyHmac as verifyHmac };
|
|
123
387
|
}
|
|
124
388
|
|
|
389
|
+
/**
|
|
390
|
+
* Generates an X25519 key pair for Elliptic Curve Diffie-Hellman (ECDH).
|
|
391
|
+
*
|
|
392
|
+
* The keys are exported in DER format and encoded as Base64 strings:
|
|
393
|
+
* - Public key: SPKI (SubjectPublicKeyInfo)
|
|
394
|
+
* - Private key: PKCS#8
|
|
395
|
+
*
|
|
396
|
+
* This key pair is suitable for secure key agreement protocols.
|
|
397
|
+
*
|
|
398
|
+
* @returns An object containing:
|
|
399
|
+
* - `publicKey`: Base64-encoded X25519 public key (DER, SPKI)
|
|
400
|
+
* - `privateKey`: Base64-encoded X25519 private key (DER, PKCS#8)
|
|
401
|
+
*
|
|
402
|
+
* @example
|
|
403
|
+
* ```ts
|
|
404
|
+
* import { generateECDHKeyPair } from "./ecdh";
|
|
405
|
+
*
|
|
406
|
+
* const { publicKey, privateKey } = generateECDHKeyPair();
|
|
407
|
+
*
|
|
408
|
+
* console.log(publicKey); // Send to the peer
|
|
409
|
+
* console.log(privateKey); // Keep secret
|
|
410
|
+
* ```
|
|
411
|
+
*/
|
|
125
412
|
declare function generateECDHKeyPair(): {
|
|
126
413
|
publicKey: string;
|
|
127
414
|
privateKey: string;
|
|
128
415
|
};
|
|
129
416
|
|
|
417
|
+
/**
|
|
418
|
+
* Generates an RSA key pair for encryption or digital signatures.
|
|
419
|
+
*
|
|
420
|
+
* - Algorithm: RSA
|
|
421
|
+
* - Key size: 2048 bits
|
|
422
|
+
* - Public key: SPKI (DER, base64-encoded)
|
|
423
|
+
* - Private key: PKCS#8 (DER, base64-encoded)
|
|
424
|
+
*
|
|
425
|
+
* @returns A Promise that resolves to an object containing:
|
|
426
|
+
* - `publicKey`: RSA public key in base64-encoded DER format
|
|
427
|
+
* - `privateKey`: RSA private key in base64-encoded DER format
|
|
428
|
+
*
|
|
429
|
+
* @example
|
|
430
|
+
* ```ts
|
|
431
|
+
* import { generateRSAKeyPair } from "./rsa";
|
|
432
|
+
*
|
|
433
|
+
* async function main() {
|
|
434
|
+
* const { publicKey, privateKey } = await generateRSAKeyPair();
|
|
435
|
+
*
|
|
436
|
+
* console.log(publicKey); // Can be shared (base64 string)
|
|
437
|
+
* console.log(privateKey); // Keep secret (base64 string)
|
|
438
|
+
* }
|
|
439
|
+
*
|
|
440
|
+
* main();
|
|
441
|
+
* ```
|
|
442
|
+
*/
|
|
130
443
|
declare function generateRSAKeyPair(): Promise<{
|
|
131
444
|
publicKey: string;
|
|
132
445
|
privateKey: string;
|
|
133
446
|
}>;
|
|
134
447
|
|
|
135
448
|
type keyType = "seal" | "sign" | "secure-channel" | "authenticated-channel";
|
|
449
|
+
/**
|
|
450
|
+
* Represents a cryptographic key or key pair for various use cases.
|
|
451
|
+
*
|
|
452
|
+
* Depending on the `keyType`, the instance may contain:
|
|
453
|
+
* - Encryption keys (`publicKey` / `privateKey`)
|
|
454
|
+
* - Signing keys (`signingPublicKey` / `signingPrivateKey`)
|
|
455
|
+
*/
|
|
136
456
|
declare class Key {
|
|
457
|
+
/** Public key for encryption or signing (Base64 or PEM depending on type) */
|
|
137
458
|
publicKey?: string;
|
|
459
|
+
/** Private key for encryption or signing (Base64 or PEM depending on type) */
|
|
138
460
|
privateKey?: string;
|
|
461
|
+
/** Public key specifically for signing (Base64) */
|
|
139
462
|
signingPublicKey?: string;
|
|
463
|
+
/** Private key specifically for signing (Base64) */
|
|
140
464
|
signingPrivateKey?: string;
|
|
465
|
+
/**
|
|
466
|
+
* Generates a new Key instance for the specified `keyType`.
|
|
467
|
+
*
|
|
468
|
+
* @param key - The type of key to generate:
|
|
469
|
+
* - `"seal"`: RSA key pair for encryption/signing
|
|
470
|
+
* - `"sign"`: Ed25519 key pair for signing
|
|
471
|
+
* - `"secure-channel"`: X25519 key pair for ECDH (secure channel)
|
|
472
|
+
* - `"authenticated-channel"`: Combined X25519 + Ed25519 key pair
|
|
473
|
+
*
|
|
474
|
+
* @returns A Promise that resolves to a `Key` instance with the generated keys.
|
|
475
|
+
*
|
|
476
|
+
* @example
|
|
477
|
+
* ```ts
|
|
478
|
+
* import { Key } from "./key";
|
|
479
|
+
*
|
|
480
|
+
* async function main() {
|
|
481
|
+
* const sealKey = await Key.generate("seal");
|
|
482
|
+
* console.log(sealKey.publicKey);
|
|
483
|
+
* console.log(sealKey.privateKey);
|
|
484
|
+
*
|
|
485
|
+
* const authKey = await Key.generate("authenticated-channel");
|
|
486
|
+
* console.log(authKey.publicKey); // Encryption key
|
|
487
|
+
* console.log(authKey.signingPublicKey); // Signing key
|
|
488
|
+
* }
|
|
489
|
+
*
|
|
490
|
+
* main();
|
|
491
|
+
* ```
|
|
492
|
+
*/
|
|
141
493
|
static generate(key: keyType): Promise<Key>;
|
|
142
494
|
}
|
|
143
495
|
|
|
@@ -150,8 +502,49 @@ declare namespace index$1 {
|
|
|
150
502
|
export { index$1_Key as Key, index$1_generateECDHKeyPair as generateECDHKeyPair, index$1_generateRSAKeyPair as generateRSAKeyPair, type index$1_keyType as keyType };
|
|
151
503
|
}
|
|
152
504
|
|
|
505
|
+
/**
|
|
506
|
+
* Generates a Time-based One-Time Password (TOTP) according to RFC 6238.
|
|
507
|
+
*
|
|
508
|
+
* @param secret - The shared secret in Base32 encoding.
|
|
509
|
+
* @param digits - Number of digits in the OTP (default: 6).
|
|
510
|
+
* @param period - Time step in seconds (default: 30).
|
|
511
|
+
* @param timestamp - Unix timestamp in milliseconds (default: current time).
|
|
512
|
+
* @returns A numeric OTP as a string, zero-padded to the specified length.
|
|
513
|
+
*
|
|
514
|
+
* @example
|
|
515
|
+
* ```ts
|
|
516
|
+
* import { generateTOTP } from "./totp";
|
|
517
|
+
*
|
|
518
|
+
* const secret = "JBSWY3DPEHPK3PXP"; // Base32 secret
|
|
519
|
+
* const otp = generateTOTP(secret);
|
|
520
|
+
* console.log(otp); // e.g., "492039"
|
|
521
|
+
*
|
|
522
|
+
* // Generate a 8-digit OTP with a 60-second period
|
|
523
|
+
* const otp8 = generateTOTP(secret, 8, 60);
|
|
524
|
+
* console.log(otp8);
|
|
525
|
+
* ```
|
|
526
|
+
*/
|
|
153
527
|
declare function generateTOTP(secret: string, digits?: number, period?: number, timestamp?: number): string;
|
|
154
528
|
|
|
529
|
+
/**
|
|
530
|
+
* Generates a numeric one-time password (OTP) of a specified length.
|
|
531
|
+
*
|
|
532
|
+
* The OTP consists only of digits (0–9) and is padded with leading zeros
|
|
533
|
+
* if necessary.
|
|
534
|
+
*
|
|
535
|
+
* @param length - The length of the OTP (default: 6). Must be a positive integer.
|
|
536
|
+
* @returns A string representing the numeric OTP.
|
|
537
|
+
*
|
|
538
|
+
* @example
|
|
539
|
+
* ```ts
|
|
540
|
+
* import { generateOTP } from "./otp";
|
|
541
|
+
*
|
|
542
|
+
* const otp = generateOTP(); // e.g., "084321"
|
|
543
|
+
* const otp8 = generateOTP(8); // e.g., "09238475"
|
|
544
|
+
*
|
|
545
|
+
* console.log(otp, otp8);
|
|
546
|
+
* ```
|
|
547
|
+
*/
|
|
155
548
|
declare function generateOTP(length?: number): string;
|
|
156
549
|
|
|
157
550
|
declare const index_generateOTP: typeof generateOTP;
|
|
@@ -193,19 +586,201 @@ interface AuthenticatedChannelOptions$1 {
|
|
|
193
586
|
}
|
|
194
587
|
type MessageEncryptOptions = SymmetricPasswordOptions$1 | SealEnvelopeOptions$1 | SecureChannelOptions$1 | AuthenticatedChannelOptions$1;
|
|
195
588
|
type MessageData$1 = string | object | Buffer;
|
|
589
|
+
/**
|
|
590
|
+
* Validates password strength
|
|
591
|
+
* @param password - The password to validate
|
|
592
|
+
* @param strictMode - If true, enforces stricter validation rules
|
|
593
|
+
* @throws {Error} If password is too short or doesn't meet requirements
|
|
594
|
+
* @example
|
|
595
|
+
* validatePassword("MyP@ssw0rd123", false);
|
|
596
|
+
*/
|
|
196
597
|
declare function validatePassword(password: string, strictMode?: boolean): void;
|
|
598
|
+
/**
|
|
599
|
+
* Validates public key format and type
|
|
600
|
+
* @param keyStr - Base64-encoded public key in SPKI format
|
|
601
|
+
* @param expectedType - Expected key type ('rsa' or 'x25519')
|
|
602
|
+
* @throws {Error} If key is invalid or doesn't match expected type
|
|
603
|
+
* @example
|
|
604
|
+
* validatePublicKey("MIIBIjANBgkq...", "rsa");
|
|
605
|
+
*/
|
|
197
606
|
declare function validatePublicKey(keyStr: string, expectedType: "rsa" | "x25519"): void;
|
|
607
|
+
/**
|
|
608
|
+
* Validates private key format and type
|
|
609
|
+
* @param keyStr - Base64-encoded private key in PKCS8 format
|
|
610
|
+
* @param expectedType - Expected key type ('rsa' or 'ed25519')
|
|
611
|
+
* @throws {Error} If key is invalid or doesn't match expected type
|
|
612
|
+
* @example
|
|
613
|
+
* validatePrivateKey("MIIEvQIBADANBgkq...", "ed25519");
|
|
614
|
+
*/
|
|
198
615
|
declare function validatePrivateKey(keyStr: string, expectedType: "rsa" | "ed25519"): void;
|
|
616
|
+
/**
|
|
617
|
+
* Encrypt a message with symmetric password encryption
|
|
618
|
+
* @param options - Symmetric password encryption options
|
|
619
|
+
* @param data - Data to encrypt (string, object, or Buffer)
|
|
620
|
+
* @returns Promise resolving to encryption result with hex-encoded data
|
|
621
|
+
* @example
|
|
622
|
+
* const result = await encrypt(
|
|
623
|
+
* { type: "symmetric-password", password: "MySecureP@ss123" },
|
|
624
|
+
* "Secret message"
|
|
625
|
+
* );
|
|
626
|
+
* console.log(result.data); // hex string
|
|
627
|
+
*/
|
|
199
628
|
declare function encrypt(options: SymmetricPasswordOptions$1, data: MessageData$1): Promise<EncryptResult$1>;
|
|
629
|
+
/**
|
|
630
|
+
* Encrypt a message using RSA sealed envelope
|
|
631
|
+
* @param options - Sealed envelope encryption options
|
|
632
|
+
* @param data - Data to encrypt (string, object, or Buffer)
|
|
633
|
+
* @returns Promise resolving to encryption result with hex-encoded data
|
|
634
|
+
* @example
|
|
635
|
+
* const result = await encrypt(
|
|
636
|
+
* {
|
|
637
|
+
* type: "sealEnvelope",
|
|
638
|
+
* recipientPublicKey: "MIIBIjANBgkq..."
|
|
639
|
+
* },
|
|
640
|
+
* { message: "Secret data", value: 42 }
|
|
641
|
+
* );
|
|
642
|
+
*/
|
|
200
643
|
declare function encrypt(options: SealEnvelopeOptions$1, data: MessageData$1): Promise<EncryptResult$1>;
|
|
644
|
+
/**
|
|
645
|
+
* Encrypt a message using ECDH secure channel
|
|
646
|
+
* @param options - Secure channel encryption options
|
|
647
|
+
* @param data - Data to encrypt (string, object, or Buffer)
|
|
648
|
+
* @returns Promise resolving to encryption result with hex-encoded data
|
|
649
|
+
* @example
|
|
650
|
+
* const result = await encrypt(
|
|
651
|
+
* {
|
|
652
|
+
* type: "secure-channel",
|
|
653
|
+
* recipientPublicKey: "MCowBQYDK2VuAyEA...",
|
|
654
|
+
* includeTimestamp: true
|
|
655
|
+
* },
|
|
656
|
+
* "Timestamped message"
|
|
657
|
+
* );
|
|
658
|
+
*/
|
|
201
659
|
declare function encrypt(options: SecureChannelOptions$1, data: MessageData$1): Promise<EncryptResult$1>;
|
|
660
|
+
/**
|
|
661
|
+
* Encrypt a message using authenticated channel (ECDH + Ed25519 signature)
|
|
662
|
+
* @param options - Authenticated channel encryption options
|
|
663
|
+
* @param data - Data to encrypt (string, object, or Buffer)
|
|
664
|
+
* @returns Promise resolving to encryption result with hex-encoded data
|
|
665
|
+
* @example
|
|
666
|
+
* const result = await encrypt(
|
|
667
|
+
* {
|
|
668
|
+
* type: "authenticated-channel",
|
|
669
|
+
* recipientPublicKey: "MCowBQYDK2VuAyEA...",
|
|
670
|
+
* senderPrivateKey: "MC4CAQAwBQYDK2Vw...",
|
|
671
|
+
* includeTimestamp: true
|
|
672
|
+
* },
|
|
673
|
+
* "Signed and encrypted message"
|
|
674
|
+
* );
|
|
675
|
+
*/
|
|
202
676
|
declare function encrypt(options: AuthenticatedChannelOptions$1, data: MessageData$1): Promise<EncryptResult$1>;
|
|
677
|
+
/**
|
|
678
|
+
* Encrypt a file with symmetric password encryption
|
|
679
|
+
* @param options - Symmetric password encryption options
|
|
680
|
+
* @param data - Unused for file mode (pass null or undefined)
|
|
681
|
+
* @param inputPath - Path to input file
|
|
682
|
+
* @param outputPath - Path to output encrypted file
|
|
683
|
+
* @returns Promise resolving to encryption result with output path
|
|
684
|
+
* @example
|
|
685
|
+
* const result = await encrypt(
|
|
686
|
+
* { type: "symmetric-password", password: "MyP@ss123" },
|
|
687
|
+
* null,
|
|
688
|
+
* "/path/to/document.pdf",
|
|
689
|
+
* "/path/to/document.pdf.enc"
|
|
690
|
+
* );
|
|
691
|
+
*/
|
|
203
692
|
declare function encrypt(options: SymmetricPasswordOptions$1, data: null | undefined, inputPath: string, outputPath: string): Promise<EncryptResult$1>;
|
|
693
|
+
/**
|
|
694
|
+
* Encrypt a file using RSA sealed envelope
|
|
695
|
+
* @param options - Sealed envelope encryption options
|
|
696
|
+
* @param data - Unused for file mode (pass null or undefined)
|
|
697
|
+
* @param inputPath - Path to input file
|
|
698
|
+
* @param outputPath - Path to output encrypted file
|
|
699
|
+
* @returns Promise resolving to encryption result with output path
|
|
700
|
+
* @example
|
|
701
|
+
* const result = await encrypt(
|
|
702
|
+
* { type: "sealEnvelope", recipientPublicKey: "MIIBIjANBgkq..." },
|
|
703
|
+
* null,
|
|
704
|
+
* "/path/to/video.mp4",
|
|
705
|
+
* "/path/to/video.mp4.enc"
|
|
706
|
+
* );
|
|
707
|
+
*/
|
|
204
708
|
declare function encrypt(options: SealEnvelopeOptions$1, data: null | undefined, inputPath: string, outputPath: string): Promise<EncryptResult$1>;
|
|
709
|
+
/**
|
|
710
|
+
* Encrypt a file using ECDH secure channel
|
|
711
|
+
* @param options - Secure channel encryption options
|
|
712
|
+
* @param data - Unused for file mode (pass null or undefined)
|
|
713
|
+
* @param inputPath - Path to input file
|
|
714
|
+
* @param outputPath - Path to output encrypted file
|
|
715
|
+
* @returns Promise resolving to encryption result with output path
|
|
716
|
+
* @example
|
|
717
|
+
* const result = await encrypt(
|
|
718
|
+
* {
|
|
719
|
+
* type: "secure-channel",
|
|
720
|
+
* recipientPublicKey: "MCowBQYDK2VuAyEA...",
|
|
721
|
+
* includeTimestamp: true
|
|
722
|
+
* },
|
|
723
|
+
* null,
|
|
724
|
+
* "/path/to/archive.zip",
|
|
725
|
+
* "/path/to/archive.zip.enc"
|
|
726
|
+
* );
|
|
727
|
+
*/
|
|
205
728
|
declare function encrypt(options: SecureChannelOptions$1, data: null | undefined, inputPath: string, outputPath: string): Promise<EncryptResult$1>;
|
|
729
|
+
/**
|
|
730
|
+
* Encrypt a file using authenticated channel
|
|
731
|
+
* @param options - Authenticated channel encryption options
|
|
732
|
+
* @param data - Unused for file mode (pass null or undefined)
|
|
733
|
+
* @param inputPath - Path to input file
|
|
734
|
+
* @param outputPath - Path to output encrypted file
|
|
735
|
+
* @returns Promise resolving to encryption result with output path
|
|
736
|
+
* @example
|
|
737
|
+
* const result = await encrypt(
|
|
738
|
+
* {
|
|
739
|
+
* type: "authenticated-channel",
|
|
740
|
+
* recipientPublicKey: "MCowBQYDK2VuAyEA...",
|
|
741
|
+
* senderPrivateKey: "MC4CAQAwBQYDK2Vw...",
|
|
742
|
+
* includeTimestamp: true
|
|
743
|
+
* },
|
|
744
|
+
* null,
|
|
745
|
+
* "/path/to/backup.tar.gz",
|
|
746
|
+
* "/path/to/backup.tar.gz.enc"
|
|
747
|
+
* );
|
|
748
|
+
*/
|
|
206
749
|
declare function encrypt(options: AuthenticatedChannelOptions$1, data: null | undefined, inputPath: string, outputPath: string): Promise<EncryptResult$1>;
|
|
750
|
+
/**
|
|
751
|
+
* Encrypt a file using streaming (memory-efficient for large files)
|
|
752
|
+
* @param options - Encryption options
|
|
753
|
+
* @param inputPath - Path to input file
|
|
754
|
+
* @param outputPath - Path to output encrypted file
|
|
755
|
+
* @throws {Error} If encryption fails
|
|
756
|
+
* @example
|
|
757
|
+
* await encryptFileStreaming(
|
|
758
|
+
* { type: "sealEnvelope", recipientPublicKey: "MIIBIjANBgkq..." },
|
|
759
|
+
* "./large-video.mp4",
|
|
760
|
+
* "./large-video.mp4.enc"
|
|
761
|
+
* );
|
|
762
|
+
*/
|
|
207
763
|
declare function encryptFileStreaming(options: MessageEncryptOptions, inputPath: string, outputPath: string): Promise<void>;
|
|
764
|
+
/**
|
|
765
|
+
* Encrypt a message (string, object, or any JSON-serializable data)
|
|
766
|
+
* @param options - Encryption options
|
|
767
|
+
* @param data - Data to encrypt
|
|
768
|
+
* @returns Hex-encoded encrypted message
|
|
769
|
+
* @example
|
|
770
|
+
* const encrypted = encryptMessage(
|
|
771
|
+
* { type: "symmetric-password", password: "MyP@ss123" },
|
|
772
|
+
* { user: "john", balance: 1000 }
|
|
773
|
+
* );
|
|
774
|
+
*/
|
|
208
775
|
declare function encryptMessage(options: MessageEncryptOptions, data: MessageData$1): string;
|
|
776
|
+
/**
|
|
777
|
+
* Generate ephemeral key pair and derive AES key using ECDH
|
|
778
|
+
* @param recipientPublicKeyStr - Base64-encoded X25519 public key
|
|
779
|
+
* @returns Object containing derived AES key and ephemeral keys
|
|
780
|
+
* @example
|
|
781
|
+
* const derived = deriveAESKeyForEncryption("MCowBQYDK2VuAyEA...");
|
|
782
|
+
* console.log(derived.aesKey.length); // 32 bytes
|
|
783
|
+
*/
|
|
209
784
|
declare function deriveAESKeyForEncryption(recipientPublicKeyStr: string): {
|
|
210
785
|
aesKey: Buffer;
|
|
211
786
|
ephemeralPublicKey: string;
|
|
@@ -252,23 +827,229 @@ interface AuthenticatedChannelDecryptOptions {
|
|
|
252
827
|
}
|
|
253
828
|
type MessageDecryptOptions = SymmetricPasswordDecryptOptions | OpenEnvelopeDecryptOptions | SecureChannelDecryptOptions | AuthenticatedChannelDecryptOptions;
|
|
254
829
|
type MessageData = string | Buffer;
|
|
830
|
+
/**
|
|
831
|
+
* Validate timestamp to prevent replay attacks
|
|
832
|
+
* @param timestamp - Unix timestamp in milliseconds
|
|
833
|
+
* @param maxAge - Maximum allowed age in milliseconds
|
|
834
|
+
* @throws {Error} If timestamp is invalid or too old
|
|
835
|
+
* @example
|
|
836
|
+
* validateTimestamp(Date.now() - 60000); // Valid: 1 minute old
|
|
837
|
+
* validateTimestamp(Date.now() - 600000); // Invalid: 10 minutes old (exceeds 5 min default)
|
|
838
|
+
*/
|
|
255
839
|
declare function validateTimestamp(timestamp: number, maxAge?: number): void;
|
|
840
|
+
/**
|
|
841
|
+
* Validate format version
|
|
842
|
+
* @param version - Version number from encrypted data
|
|
843
|
+
* @throws {Error} If version is not supported
|
|
844
|
+
* @example
|
|
845
|
+
* validateVersion(0x01); // Valid
|
|
846
|
+
* validateVersion(0x99); // Throws error
|
|
847
|
+
*/
|
|
256
848
|
declare function validateVersion(version: number): void;
|
|
849
|
+
/**
|
|
850
|
+
* Decrypt a message encrypted with symmetric password
|
|
851
|
+
* @param options - Symmetric password decryption options
|
|
852
|
+
* @param data - Hex-encoded encrypted message
|
|
853
|
+
* @returns Promise resolving to decryption result with data and metadata
|
|
854
|
+
* @example
|
|
855
|
+
* const result = await decrypt(
|
|
856
|
+
* { type: "symmetric-password", password: "MySecureP@ss123" },
|
|
857
|
+
* "0100a1b2c3d4..." // hex string from encrypt()
|
|
858
|
+
* );
|
|
859
|
+
* console.log(result.data); // "Secret message" or { key: "value" }
|
|
860
|
+
*/
|
|
257
861
|
declare function decrypt(options: SymmetricPasswordDecryptOptions, data: MessageData): Promise<MessageDecryptResult>;
|
|
862
|
+
/**
|
|
863
|
+
* Decrypt a message encrypted with RSA sealed envelope
|
|
864
|
+
* @param options - Open envelope decryption options
|
|
865
|
+
* @param data - Hex-encoded encrypted message
|
|
866
|
+
* @returns Promise resolving to decryption result with data and metadata
|
|
867
|
+
* @example
|
|
868
|
+
* const result = await decrypt(
|
|
869
|
+
* {
|
|
870
|
+
* type: "openEnvelope",
|
|
871
|
+
* recipientPrivateKey: "MIIEvQIBADANBgkq..."
|
|
872
|
+
* },
|
|
873
|
+
* encryptedHex
|
|
874
|
+
* );
|
|
875
|
+
* console.log(result.data);
|
|
876
|
+
*/
|
|
258
877
|
declare function decrypt(options: OpenEnvelopeDecryptOptions, data: MessageData): Promise<MessageDecryptResult>;
|
|
878
|
+
/**
|
|
879
|
+
* Decrypt a message encrypted with ECDH secure channel
|
|
880
|
+
* @param options - Secure channel decryption options
|
|
881
|
+
* @param data - Hex-encoded encrypted message
|
|
882
|
+
* @returns Promise resolving to decryption result with data, timestamp, and metadata
|
|
883
|
+
* @example
|
|
884
|
+
* const result = await decrypt(
|
|
885
|
+
* {
|
|
886
|
+
* type: "secure-channel",
|
|
887
|
+
* recipientPrivateKey: "MC4CAQAwBQYDK2VuBCIAIE...",
|
|
888
|
+
* validateTimestamp: true
|
|
889
|
+
* },
|
|
890
|
+
* encryptedHex
|
|
891
|
+
* );
|
|
892
|
+
* console.log(result.metadata?.timestamp); // Unix timestamp
|
|
893
|
+
*/
|
|
259
894
|
declare function decrypt(options: SecureChannelDecryptOptions, data: MessageData): Promise<MessageDecryptResult>;
|
|
895
|
+
/**
|
|
896
|
+
* Decrypt a message encrypted with authenticated channel (ECDH + Ed25519)
|
|
897
|
+
* @param options - Authenticated channel decryption options
|
|
898
|
+
* @param data - Hex-encoded encrypted message
|
|
899
|
+
* @returns Promise resolving to decryption result with verified data and metadata
|
|
900
|
+
* @example
|
|
901
|
+
* const result = await decrypt(
|
|
902
|
+
* {
|
|
903
|
+
* type: "authenticated-channel",
|
|
904
|
+
* recipientPrivateKey: "MC4CAQAwBQYDK2VuBCIAIE...",
|
|
905
|
+
* senderPublicKey: "MCowBQYDK2VwAyEA...",
|
|
906
|
+
* validateTimestamp: true
|
|
907
|
+
* },
|
|
908
|
+
* encryptedHex
|
|
909
|
+
* );
|
|
910
|
+
* console.log(result.metadata?.authenticated); // true
|
|
911
|
+
*/
|
|
260
912
|
declare function decrypt(options: AuthenticatedChannelDecryptOptions, data: MessageData): Promise<MessageDecryptResult>;
|
|
913
|
+
/**
|
|
914
|
+
* Decrypt a file encrypted with symmetric password
|
|
915
|
+
* @param options - Symmetric password decryption options
|
|
916
|
+
* @param data - Unused for file mode (pass null or undefined)
|
|
917
|
+
* @param inputPath - Path to encrypted file
|
|
918
|
+
* @param outputPath - Path where decrypted file will be saved
|
|
919
|
+
* @returns Promise resolving to file decryption result
|
|
920
|
+
* @example
|
|
921
|
+
* const result = await decrypt(
|
|
922
|
+
* { type: "symmetric-password", password: "MyP@ss123" },
|
|
923
|
+
* null,
|
|
924
|
+
* "./document.pdf.enc",
|
|
925
|
+
* "./document.pdf"
|
|
926
|
+
* );
|
|
927
|
+
* console.log(result.outputPath); // "./document.pdf"
|
|
928
|
+
*/
|
|
261
929
|
declare function decrypt(options: SymmetricPasswordDecryptOptions, data: null | undefined, inputPath: string, outputPath: string): Promise<FileDecryptResult>;
|
|
930
|
+
/**
|
|
931
|
+
* Decrypt a file encrypted with RSA sealed envelope
|
|
932
|
+
* @param options - Open envelope decryption options
|
|
933
|
+
* @param data - Unused for file mode (pass null or undefined)
|
|
934
|
+
* @param inputPath - Path to encrypted file
|
|
935
|
+
* @param outputPath - Path where decrypted file will be saved
|
|
936
|
+
* @returns Promise resolving to file decryption result
|
|
937
|
+
* @example
|
|
938
|
+
* const result = await decrypt(
|
|
939
|
+
* {
|
|
940
|
+
* type: "openEnvelope",
|
|
941
|
+
* recipientPrivateKey: "MIIEvQIBADANBgkq..."
|
|
942
|
+
* },
|
|
943
|
+
* null,
|
|
944
|
+
* "./video.mp4.enc",
|
|
945
|
+
* "./video.mp4"
|
|
946
|
+
* );
|
|
947
|
+
*/
|
|
262
948
|
declare function decrypt(options: OpenEnvelopeDecryptOptions, data: null | undefined, inputPath: string, outputPath: string): Promise<FileDecryptResult>;
|
|
949
|
+
/**
|
|
950
|
+
* Decrypt a file encrypted with ECDH secure channel
|
|
951
|
+
* @param options - Secure channel decryption options
|
|
952
|
+
* @param data - Unused for file mode (pass null or undefined)
|
|
953
|
+
* @param inputPath - Path to encrypted file
|
|
954
|
+
* @param outputPath - Path where decrypted file will be saved
|
|
955
|
+
* @returns Promise resolving to file decryption result
|
|
956
|
+
* @example
|
|
957
|
+
* const result = await decrypt(
|
|
958
|
+
* {
|
|
959
|
+
* type: "secure-channel",
|
|
960
|
+
* recipientPrivateKey: "MC4CAQAwBQYDK2VuBCIAIE...",
|
|
961
|
+
* validateTimestamp: true
|
|
962
|
+
* },
|
|
963
|
+
* null,
|
|
964
|
+
* "./archive.zip.enc",
|
|
965
|
+
* "./archive.zip"
|
|
966
|
+
* );
|
|
967
|
+
*/
|
|
263
968
|
declare function decrypt(options: SecureChannelDecryptOptions, data: null | undefined, inputPath: string, outputPath: string): Promise<FileDecryptResult>;
|
|
969
|
+
/**
|
|
970
|
+
* Decrypt a file encrypted with authenticated channel
|
|
971
|
+
* @param options - Authenticated channel decryption options
|
|
972
|
+
* @param data - Unused for file mode (pass null or undefined)
|
|
973
|
+
* @param inputPath - Path to encrypted file
|
|
974
|
+
* @param outputPath - Path where decrypted file will be saved
|
|
975
|
+
* @returns Promise resolving to file decryption result with authentication metadata
|
|
976
|
+
* @example
|
|
977
|
+
* const result = await decrypt(
|
|
978
|
+
* {
|
|
979
|
+
* type: "authenticated-channel",
|
|
980
|
+
* recipientPrivateKey: "MC4CAQAwBQYDK2VuBCIAIE...",
|
|
981
|
+
* senderPublicKey: "MCowBQYDK2VwAyEA...",
|
|
982
|
+
* validateTimestamp: true
|
|
983
|
+
* },
|
|
984
|
+
* null,
|
|
985
|
+
* "./backup.tar.gz.enc",
|
|
986
|
+
* "./backup.tar.gz"
|
|
987
|
+
* );
|
|
988
|
+
*/
|
|
264
989
|
declare function decrypt(options: AuthenticatedChannelDecryptOptions, data: null | undefined, inputPath: string, outputPath: string): Promise<FileDecryptResult>;
|
|
990
|
+
/**
|
|
991
|
+
* Decrypt a file using streaming for memory efficiency
|
|
992
|
+
* @param options - Decryption options
|
|
993
|
+
* @param inputPath - Path to encrypted file
|
|
994
|
+
* @param outputPath - Path where decrypted file will be saved
|
|
995
|
+
* @throws {Error} If decryption fails or file format is invalid
|
|
996
|
+
* @example
|
|
997
|
+
* await decryptFile(
|
|
998
|
+
* { type: "symmetric-password", password: "MyP@ss123" },
|
|
999
|
+
* "./document.pdf.enc",
|
|
1000
|
+
* "./document.pdf"
|
|
1001
|
+
* );
|
|
1002
|
+
*/
|
|
265
1003
|
declare function decryptFile(options: MessageDecryptOptions, inputPath: string, outputPath: string): Promise<void>;
|
|
1004
|
+
/**
|
|
1005
|
+
* Decrypt a message (hex string) and return original data
|
|
1006
|
+
* @param options - Decryption options
|
|
1007
|
+
* @param encryptedHex - Hex-encoded encrypted message
|
|
1008
|
+
* @returns Object containing decrypted data and metadata
|
|
1009
|
+
* @throws {Error} If decryption fails, password is wrong, signature is invalid, or format is corrupted
|
|
1010
|
+
* @example
|
|
1011
|
+
* const result = decryptMessage(
|
|
1012
|
+
* { type: "symmetric-password", password: "MyP@ss123" },
|
|
1013
|
+
* "0100a1b2c3d4e5f6..." // hex string
|
|
1014
|
+
* );
|
|
1015
|
+
* console.log(result.data); // "Secret message" or { key: "value" }
|
|
1016
|
+
* console.log(result.metadata?.timestamp); // Unix timestamp if present
|
|
1017
|
+
*/
|
|
266
1018
|
declare function decryptMessage(options: MessageDecryptOptions, encryptedHex: string): {
|
|
267
1019
|
data: string | object;
|
|
268
1020
|
metadata?: DecryptMetadata;
|
|
269
1021
|
};
|
|
1022
|
+
/**
|
|
1023
|
+
* Derive AES key from recipient's private key and ephemeral public key using ECDH
|
|
1024
|
+
* @param recipientPrivateKeyStr - Base64-encoded X25519 private key in PKCS8 format
|
|
1025
|
+
* @param ephemeralPublicKeyStr - Base64-encoded X25519 ephemeral public key from sender
|
|
1026
|
+
* @param salt - 16-byte salt for key derivation
|
|
1027
|
+
* @returns 32-byte AES-256 key derived using HKDF
|
|
1028
|
+
* @example
|
|
1029
|
+
* const aesKey = deriveAESKeyForDecryption(
|
|
1030
|
+
* "MC4CAQAwBQYDK2VuBCIAIE...",
|
|
1031
|
+
* "MCowBQYDK2VuAyEA...",
|
|
1032
|
+
* Buffer.from("a1b2c3d4e5f6...", "hex")
|
|
1033
|
+
* );
|
|
1034
|
+
* console.log(aesKey.length); // 32 bytes
|
|
1035
|
+
*/
|
|
270
1036
|
declare function deriveAESKeyForDecryption(recipientPrivateKeyStr: string, ephemeralPublicKeyStr: string, salt: Buffer): Buffer;
|
|
271
1037
|
|
|
1038
|
+
/**
|
|
1039
|
+
* Crypto Utils - Main Export File
|
|
1040
|
+
*
|
|
1041
|
+
* A comprehensive educational cryptography library implementing:
|
|
1042
|
+
* - Symmetric encryption (AES-256-GCM with password)
|
|
1043
|
+
* - Asymmetric encryption (RSA-OAEP)
|
|
1044
|
+
* - ECDH key exchange (X25519)
|
|
1045
|
+
* - Digital signatures (Ed25519)
|
|
1046
|
+
* - Forward secrecy
|
|
1047
|
+
* - Replay attack prevention
|
|
1048
|
+
* -
|
|
1049
|
+
* @module crypto-utils
|
|
1050
|
+
* @version 1.0.0
|
|
1051
|
+
*/
|
|
1052
|
+
|
|
272
1053
|
interface SymmetricPasswordOptions {
|
|
273
1054
|
type: "symmetric-password";
|
|
274
1055
|
password: string;
|