@alessiofrittoli/crypto-cipher 3.0.0-alpha.1 → 3.0.0-alpha.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-B9WIVWrs.d.mts +364 -0
- package/dist/index-B9WIVWrs.d.ts +364 -0
- package/dist/index.d.mts +3 -236
- package/dist/index.d.ts +3 -236
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/dist/types.d.mts +4 -115
- package/dist/types.d.ts +4 -115
- package/package.json +6 -4
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
import crypto, { KeyLike, Cipheriv, Decipheriv } from 'crypto';
|
|
2
|
+
import { CoerceToUint8ArrayInput } from '@alessiofrittoli/crypto-buffer';
|
|
3
|
+
import { Readable, Writable, Transform } from 'stream';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Cipher types.
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
declare namespace Cph {
|
|
10
|
+
/** Cipher CBC algorithm types. */
|
|
11
|
+
type CBCTypes = 'aes-128-cbc' | 'aes-192-cbc' | 'aes-256-cbc';
|
|
12
|
+
/** Cipher supported algorithm types. */
|
|
13
|
+
type AesAlgorithm = typeof Cipher.ALGORITHM[keyof typeof Cipher.ALGORITHM];
|
|
14
|
+
/**
|
|
15
|
+
* Common Cipher options.
|
|
16
|
+
* @template T The AES algorithm type.
|
|
17
|
+
*/
|
|
18
|
+
interface Options<T extends Cph.AesAlgorithm = Cph.AesAlgorithm> {
|
|
19
|
+
/** The Cipher algorithm to use. Default: `aes-256-gcm` | `aes-256-cbc`. */
|
|
20
|
+
algorithm?: T;
|
|
21
|
+
/** The salt length. Minimum: `16`, Maximum: `64`. Default: `32`. */
|
|
22
|
+
salt?: number;
|
|
23
|
+
/** The `IV` length. Minimum: `8`, Maximum: `32`. Default: `16`. */
|
|
24
|
+
iv?: number;
|
|
25
|
+
/** The `authTag` length. Minimum: `4`, Maximum: `16`. Default: `16`. */
|
|
26
|
+
authTag?: number;
|
|
27
|
+
/** Custom `Additional Authenticated Data`. */
|
|
28
|
+
aad?: CoerceToUint8ArrayInput;
|
|
29
|
+
/** The `AAD` length. Minimum: `16`, Maximum: `128`. Default: `32`. */
|
|
30
|
+
aadLength?: number;
|
|
31
|
+
}
|
|
32
|
+
interface NewKeyIVOptions<T extends Cph.AesAlgorithm = Cph.AesAlgorithm> extends Cph.Options<T> {
|
|
33
|
+
secret?: CoerceToUint8ArrayInput;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Resolved Common options.
|
|
37
|
+
*/
|
|
38
|
+
interface ResolvedOptions<T extends Cph.AesAlgorithm = Cph.AesAlgorithm> extends Required<Omit<Cph.Options<T>, 'aad'>> {
|
|
39
|
+
/** The symmetric key length. */
|
|
40
|
+
length: number;
|
|
41
|
+
/** The Additional Authentication Data. */
|
|
42
|
+
aad?: Uint8Array;
|
|
43
|
+
}
|
|
44
|
+
type PrivateKey = KeyLike | {
|
|
45
|
+
key: KeyLike;
|
|
46
|
+
passphrase?: string;
|
|
47
|
+
};
|
|
48
|
+
namespace Stream {
|
|
49
|
+
/**
|
|
50
|
+
* Options for encrypting a stream.
|
|
51
|
+
*
|
|
52
|
+
*/
|
|
53
|
+
interface EncryptOptions extends Pick<Cph.Options<Cph.CBCTypes>, 'algorithm'> {
|
|
54
|
+
/** The `Readable` Stream from where raw data to encrypt is read. */
|
|
55
|
+
input: Readable;
|
|
56
|
+
/** The `Writable` Stream where encrypted data is written. */
|
|
57
|
+
output: Writable;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Options for decrypting a stream.
|
|
61
|
+
*
|
|
62
|
+
*/
|
|
63
|
+
type DecryptOptions = Cph.Stream.EncryptOptions;
|
|
64
|
+
/**
|
|
65
|
+
* Resolved Stream Encrypt options.
|
|
66
|
+
*
|
|
67
|
+
*/
|
|
68
|
+
type EncryptResolvedOptions = (Cph.ResolvedOptions & Required<Cph.Stream.EncryptOptions>);
|
|
69
|
+
/**
|
|
70
|
+
* Resolved Stream Decrypt options.
|
|
71
|
+
*
|
|
72
|
+
*/
|
|
73
|
+
type DecryptResolvedOptions = (Cph.ResolvedOptions & Required<Cph.Stream.DecryptOptions>);
|
|
74
|
+
/**
|
|
75
|
+
* INTERNAL USE ONLY
|
|
76
|
+
*/
|
|
77
|
+
interface CipherOptions {
|
|
78
|
+
cipher: Cipheriv;
|
|
79
|
+
encryptedKey?: Buffer;
|
|
80
|
+
input: Readable;
|
|
81
|
+
output: Writable;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* INTERNAL USE ONLY
|
|
85
|
+
*/
|
|
86
|
+
interface DecipherOptions {
|
|
87
|
+
decipher: Decipheriv;
|
|
88
|
+
input: Readable;
|
|
89
|
+
output: Writable;
|
|
90
|
+
}
|
|
91
|
+
type ExtractedKeyLength = [KeyLength: number, input: Readable | Transform];
|
|
92
|
+
type ExtractedBytes = [DataRead: Buffer, input: Transform];
|
|
93
|
+
}
|
|
94
|
+
namespace Decrypt {
|
|
95
|
+
interface ExtractKeyOptions {
|
|
96
|
+
privateKey: Cph.PrivateKey;
|
|
97
|
+
EncryptedKeyIV: Buffer;
|
|
98
|
+
keyLength: number;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
declare class Cipher {
|
|
104
|
+
/**
|
|
105
|
+
* Cipher default `salt` lengths.
|
|
106
|
+
*
|
|
107
|
+
*/
|
|
108
|
+
static readonly SALT_LENGTH: {
|
|
109
|
+
readonly min: 16;
|
|
110
|
+
readonly max: 64;
|
|
111
|
+
readonly default: 32;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Cipher default `IV` lengths.
|
|
115
|
+
*
|
|
116
|
+
*/
|
|
117
|
+
static readonly IV_LENGTH: {
|
|
118
|
+
readonly min: 8;
|
|
119
|
+
readonly max: 32;
|
|
120
|
+
readonly default: 16;
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Cipher default `Auth Tag` lengths.
|
|
124
|
+
*
|
|
125
|
+
*/
|
|
126
|
+
static readonly AUTH_TAG_LENGTH: {
|
|
127
|
+
readonly min: 4;
|
|
128
|
+
readonly max: 16;
|
|
129
|
+
readonly default: 16;
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Cipher default `Additional Authenticated Data` lengths.
|
|
133
|
+
*
|
|
134
|
+
*/
|
|
135
|
+
static readonly AAD_LENGTH: {
|
|
136
|
+
readonly min: 16;
|
|
137
|
+
readonly max: 128;
|
|
138
|
+
readonly default: 32;
|
|
139
|
+
};
|
|
140
|
+
static readonly ALGORITHM: {
|
|
141
|
+
readonly AES_128_CBC: "aes-128-cbc";
|
|
142
|
+
readonly AES_192_CBC: "aes-192-cbc";
|
|
143
|
+
readonly AES_256_CBC: "aes-256-cbc";
|
|
144
|
+
readonly AES_128_CCM: "aes-128-ccm";
|
|
145
|
+
readonly AES_192_CCM: "aes-192-ccm";
|
|
146
|
+
readonly AES_256_CCM: "aes-256-ccm";
|
|
147
|
+
readonly AES_128_GCM: "aes-128-gcm";
|
|
148
|
+
readonly AES_192_GCM: "aes-192-gcm";
|
|
149
|
+
readonly AES_256_GCM: "aes-256-gcm";
|
|
150
|
+
readonly AES_128_OCB: "aes-128-ocb";
|
|
151
|
+
readonly AES_192_OCB: "aes-192-ocb";
|
|
152
|
+
readonly AES_256_OCB: "aes-256-ocb";
|
|
153
|
+
readonly CHACHA_20_POLY: "chacha20-poly1305";
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Default AES algorithms used based on functionality.
|
|
157
|
+
*
|
|
158
|
+
*/
|
|
159
|
+
static readonly DEFAULT_ALGORITHM: {
|
|
160
|
+
readonly buffer: "aes-256-gcm";
|
|
161
|
+
readonly stream: "aes-256-cbc";
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* Supported AES algorithms.
|
|
165
|
+
*
|
|
166
|
+
*/
|
|
167
|
+
static readonly ALGORITHMS: Cph.AesAlgorithm[];
|
|
168
|
+
/**
|
|
169
|
+
* Encrypt in-memory data buffer.
|
|
170
|
+
*
|
|
171
|
+
* ⚠️ This is not suitable for large data. Use {@link Cipher.StreamEncrypt()} or {@link Cipher.HybridStreamEncrypt()} methods for large data encryption.
|
|
172
|
+
*
|
|
173
|
+
* @param data The data to encrypt.
|
|
174
|
+
* @param secret The secret key used to encrypt the `data`.
|
|
175
|
+
* @param options ( Optional ) Additional options.
|
|
176
|
+
* @returns The encrypted result Buffer.
|
|
177
|
+
*/
|
|
178
|
+
static Encrypt(data: CoerceToUint8ArrayInput, secret: CoerceToUint8ArrayInput, options?: Cph.Options): Buffer<ArrayBuffer>;
|
|
179
|
+
/**
|
|
180
|
+
* Decrypt in-memory data buffer.
|
|
181
|
+
*
|
|
182
|
+
* ⚠️ This is not suitable for large data. Use {@link Cipher.StreamDecrypt()} or {@link Cipher.HybridStreamDecrypt()} methods for large data decryption.
|
|
183
|
+
*
|
|
184
|
+
* @param data The data to decrypt.
|
|
185
|
+
* @param secret The secret key used to decrypt the `data`.
|
|
186
|
+
* @param options ( Optional ) Additional options. Must be the same used while encrypting data.
|
|
187
|
+
* @returns The decrypted data Buffer.
|
|
188
|
+
*/
|
|
189
|
+
static Decrypt(data: CoerceToUint8ArrayInput, secret: CoerceToUint8ArrayInput, options?: Cph.Options): Buffer<ArrayBuffer>;
|
|
190
|
+
/**
|
|
191
|
+
* Encrypt in-memory data using hybrid encryption.
|
|
192
|
+
*
|
|
193
|
+
* ⚠️ This is not suitable for large data. Use {@link Cipher.HybridStreamEncrypt()} method for large data encryption.
|
|
194
|
+
*
|
|
195
|
+
* @param data The data to encrypt.
|
|
196
|
+
* @param publicKey The public key.
|
|
197
|
+
* @param options ( Optional ) Additional options.
|
|
198
|
+
*
|
|
199
|
+
* @returns The encrypted result Buffer.
|
|
200
|
+
*/
|
|
201
|
+
static HybridEncrypt(data: CoerceToUint8ArrayInput, publicKey: crypto.KeyLike, options?: Cph.Options): Buffer<ArrayBuffer>;
|
|
202
|
+
/**
|
|
203
|
+
* Decrypt in-memory data using hybrid decryption.
|
|
204
|
+
*
|
|
205
|
+
* ⚠️ This is not suitable for large data. Use {@link Cipher.stream.HybridDecrypt()} method for large data encryption.
|
|
206
|
+
*
|
|
207
|
+
* @param data The encrypted data to decrypt.
|
|
208
|
+
* @param privateKey The private key.
|
|
209
|
+
* @param options ( Optional ) Additional options.
|
|
210
|
+
*
|
|
211
|
+
* @returns The encrypted result Buffer.
|
|
212
|
+
*/
|
|
213
|
+
static HybridDecrypt(data: CoerceToUint8ArrayInput, privateKey: Cph.PrivateKey, options?: Cph.Options): Buffer<ArrayBuffer>;
|
|
214
|
+
/**
|
|
215
|
+
* Cipher stream based functions.
|
|
216
|
+
*
|
|
217
|
+
*/
|
|
218
|
+
static stream: {
|
|
219
|
+
/**
|
|
220
|
+
* Encrypt stream data.
|
|
221
|
+
*
|
|
222
|
+
* @param secret The secret key used to encrypt the data.
|
|
223
|
+
* @param options An object defining required options. See {@link Cph.Stream.EncryptOptions} for more info.
|
|
224
|
+
*
|
|
225
|
+
* @returns A new Promise that resolves `void` once stream encryption is completed.
|
|
226
|
+
*/
|
|
227
|
+
Encrypt(secret: CoerceToUint8ArrayInput, options: Cph.Stream.EncryptOptions): Promise<void>;
|
|
228
|
+
/**
|
|
229
|
+
* Decrypt stream data.
|
|
230
|
+
*
|
|
231
|
+
* @param secret The secret key used to decrypt the data.
|
|
232
|
+
* @param options An object defining required options. See {@link Cph.Stream.DecryptOptions} for more info.
|
|
233
|
+
*
|
|
234
|
+
* @returns A new Promise that resolves `void` once stream decryption is completed.
|
|
235
|
+
*/
|
|
236
|
+
Decrypt(secret: CoerceToUint8ArrayInput, options: Cph.Stream.DecryptOptions): Promise<void>;
|
|
237
|
+
/**
|
|
238
|
+
* Encrypt stream data using hybrid encryption.
|
|
239
|
+
*
|
|
240
|
+
* @param publicKey The public key.
|
|
241
|
+
* @param options An object defining required options. See {@link Cph.Stream.EncryptOptions} for more info.
|
|
242
|
+
*
|
|
243
|
+
* @returns A new Promise that resolves `void` once stream encryption is completed.
|
|
244
|
+
*/
|
|
245
|
+
HybridEncrypt(publicKey: crypto.KeyLike, options: Cph.Stream.EncryptOptions): Promise<void>;
|
|
246
|
+
/**
|
|
247
|
+
* Decrypt stream data using hybrid decryption.
|
|
248
|
+
*
|
|
249
|
+
* @param privateKey The private key.
|
|
250
|
+
* @param options An object defining required options. See {@link Cph.Stream.DecryptOptions} for more info.
|
|
251
|
+
*
|
|
252
|
+
* @returns A new Promise that resolves `void` once stream decryption is completed.
|
|
253
|
+
*/
|
|
254
|
+
HybridDecrypt(privateKey: Cph.PrivateKey, options: Cph.Stream.DecryptOptions): Promise<void>;
|
|
255
|
+
/**
|
|
256
|
+
* Handle pipe flow to encrypt a Stream.
|
|
257
|
+
*
|
|
258
|
+
* @param options Required parameters.
|
|
259
|
+
* @returns A new Promise that resolves `void` once stream is completed.
|
|
260
|
+
*/
|
|
261
|
+
Cipher(options: Cph.Stream.CipherOptions): Promise<void>;
|
|
262
|
+
/**
|
|
263
|
+
* Handle pipe flow to decrypt a Stream.
|
|
264
|
+
*
|
|
265
|
+
* @param options Required parameters.
|
|
266
|
+
* @returns A new Promise that resolves `void` once stream is completed.
|
|
267
|
+
*/
|
|
268
|
+
Decipher(options: Cph.Stream.DecipherOptions): Promise<void>;
|
|
269
|
+
};
|
|
270
|
+
private static _stream;
|
|
271
|
+
/**
|
|
272
|
+
* Decrypt Key and Initialization Vector.
|
|
273
|
+
*
|
|
274
|
+
* @param options An object containing required data.
|
|
275
|
+
* @returns An object containing decrpyted `Key` and `IV`.
|
|
276
|
+
*/
|
|
277
|
+
private static DecryptKeyIV;
|
|
278
|
+
private static GetPrivateKey;
|
|
279
|
+
/**
|
|
280
|
+
* Generates a `Scrypt` Symmetric Key and the Initialization Vector with the given options.
|
|
281
|
+
*
|
|
282
|
+
* @param options (Optional) Additional options.
|
|
283
|
+
*
|
|
284
|
+
* @returns An object with the generated `Key`, `IV`, `salt` and resolved `options`.
|
|
285
|
+
*/
|
|
286
|
+
static NewKeyIV<T extends Cph.ResolvedOptions = Cph.ResolvedOptions>(options?: Cph.NewKeyIVOptions): {
|
|
287
|
+
options: T;
|
|
288
|
+
Key: NonSharedBuffer;
|
|
289
|
+
IV: NonSharedBuffer;
|
|
290
|
+
AAD: Uint8Array<ArrayBufferLike> | NonSharedBuffer;
|
|
291
|
+
salt: NonSharedBuffer;
|
|
292
|
+
};
|
|
293
|
+
/**
|
|
294
|
+
* Resolves the given `options` with `Cipher` defaults and constraints.
|
|
295
|
+
*
|
|
296
|
+
* @param options ( Optional ) Additional options.
|
|
297
|
+
* @returns The given `options` with `Cipher` defaults and constraints.
|
|
298
|
+
*/
|
|
299
|
+
private static ResolveOptions;
|
|
300
|
+
/**
|
|
301
|
+
* Get the Initialization Vector length based on the given algorithm.
|
|
302
|
+
*
|
|
303
|
+
* @param algorithm The algorithm in use.
|
|
304
|
+
* @param options ( Optional ) Additional options.
|
|
305
|
+
* @returns The Initialization Vector length based on the given algorithm
|
|
306
|
+
*/
|
|
307
|
+
static GetIVLength(algorithm?: Cph.Options['algorithm'], options?: Cph.Options): number;
|
|
308
|
+
/**
|
|
309
|
+
* Ensure correct key length based on the given algorithm.
|
|
310
|
+
*
|
|
311
|
+
* @param algorithm The AES algorithm name.
|
|
312
|
+
* @returns An object with validated `algorithm` and `keyLength`.
|
|
313
|
+
*/
|
|
314
|
+
static GetKeyLength(algorithm?: Cph.Options['algorithm']): {
|
|
315
|
+
readonly algorithm: "chacha20-poly1305";
|
|
316
|
+
readonly keyLength: number;
|
|
317
|
+
} | {
|
|
318
|
+
readonly algorithm: "aes-128-cbc" | "aes-128-ccm" | "aes-128-gcm" | "aes-128-ocb";
|
|
319
|
+
readonly keyLength: number;
|
|
320
|
+
} | {
|
|
321
|
+
readonly algorithm: "aes-192-cbc" | "aes-192-ccm" | "aes-192-gcm" | "aes-192-ocb";
|
|
322
|
+
readonly keyLength: number;
|
|
323
|
+
} | {
|
|
324
|
+
readonly algorithm: "aes-256-cbc" | "aes-256-ccm" | "aes-256-gcm" | "aes-256-ocb";
|
|
325
|
+
readonly keyLength: number;
|
|
326
|
+
};
|
|
327
|
+
/**
|
|
328
|
+
* Check if the given algorithm is a Cipher AES-GCM algorithm.
|
|
329
|
+
*
|
|
330
|
+
* @param algorithm The AES Algorithm to check.
|
|
331
|
+
* @returns `true` if the given algorithm is a Cipher AES-GCM algorithm. `false` otherwise.
|
|
332
|
+
*/
|
|
333
|
+
static IsGCM(algorithm: Cph.AesAlgorithm): algorithm is crypto.CipherGCMTypes;
|
|
334
|
+
/**
|
|
335
|
+
* Check if the given algorithm is a Cipher AES-CCM algorithm.
|
|
336
|
+
*
|
|
337
|
+
* @param algorithm The AES Algorithm to check.
|
|
338
|
+
* @returns `true` if the given algorithm is a Cipher AES-CCM algorithm. `false` otherwise.
|
|
339
|
+
*/
|
|
340
|
+
static IsCCM(algorithm: Cph.AesAlgorithm): algorithm is crypto.CipherCCMTypes;
|
|
341
|
+
/**
|
|
342
|
+
* Check if the given algorithm is a Cipher chacha20-poly1305 algorithm.
|
|
343
|
+
*
|
|
344
|
+
* @param algorithm The AES Algorithm to check.
|
|
345
|
+
* @returns `true` if the given algorithm is a Cipher chacha20-poly1305 algorithm. `false` otherwise.
|
|
346
|
+
*/
|
|
347
|
+
static IsChacha20Poly(algorithm: Cph.AesAlgorithm): algorithm is crypto.CipherChaCha20Poly1305Types;
|
|
348
|
+
/**
|
|
349
|
+
* Check if the given algorithm is a Cipher AES-OCB algorithm.
|
|
350
|
+
*
|
|
351
|
+
* @param algorithm The AES Algorithm to check.
|
|
352
|
+
* @returns `true` if the given algorithm is a Cipher AES-OCB algorithm. `false` otherwise.
|
|
353
|
+
*/
|
|
354
|
+
static IsOCB(algorithm: Cph.AesAlgorithm): algorithm is crypto.CipherOCBTypes;
|
|
355
|
+
/**
|
|
356
|
+
* Check if the given algorithm is a Cipher AES-CBC algorithm.
|
|
357
|
+
*
|
|
358
|
+
* @param algorithm The AES Algorithm to check.
|
|
359
|
+
* @returns `true` if the given algorithm is a Cipher AES-CBC algorithm. `false` otherwise.
|
|
360
|
+
*/
|
|
361
|
+
static IsCBC(algorithm: Cph.AesAlgorithm): algorithm is Cph.CBCTypes;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
export { Cph as C, Cipher as a };
|