@alessiofrittoli/crypto-cipher 3.1.0 → 3.2.0
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/README.md +221 -188
- package/dist/index-D2HlPPn6.d.cts +361 -0
- package/dist/index-D2HlPPn6.d.mts +361 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.mts +2 -4
- package/dist/index.mjs +1 -1
- package/dist/types/index.d.cts +2 -0
- package/dist/types/index.d.mts +2 -0
- package/dist/types/index.mjs +1 -0
- package/package.json +116 -122
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -1
- package/dist/types.d.mts +0 -364
- package/dist/types.d.ts +0 -364
- package/dist/types.js +0 -1
- /package/dist/{types.mjs → types/index.cjs} +0 -0
package/dist/types.d.ts
DELETED
|
@@ -1,364 +0,0 @@
|
|
|
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
|
-
/** The RSA Private Key. */
|
|
45
|
-
type PrivateKey = KeyLike | {
|
|
46
|
-
key: KeyLike;
|
|
47
|
-
passphrase?: string;
|
|
48
|
-
};
|
|
49
|
-
namespace Stream {
|
|
50
|
-
/**
|
|
51
|
-
* Options for encrypting a stream.
|
|
52
|
-
*
|
|
53
|
-
*/
|
|
54
|
-
interface EncryptOptions extends Pick<Cph.Options<Cph.CBCTypes>, 'algorithm'> {
|
|
55
|
-
/** The `Readable` Stream from where raw data to encrypt is read. */
|
|
56
|
-
input: Readable;
|
|
57
|
-
/** The `Writable` Stream where encrypted data is written. */
|
|
58
|
-
output: Writable;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Options for decrypting a stream.
|
|
62
|
-
*
|
|
63
|
-
*/
|
|
64
|
-
type DecryptOptions = Cph.Stream.EncryptOptions;
|
|
65
|
-
/**
|
|
66
|
-
* Resolved Stream Encrypt options.
|
|
67
|
-
*
|
|
68
|
-
*/
|
|
69
|
-
type EncryptResolvedOptions = (Cph.ResolvedOptions & Required<Cph.Stream.EncryptOptions>);
|
|
70
|
-
/**
|
|
71
|
-
* Resolved Stream Decrypt options.
|
|
72
|
-
*
|
|
73
|
-
*/
|
|
74
|
-
type DecryptResolvedOptions = (Cph.ResolvedOptions & Required<Cph.Stream.DecryptOptions>);
|
|
75
|
-
/**
|
|
76
|
-
* INTERNAL USE ONLY
|
|
77
|
-
*/
|
|
78
|
-
interface CipherOptions {
|
|
79
|
-
cipher: Cipheriv;
|
|
80
|
-
encryptedKey?: Buffer;
|
|
81
|
-
input: Readable;
|
|
82
|
-
output: Writable;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* INTERNAL USE ONLY
|
|
86
|
-
*/
|
|
87
|
-
interface DecipherOptions {
|
|
88
|
-
decipher: Decipheriv;
|
|
89
|
-
input: Readable;
|
|
90
|
-
output: Writable;
|
|
91
|
-
}
|
|
92
|
-
type ExtractedKeyLength = [KeyLength: number, input: Readable | Transform];
|
|
93
|
-
type ExtractedBytes = [DataRead: Buffer, input: Transform];
|
|
94
|
-
}
|
|
95
|
-
namespace Decrypt {
|
|
96
|
-
interface ExtractKeyOptions {
|
|
97
|
-
privateKey: Cph.PrivateKey;
|
|
98
|
-
EncryptedKeyIV: Buffer;
|
|
99
|
-
keyLength: number;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
declare class Cipher {
|
|
105
|
-
/**
|
|
106
|
-
* Cipher default `salt` lengths.
|
|
107
|
-
*
|
|
108
|
-
*/
|
|
109
|
-
static readonly SALT_LENGTH: {
|
|
110
|
-
readonly min: 16;
|
|
111
|
-
readonly max: 64;
|
|
112
|
-
readonly default: 32;
|
|
113
|
-
};
|
|
114
|
-
/**
|
|
115
|
-
* Cipher default `IV` lengths.
|
|
116
|
-
*
|
|
117
|
-
*/
|
|
118
|
-
static readonly IV_LENGTH: {
|
|
119
|
-
readonly min: 8;
|
|
120
|
-
readonly max: 32;
|
|
121
|
-
readonly default: 16;
|
|
122
|
-
};
|
|
123
|
-
/**
|
|
124
|
-
* Cipher default `Auth Tag` lengths.
|
|
125
|
-
*
|
|
126
|
-
*/
|
|
127
|
-
static readonly AUTH_TAG_LENGTH: {
|
|
128
|
-
readonly min: 4;
|
|
129
|
-
readonly max: 16;
|
|
130
|
-
readonly default: 16;
|
|
131
|
-
};
|
|
132
|
-
/**
|
|
133
|
-
* Cipher default `Additional Authenticated Data` lengths.
|
|
134
|
-
*
|
|
135
|
-
*/
|
|
136
|
-
static readonly AAD_LENGTH: {
|
|
137
|
-
readonly min: 16;
|
|
138
|
-
readonly max: 128;
|
|
139
|
-
readonly default: 32;
|
|
140
|
-
};
|
|
141
|
-
/**
|
|
142
|
-
* Cipher algorithm name.
|
|
143
|
-
*
|
|
144
|
-
*/
|
|
145
|
-
static readonly ALGORITHM: {
|
|
146
|
-
/** AES 128 bit Cipher Block Chaining. */
|
|
147
|
-
readonly AES_128_CBC: "aes-128-cbc";
|
|
148
|
-
/** AES 192 bit Cipher Block Chaining. */
|
|
149
|
-
readonly AES_192_CBC: "aes-192-cbc";
|
|
150
|
-
/** AES 256 bit Cipher Block Chaining. */
|
|
151
|
-
readonly AES_256_CBC: "aes-256-cbc";
|
|
152
|
-
/** AES 128 bit AEAD Counter with CBC-MAC. */
|
|
153
|
-
readonly AES_128_CCM: "aes-128-ccm";
|
|
154
|
-
/** AES 192 bit AEAD Counter with CBC-MAC. */
|
|
155
|
-
readonly AES_192_CCM: "aes-192-ccm";
|
|
156
|
-
/** AES 256 bit AEAD Counter with CBC-MAC. */
|
|
157
|
-
readonly AES_256_CCM: "aes-256-ccm";
|
|
158
|
-
/** AES 128 bit AEAD Galois/Counter Mode. */
|
|
159
|
-
readonly AES_128_GCM: "aes-128-gcm";
|
|
160
|
-
/** AES 192 bit AEAD Galois/Counter Mode. */
|
|
161
|
-
readonly AES_192_GCM: "aes-192-gcm";
|
|
162
|
-
/** AES 256 bit AEAD Galois/Counter Mode. */
|
|
163
|
-
readonly AES_256_GCM: "aes-256-gcm";
|
|
164
|
-
/** AES 128 bit AEAD Offset Codebook Mode. */
|
|
165
|
-
readonly AES_128_OCB: "aes-128-ocb";
|
|
166
|
-
/** AES 192 bit AEAD Offset Codebook Mode. */
|
|
167
|
-
readonly AES_192_OCB: "aes-192-ocb";
|
|
168
|
-
/** AES 256 bit AEAD Offset Codebook Mode. */
|
|
169
|
-
readonly AES_256_OCB: "aes-256-ocb";
|
|
170
|
-
/** AES 256 bit AEAD. */
|
|
171
|
-
readonly CHACHA_20_POLY: "chacha20-poly1305";
|
|
172
|
-
};
|
|
173
|
-
/**
|
|
174
|
-
* Default AES algorithms used based on functionality.
|
|
175
|
-
*
|
|
176
|
-
*/
|
|
177
|
-
static readonly DEFAULT_ALGORITHM: {
|
|
178
|
-
readonly buffer: "aes-256-gcm";
|
|
179
|
-
readonly stream: "aes-256-cbc";
|
|
180
|
-
};
|
|
181
|
-
/**
|
|
182
|
-
* Supported AES algorithms.
|
|
183
|
-
*
|
|
184
|
-
*/
|
|
185
|
-
static readonly ALGORITHMS: Cph.AesAlgorithm[];
|
|
186
|
-
/**
|
|
187
|
-
* Encrypt in-memory data buffer.
|
|
188
|
-
*
|
|
189
|
-
* ⚠️ This is not suitable for large data encryption. Use {@link Cipher.stream.Encrypt()} or {@link Cipher.stream.HybridEncrypt()} methods for large data encryption.
|
|
190
|
-
*
|
|
191
|
-
* @param data The data to encrypt.
|
|
192
|
-
* @param secret The secret key used to encrypt the `data`.
|
|
193
|
-
* @param options (Optional) Additional options.
|
|
194
|
-
* @returns The encrypted result Buffer.
|
|
195
|
-
*/
|
|
196
|
-
static Encrypt(data: CoerceToUint8ArrayInput, secret: CoerceToUint8ArrayInput, options?: Cph.Options): Buffer<ArrayBuffer>;
|
|
197
|
-
/**
|
|
198
|
-
* Decrypt in-memory data buffer.
|
|
199
|
-
*
|
|
200
|
-
* ⚠️ This is not suitable for large data decryption. Use {@link Cipher.stream.Decrypt()} or {@link Cipher.stream.HybridDecrypt()} methods for large data decryption.
|
|
201
|
-
*
|
|
202
|
-
* @param data The data to decrypt.
|
|
203
|
-
* @param secret The secret key used to decrypt the `data`.
|
|
204
|
-
* @param options (Optional) Additional options. Must be the same used while encrypting data.
|
|
205
|
-
* @returns The decrypted data Buffer.
|
|
206
|
-
*/
|
|
207
|
-
static Decrypt(data: CoerceToUint8ArrayInput, secret: CoerceToUint8ArrayInput, options?: Cph.Options): Buffer<ArrayBuffer>;
|
|
208
|
-
/**
|
|
209
|
-
* Encrypt in-memory data using hybrid encryption.
|
|
210
|
-
*
|
|
211
|
-
* ⚠️ This is not suitable for large data encryption. Use {@link Cipher.stream.HybridEncrypt()} method for large data encryption.
|
|
212
|
-
*
|
|
213
|
-
* @param data The data to encrypt.
|
|
214
|
-
* @param key The RSA Public Key.
|
|
215
|
-
* @param options (Optional) Additional options.
|
|
216
|
-
*
|
|
217
|
-
* @returns The encrypted result Buffer.
|
|
218
|
-
*/
|
|
219
|
-
static HybridEncrypt(data: CoerceToUint8ArrayInput, key: crypto.KeyLike, options?: Cph.Options): Buffer<ArrayBuffer>;
|
|
220
|
-
/**
|
|
221
|
-
* Decrypt in-memory data using hybrid decryption.
|
|
222
|
-
*
|
|
223
|
-
* ⚠️ This is not suitable for large data decryption. Use {@link Cipher.stream.HybridDecrypt()} method for large data decryption.
|
|
224
|
-
*
|
|
225
|
-
* @param data The encrypted data to decrypt.
|
|
226
|
-
* @param key The RSA Private Key.
|
|
227
|
-
* @param options (Optional) Additional options.
|
|
228
|
-
*
|
|
229
|
-
* @returns The encrypted result Buffer.
|
|
230
|
-
*/
|
|
231
|
-
static HybridDecrypt(data: CoerceToUint8ArrayInput, key: Cph.PrivateKey, options?: Cph.Options): Buffer<ArrayBuffer>;
|
|
232
|
-
/**
|
|
233
|
-
* Cipher stream based functions.
|
|
234
|
-
*
|
|
235
|
-
*/
|
|
236
|
-
static stream: {
|
|
237
|
-
/**
|
|
238
|
-
* Encrypt stream data.
|
|
239
|
-
*
|
|
240
|
-
* @param secret The secret key used to encrypt the data.
|
|
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
|
-
Encrypt(secret: CoerceToUint8ArrayInput, options: Cph.Stream.EncryptOptions): Promise<void>;
|
|
246
|
-
/**
|
|
247
|
-
* Decrypt stream data.
|
|
248
|
-
*
|
|
249
|
-
* @param secret The secret key used to decrypt the data.
|
|
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
|
-
Decrypt(secret: CoerceToUint8ArrayInput, options: Cph.Stream.DecryptOptions): Promise<void>;
|
|
255
|
-
/**
|
|
256
|
-
* Encrypt stream data using hybrid encryption.
|
|
257
|
-
*
|
|
258
|
-
* @param key The RSA Public Key.
|
|
259
|
-
* @param options An object defining required options. See {@link Cph.Stream.EncryptOptions} for more info.
|
|
260
|
-
*
|
|
261
|
-
* @returns A new Promise that resolves `void` once stream encryption is completed.
|
|
262
|
-
*/
|
|
263
|
-
HybridEncrypt(key: crypto.KeyLike, options: Cph.Stream.EncryptOptions): Promise<void>;
|
|
264
|
-
/**
|
|
265
|
-
* Decrypt stream data using hybrid decryption.
|
|
266
|
-
*
|
|
267
|
-
* @param key The private key.
|
|
268
|
-
* @param options An object defining required options. See {@link Cph.Stream.DecryptOptions} for more info.
|
|
269
|
-
*
|
|
270
|
-
* @returns A new Promise that resolves `void` once stream decryption is completed.
|
|
271
|
-
*/
|
|
272
|
-
HybridDecrypt(key: Cph.PrivateKey, options: Cph.Stream.DecryptOptions): Promise<void>;
|
|
273
|
-
/**
|
|
274
|
-
* Handle pipe flow to encrypt a Stream.
|
|
275
|
-
*
|
|
276
|
-
* @param options Required parameters.
|
|
277
|
-
* @returns A new Promise that resolves `void` once stream is completed.
|
|
278
|
-
*/
|
|
279
|
-
Cipher(options: Cph.Stream.CipherOptions): Promise<void>;
|
|
280
|
-
/**
|
|
281
|
-
* Handle pipe flow to decrypt a Stream.
|
|
282
|
-
*
|
|
283
|
-
* @param options Required parameters.
|
|
284
|
-
* @returns A new Promise that resolves `void` once stream is completed.
|
|
285
|
-
*/
|
|
286
|
-
Decipher(options: Cph.Stream.DecipherOptions): Promise<void>;
|
|
287
|
-
};
|
|
288
|
-
private static _stream;
|
|
289
|
-
/**
|
|
290
|
-
* Decrypt Key and Initialization Vector.
|
|
291
|
-
*
|
|
292
|
-
* @param options An object containing required data.
|
|
293
|
-
* @returns An object containing decrpyted `Key` and `IV`.
|
|
294
|
-
*/
|
|
295
|
-
private static DecryptKeyIV;
|
|
296
|
-
private static GetPrivateKey;
|
|
297
|
-
/**
|
|
298
|
-
* Generates a `Scrypt` Symmetric Key and the Initialization Vector with the given options.
|
|
299
|
-
*
|
|
300
|
-
* @param options (Optional) Additional options.
|
|
301
|
-
*
|
|
302
|
-
* @returns An object with the generated `Key`, `IV`, `salt` and resolved `options`.
|
|
303
|
-
*/
|
|
304
|
-
private static NewKeyIV;
|
|
305
|
-
/**
|
|
306
|
-
* Resolves the given `options` with `Cipher` defaults and constraints.
|
|
307
|
-
*
|
|
308
|
-
* @param options (Optional) Additional options.
|
|
309
|
-
* @returns The given `options` with `Cipher` defaults and constraints.
|
|
310
|
-
*/
|
|
311
|
-
private static ResolveOptions;
|
|
312
|
-
/**
|
|
313
|
-
* Get the Initialization Vector length based on the given algorithm.
|
|
314
|
-
*
|
|
315
|
-
* @param algorithm The algorithm in use.
|
|
316
|
-
* @param options (Optional) Additional options.
|
|
317
|
-
* @returns The Initialization Vector length based on the given algorithm
|
|
318
|
-
*/
|
|
319
|
-
private static GetIVLength;
|
|
320
|
-
/**
|
|
321
|
-
* Ensure correct key length based on the given algorithm.
|
|
322
|
-
*
|
|
323
|
-
* @param algorithm The AES algorithm name.
|
|
324
|
-
* @returns An object with validated `algorithm` and `keyLength`.
|
|
325
|
-
*/
|
|
326
|
-
private static GetKeyLength;
|
|
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 { Cipher as C, Cph };
|
package/dist/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
File without changes
|