@alessiofrittoli/crypto-cipher 1.0.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 +995 -0
- package/dist/index.d.mts +206 -0
- package/dist/index.d.ts +206 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/dist/types.d.mts +89 -0
- package/dist/types.d.ts +89 -0
- package/dist/types.js +1 -0
- package/dist/types.mjs +0 -0
- package/license.md +21 -0
- package/package.json +91 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
import { CoerceToUint8ArrayInput } from '@alessiofrittoli/crypto-buffer/coercion';
|
|
3
|
+
import { Cph } from './types.mjs';
|
|
4
|
+
import '@alessiofrittoli/crypto-buffer';
|
|
5
|
+
import 'stream';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Utility class for AES encryption and decryption following [NIST SP 800-38D](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf) standard reccomendations.
|
|
9
|
+
*/
|
|
10
|
+
declare class Cipher {
|
|
11
|
+
/**
|
|
12
|
+
* Cipher default `salt` lengths.
|
|
13
|
+
*/
|
|
14
|
+
static readonly SALT_LENGTH: {
|
|
15
|
+
readonly min: 16;
|
|
16
|
+
readonly max: 64;
|
|
17
|
+
readonly default: 32;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Cipher default `IV` lengths.
|
|
21
|
+
*/
|
|
22
|
+
static readonly IV_LENGTH: {
|
|
23
|
+
readonly min: 8;
|
|
24
|
+
readonly max: 32;
|
|
25
|
+
readonly default: 16;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Cipher default `Auth Tag` lengths.
|
|
29
|
+
*/
|
|
30
|
+
static readonly AUTH_TAG_LENGTH: {
|
|
31
|
+
readonly min: 4;
|
|
32
|
+
readonly max: 16;
|
|
33
|
+
readonly default: 16;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Cipher default `Additional Authenticated Data` lengths.
|
|
37
|
+
*/
|
|
38
|
+
static readonly AAD_LENGTH: {
|
|
39
|
+
readonly min: 16;
|
|
40
|
+
readonly max: 128;
|
|
41
|
+
readonly default: 32;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Default AES algorithms used based on functionality.
|
|
45
|
+
*/
|
|
46
|
+
static readonly DEFAULT_ALGORITHM: {
|
|
47
|
+
readonly buffer: "aes-256-gcm";
|
|
48
|
+
readonly stream: "aes-256-cbc";
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Supported AES algorithms.
|
|
52
|
+
*/
|
|
53
|
+
static readonly ALGORITHMS: Cph.AesAlgorithm[];
|
|
54
|
+
/**
|
|
55
|
+
* Encrypt in-memory data buffer.
|
|
56
|
+
*
|
|
57
|
+
* ⚠️ This is not suitable for large data. Use {@link Cipher.streamEncrypt()} or {@link Cipher.hybridEncrypt()} methods for large data encryption.
|
|
58
|
+
*
|
|
59
|
+
* @param data The data to encrypt.
|
|
60
|
+
* @param secret The secret key used to encrypt the `data`.
|
|
61
|
+
* @param options ( Optional ) Additional options.
|
|
62
|
+
* @returns The encrypted result Buffer.
|
|
63
|
+
*/
|
|
64
|
+
static encrypt(data: CoerceToUint8ArrayInput, secret: CoerceToUint8ArrayInput, options?: Cph.Options): Buffer;
|
|
65
|
+
/**
|
|
66
|
+
* Decrypt in-memory data buffer.
|
|
67
|
+
*
|
|
68
|
+
* ⚠️ This is not suitable for large data. Use {@link Cipher.streamDecrypt()} or {@link Cipher.hybridDecrypt()} methods for large data decryption.
|
|
69
|
+
*
|
|
70
|
+
* @param data The data to decrypt.
|
|
71
|
+
* @param secret The secret key used to decrypt the `data`.
|
|
72
|
+
* @param options ( Optional ) Additional options. Must be the same used while encrypting data.
|
|
73
|
+
* @returns The decrypted data Buffer.
|
|
74
|
+
*/
|
|
75
|
+
static decrypt(data: CoerceToUint8ArrayInput, secret: CoerceToUint8ArrayInput, options?: Cph.Options): Buffer;
|
|
76
|
+
/**
|
|
77
|
+
* Encrypt a `Readable` to a `Writable` Stream.
|
|
78
|
+
*
|
|
79
|
+
* The `Readable` Stream could be 'in-memory buffer' or 'file' based.
|
|
80
|
+
*
|
|
81
|
+
* @param secret The secret key used to encrypt the `data`.
|
|
82
|
+
* @param options Additional options.
|
|
83
|
+
* @returns A new Promise that resolves `void` once stream is completed.
|
|
84
|
+
*/
|
|
85
|
+
static streamEncrypt(secret: CoerceToUint8ArrayInput, options: Cph.Stream.Symmetric.EncryptOptions): Promise<void>;
|
|
86
|
+
/**
|
|
87
|
+
* Decrypt a `Readable` to a `Writable` Stream.
|
|
88
|
+
*
|
|
89
|
+
* The `Readable` Stream could be 'in-memory buffer' or 'file' based.
|
|
90
|
+
*
|
|
91
|
+
* @param secret The secret key used to encrypt the `data`.
|
|
92
|
+
* @param options Additional options.
|
|
93
|
+
* @returns A new Promise that resolves `void` once stream is completed.
|
|
94
|
+
*/
|
|
95
|
+
static streamDecrypt(secret: CoerceToUint8ArrayInput, options: Cph.Stream.Symmetric.DecryptOptions): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* Encrypt a `Readable` to a `Writable` Stream with hybird Encryption.
|
|
98
|
+
*
|
|
99
|
+
* The `Readable` Stream could be 'in-memory buffer' or 'file' based.
|
|
100
|
+
*
|
|
101
|
+
* @param secret The secret key used to encrypt the stream.
|
|
102
|
+
* @param publicKey The RSA public key used to encrypt the symmetric key.
|
|
103
|
+
* @param options Options for the stream encryption.
|
|
104
|
+
* @returns A new Promise that resolves `void` once stream is completed.
|
|
105
|
+
*/
|
|
106
|
+
static hybridEncrypt(secret: CoerceToUint8ArrayInput, publicKey: crypto.RsaPublicKey | crypto.RsaPrivateKey | crypto.KeyLike, options: Cph.Stream.Hybrid.EncryptOptions): Promise<void>;
|
|
107
|
+
/**
|
|
108
|
+
* Decrypt a `Readable` to a `Writable` Stream with hybrid Decryption.
|
|
109
|
+
*
|
|
110
|
+
* The `Readable` Stream could be 'in-memory buffer' or 'file' based.
|
|
111
|
+
*
|
|
112
|
+
* @param privateKey The RSA private key used to decrypt the symmetric key.
|
|
113
|
+
* @param options Options for the stream decryption.
|
|
114
|
+
* @returns A new Promise that resolves `void` once stream is completed.
|
|
115
|
+
*/
|
|
116
|
+
static hybridDecrypt(privateKey: crypto.RsaPrivateKey | crypto.KeyLike, options: Cph.Stream.Hybrid.DecryptOptions): Promise<void>;
|
|
117
|
+
/**
|
|
118
|
+
* Handle pipe flow to encrypt a Stream.
|
|
119
|
+
*
|
|
120
|
+
* @param options Required parameters.
|
|
121
|
+
* @returns A new Promise that resolves `void` once stream is completed.
|
|
122
|
+
*/
|
|
123
|
+
private static stream;
|
|
124
|
+
/**
|
|
125
|
+
* Handle pipe flow to decrypt a Stream.
|
|
126
|
+
*
|
|
127
|
+
* @param options Required parameters.
|
|
128
|
+
* @returns A new Promise that resolves `void` once stream is completed.
|
|
129
|
+
*/
|
|
130
|
+
private static decipherStream;
|
|
131
|
+
/**
|
|
132
|
+
* Extract the Cipher Encrypted Symmetric Key and Initialization Vector from an encrypted `Readable` Stream.
|
|
133
|
+
*
|
|
134
|
+
* @param input The `Readable` Stream.
|
|
135
|
+
* @param keyLength The encrypted key length in bytes. This is used to properly extract the encrypted Cipher Key and Initialization Vector.
|
|
136
|
+
* @returns A new Promise that resolve a tuple containing the Cipher Encrypted Symmetric Key and Initialization Vector once fulfilled.
|
|
137
|
+
*/
|
|
138
|
+
private static extractKeyIV;
|
|
139
|
+
/**
|
|
140
|
+
* Generates a `Scrypt` Symmetric Key and the Initialization Vector with the given options.
|
|
141
|
+
*
|
|
142
|
+
* @param secret The secret key.
|
|
143
|
+
* @param options ( Optional ) Additional options.
|
|
144
|
+
*
|
|
145
|
+
* @returns An object with the generated `Key`, `IV`, `salt` and resolved `options`.
|
|
146
|
+
*/
|
|
147
|
+
static newKeyIV<T extends Cph.ResolvedOptions = Cph.ResolvedOptions>(secret: CoerceToUint8ArrayInput, options?: Cph.Options): {
|
|
148
|
+
options: T;
|
|
149
|
+
Key: Buffer<ArrayBufferLike>;
|
|
150
|
+
IV: Buffer<ArrayBufferLike>;
|
|
151
|
+
AAD: Uint8Array<ArrayBufferLike> | Buffer<ArrayBufferLike>;
|
|
152
|
+
salt: Buffer<ArrayBufferLike>;
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* Resolves the given `options` with `Cipher` defaults and constraints.
|
|
156
|
+
*
|
|
157
|
+
* @param options ( Optional ) Additional options.
|
|
158
|
+
* @returns The given `options` with `Cipher` defaults and constraints.
|
|
159
|
+
*/
|
|
160
|
+
static resolveOptions<T extends Cph.ResolvedOptions = Cph.ResolvedOptions>(options?: Cph.Options): T;
|
|
161
|
+
/**
|
|
162
|
+
* Get the Initialization Vector length based on the given algorithm.
|
|
163
|
+
*
|
|
164
|
+
* @param algorithm The algorithm in use.
|
|
165
|
+
* @param options ( Optional ) Additional options.
|
|
166
|
+
* @returns The Initialization Vector length based on the given algorithm
|
|
167
|
+
*/
|
|
168
|
+
private static getIVLength;
|
|
169
|
+
/**
|
|
170
|
+
* Ensure correct key length based on the given algorithm.
|
|
171
|
+
*
|
|
172
|
+
* @param algorithm The AES algorithm name.
|
|
173
|
+
* @returns An object with validated `algorithm` and `keyLength`.
|
|
174
|
+
*/
|
|
175
|
+
private static getKeyLength;
|
|
176
|
+
/**
|
|
177
|
+
* Check if the given algorithm is a Cipher AES-GCM algorithm.
|
|
178
|
+
*
|
|
179
|
+
* @param algorithm The AES Algorithm to check.
|
|
180
|
+
* @returns `true` if the given algorithm is a Cipher AES-GCM algorithm. `false` otherwise.
|
|
181
|
+
*/
|
|
182
|
+
static isGCM(algorithm: Cph.AesAlgorithm): algorithm is crypto.CipherGCMTypes;
|
|
183
|
+
/**
|
|
184
|
+
* Check if the given algorithm is a Cipher AES-CCM algorithm.
|
|
185
|
+
*
|
|
186
|
+
* @param algorithm The AES Algorithm to check.
|
|
187
|
+
* @returns `true` if the given algorithm is a Cipher AES-CCM algorithm. `false` otherwise.
|
|
188
|
+
*/
|
|
189
|
+
static isCCM(algorithm: Cph.AesAlgorithm): algorithm is crypto.CipherCCMTypes;
|
|
190
|
+
/**
|
|
191
|
+
* Check if the given algorithm is a Cipher AES-OCB algorithm.
|
|
192
|
+
*
|
|
193
|
+
* @param algorithm The AES Algorithm to check.
|
|
194
|
+
* @returns `true` if the given algorithm is a Cipher AES-OCB algorithm. `false` otherwise.
|
|
195
|
+
*/
|
|
196
|
+
static isOCB(algorithm: Cph.AesAlgorithm): algorithm is crypto.CipherOCBTypes;
|
|
197
|
+
/**
|
|
198
|
+
* Check if the given algorithm is a Cipher AES-CBC algorithm.
|
|
199
|
+
*
|
|
200
|
+
* @param algorithm The AES Algorithm to check.
|
|
201
|
+
* @returns `true` if the given algorithm is a Cipher AES-CBC algorithm. `false` otherwise.
|
|
202
|
+
*/
|
|
203
|
+
static isCBC(algorithm: Cph.AesAlgorithm): algorithm is Cph.CBCTypes;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export { Cipher };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
import { CoerceToUint8ArrayInput } from '@alessiofrittoli/crypto-buffer/coercion';
|
|
3
|
+
import { Cph } from './types.js';
|
|
4
|
+
import '@alessiofrittoli/crypto-buffer';
|
|
5
|
+
import 'stream';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Utility class for AES encryption and decryption following [NIST SP 800-38D](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf) standard reccomendations.
|
|
9
|
+
*/
|
|
10
|
+
declare class Cipher {
|
|
11
|
+
/**
|
|
12
|
+
* Cipher default `salt` lengths.
|
|
13
|
+
*/
|
|
14
|
+
static readonly SALT_LENGTH: {
|
|
15
|
+
readonly min: 16;
|
|
16
|
+
readonly max: 64;
|
|
17
|
+
readonly default: 32;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Cipher default `IV` lengths.
|
|
21
|
+
*/
|
|
22
|
+
static readonly IV_LENGTH: {
|
|
23
|
+
readonly min: 8;
|
|
24
|
+
readonly max: 32;
|
|
25
|
+
readonly default: 16;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Cipher default `Auth Tag` lengths.
|
|
29
|
+
*/
|
|
30
|
+
static readonly AUTH_TAG_LENGTH: {
|
|
31
|
+
readonly min: 4;
|
|
32
|
+
readonly max: 16;
|
|
33
|
+
readonly default: 16;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Cipher default `Additional Authenticated Data` lengths.
|
|
37
|
+
*/
|
|
38
|
+
static readonly AAD_LENGTH: {
|
|
39
|
+
readonly min: 16;
|
|
40
|
+
readonly max: 128;
|
|
41
|
+
readonly default: 32;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Default AES algorithms used based on functionality.
|
|
45
|
+
*/
|
|
46
|
+
static readonly DEFAULT_ALGORITHM: {
|
|
47
|
+
readonly buffer: "aes-256-gcm";
|
|
48
|
+
readonly stream: "aes-256-cbc";
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Supported AES algorithms.
|
|
52
|
+
*/
|
|
53
|
+
static readonly ALGORITHMS: Cph.AesAlgorithm[];
|
|
54
|
+
/**
|
|
55
|
+
* Encrypt in-memory data buffer.
|
|
56
|
+
*
|
|
57
|
+
* ⚠️ This is not suitable for large data. Use {@link Cipher.streamEncrypt()} or {@link Cipher.hybridEncrypt()} methods for large data encryption.
|
|
58
|
+
*
|
|
59
|
+
* @param data The data to encrypt.
|
|
60
|
+
* @param secret The secret key used to encrypt the `data`.
|
|
61
|
+
* @param options ( Optional ) Additional options.
|
|
62
|
+
* @returns The encrypted result Buffer.
|
|
63
|
+
*/
|
|
64
|
+
static encrypt(data: CoerceToUint8ArrayInput, secret: CoerceToUint8ArrayInput, options?: Cph.Options): Buffer;
|
|
65
|
+
/**
|
|
66
|
+
* Decrypt in-memory data buffer.
|
|
67
|
+
*
|
|
68
|
+
* ⚠️ This is not suitable for large data. Use {@link Cipher.streamDecrypt()} or {@link Cipher.hybridDecrypt()} methods for large data decryption.
|
|
69
|
+
*
|
|
70
|
+
* @param data The data to decrypt.
|
|
71
|
+
* @param secret The secret key used to decrypt the `data`.
|
|
72
|
+
* @param options ( Optional ) Additional options. Must be the same used while encrypting data.
|
|
73
|
+
* @returns The decrypted data Buffer.
|
|
74
|
+
*/
|
|
75
|
+
static decrypt(data: CoerceToUint8ArrayInput, secret: CoerceToUint8ArrayInput, options?: Cph.Options): Buffer;
|
|
76
|
+
/**
|
|
77
|
+
* Encrypt a `Readable` to a `Writable` Stream.
|
|
78
|
+
*
|
|
79
|
+
* The `Readable` Stream could be 'in-memory buffer' or 'file' based.
|
|
80
|
+
*
|
|
81
|
+
* @param secret The secret key used to encrypt the `data`.
|
|
82
|
+
* @param options Additional options.
|
|
83
|
+
* @returns A new Promise that resolves `void` once stream is completed.
|
|
84
|
+
*/
|
|
85
|
+
static streamEncrypt(secret: CoerceToUint8ArrayInput, options: Cph.Stream.Symmetric.EncryptOptions): Promise<void>;
|
|
86
|
+
/**
|
|
87
|
+
* Decrypt a `Readable` to a `Writable` Stream.
|
|
88
|
+
*
|
|
89
|
+
* The `Readable` Stream could be 'in-memory buffer' or 'file' based.
|
|
90
|
+
*
|
|
91
|
+
* @param secret The secret key used to encrypt the `data`.
|
|
92
|
+
* @param options Additional options.
|
|
93
|
+
* @returns A new Promise that resolves `void` once stream is completed.
|
|
94
|
+
*/
|
|
95
|
+
static streamDecrypt(secret: CoerceToUint8ArrayInput, options: Cph.Stream.Symmetric.DecryptOptions): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* Encrypt a `Readable` to a `Writable` Stream with hybird Encryption.
|
|
98
|
+
*
|
|
99
|
+
* The `Readable` Stream could be 'in-memory buffer' or 'file' based.
|
|
100
|
+
*
|
|
101
|
+
* @param secret The secret key used to encrypt the stream.
|
|
102
|
+
* @param publicKey The RSA public key used to encrypt the symmetric key.
|
|
103
|
+
* @param options Options for the stream encryption.
|
|
104
|
+
* @returns A new Promise that resolves `void` once stream is completed.
|
|
105
|
+
*/
|
|
106
|
+
static hybridEncrypt(secret: CoerceToUint8ArrayInput, publicKey: crypto.RsaPublicKey | crypto.RsaPrivateKey | crypto.KeyLike, options: Cph.Stream.Hybrid.EncryptOptions): Promise<void>;
|
|
107
|
+
/**
|
|
108
|
+
* Decrypt a `Readable` to a `Writable` Stream with hybrid Decryption.
|
|
109
|
+
*
|
|
110
|
+
* The `Readable` Stream could be 'in-memory buffer' or 'file' based.
|
|
111
|
+
*
|
|
112
|
+
* @param privateKey The RSA private key used to decrypt the symmetric key.
|
|
113
|
+
* @param options Options for the stream decryption.
|
|
114
|
+
* @returns A new Promise that resolves `void` once stream is completed.
|
|
115
|
+
*/
|
|
116
|
+
static hybridDecrypt(privateKey: crypto.RsaPrivateKey | crypto.KeyLike, options: Cph.Stream.Hybrid.DecryptOptions): Promise<void>;
|
|
117
|
+
/**
|
|
118
|
+
* Handle pipe flow to encrypt a Stream.
|
|
119
|
+
*
|
|
120
|
+
* @param options Required parameters.
|
|
121
|
+
* @returns A new Promise that resolves `void` once stream is completed.
|
|
122
|
+
*/
|
|
123
|
+
private static stream;
|
|
124
|
+
/**
|
|
125
|
+
* Handle pipe flow to decrypt a Stream.
|
|
126
|
+
*
|
|
127
|
+
* @param options Required parameters.
|
|
128
|
+
* @returns A new Promise that resolves `void` once stream is completed.
|
|
129
|
+
*/
|
|
130
|
+
private static decipherStream;
|
|
131
|
+
/**
|
|
132
|
+
* Extract the Cipher Encrypted Symmetric Key and Initialization Vector from an encrypted `Readable` Stream.
|
|
133
|
+
*
|
|
134
|
+
* @param input The `Readable` Stream.
|
|
135
|
+
* @param keyLength The encrypted key length in bytes. This is used to properly extract the encrypted Cipher Key and Initialization Vector.
|
|
136
|
+
* @returns A new Promise that resolve a tuple containing the Cipher Encrypted Symmetric Key and Initialization Vector once fulfilled.
|
|
137
|
+
*/
|
|
138
|
+
private static extractKeyIV;
|
|
139
|
+
/**
|
|
140
|
+
* Generates a `Scrypt` Symmetric Key and the Initialization Vector with the given options.
|
|
141
|
+
*
|
|
142
|
+
* @param secret The secret key.
|
|
143
|
+
* @param options ( Optional ) Additional options.
|
|
144
|
+
*
|
|
145
|
+
* @returns An object with the generated `Key`, `IV`, `salt` and resolved `options`.
|
|
146
|
+
*/
|
|
147
|
+
static newKeyIV<T extends Cph.ResolvedOptions = Cph.ResolvedOptions>(secret: CoerceToUint8ArrayInput, options?: Cph.Options): {
|
|
148
|
+
options: T;
|
|
149
|
+
Key: Buffer<ArrayBufferLike>;
|
|
150
|
+
IV: Buffer<ArrayBufferLike>;
|
|
151
|
+
AAD: Uint8Array<ArrayBufferLike> | Buffer<ArrayBufferLike>;
|
|
152
|
+
salt: Buffer<ArrayBufferLike>;
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* Resolves the given `options` with `Cipher` defaults and constraints.
|
|
156
|
+
*
|
|
157
|
+
* @param options ( Optional ) Additional options.
|
|
158
|
+
* @returns The given `options` with `Cipher` defaults and constraints.
|
|
159
|
+
*/
|
|
160
|
+
static resolveOptions<T extends Cph.ResolvedOptions = Cph.ResolvedOptions>(options?: Cph.Options): T;
|
|
161
|
+
/**
|
|
162
|
+
* Get the Initialization Vector length based on the given algorithm.
|
|
163
|
+
*
|
|
164
|
+
* @param algorithm The algorithm in use.
|
|
165
|
+
* @param options ( Optional ) Additional options.
|
|
166
|
+
* @returns The Initialization Vector length based on the given algorithm
|
|
167
|
+
*/
|
|
168
|
+
private static getIVLength;
|
|
169
|
+
/**
|
|
170
|
+
* Ensure correct key length based on the given algorithm.
|
|
171
|
+
*
|
|
172
|
+
* @param algorithm The AES algorithm name.
|
|
173
|
+
* @returns An object with validated `algorithm` and `keyLength`.
|
|
174
|
+
*/
|
|
175
|
+
private static getKeyLength;
|
|
176
|
+
/**
|
|
177
|
+
* Check if the given algorithm is a Cipher AES-GCM algorithm.
|
|
178
|
+
*
|
|
179
|
+
* @param algorithm The AES Algorithm to check.
|
|
180
|
+
* @returns `true` if the given algorithm is a Cipher AES-GCM algorithm. `false` otherwise.
|
|
181
|
+
*/
|
|
182
|
+
static isGCM(algorithm: Cph.AesAlgorithm): algorithm is crypto.CipherGCMTypes;
|
|
183
|
+
/**
|
|
184
|
+
* Check if the given algorithm is a Cipher AES-CCM algorithm.
|
|
185
|
+
*
|
|
186
|
+
* @param algorithm The AES Algorithm to check.
|
|
187
|
+
* @returns `true` if the given algorithm is a Cipher AES-CCM algorithm. `false` otherwise.
|
|
188
|
+
*/
|
|
189
|
+
static isCCM(algorithm: Cph.AesAlgorithm): algorithm is crypto.CipherCCMTypes;
|
|
190
|
+
/**
|
|
191
|
+
* Check if the given algorithm is a Cipher AES-OCB algorithm.
|
|
192
|
+
*
|
|
193
|
+
* @param algorithm The AES Algorithm to check.
|
|
194
|
+
* @returns `true` if the given algorithm is a Cipher AES-OCB algorithm. `false` otherwise.
|
|
195
|
+
*/
|
|
196
|
+
static isOCB(algorithm: Cph.AesAlgorithm): algorithm is crypto.CipherOCBTypes;
|
|
197
|
+
/**
|
|
198
|
+
* Check if the given algorithm is a Cipher AES-CBC algorithm.
|
|
199
|
+
*
|
|
200
|
+
* @param algorithm The AES Algorithm to check.
|
|
201
|
+
* @returns `true` if the given algorithm is a Cipher AES-CBC algorithm. `false` otherwise.
|
|
202
|
+
*/
|
|
203
|
+
static isCBC(algorithm: Cph.AesAlgorithm): algorithm is Cph.CBCTypes;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export { Cipher };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';var u=require('crypto'),stream=require('stream'),coercion=require('@alessiofrittoli/crypto-buffer/coercion');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var u__default=/*#__PURE__*/_interopDefault(u);var t=class t{static encrypt(a,e,o={}){let r=coercion.coerceToUint8Array(a),{salt:n,Key:c,IV:i,AAD:p,options:{algorithm:s,authTag:h}}=t.newKeyIV(e,o);if(t.isCBC(s)){let d=u__default.default.createCipheriv(s,c,i),f=Buffer.concat([d.update(r),d.final()]);return Buffer.concat([n,i,f])}let y=u__default.default.createCipheriv(s,c,i,{authTagLength:h});t.isCCM(s)&&y.setAAD(p,{plaintextLength:r.length}),(t.isGCM(s)||t.isOCB(s))&&y.setAAD(p);let m=Buffer.concat([y.update(r),y.final()]),l=y.getAuthTag();return Buffer.concat([n,i,p,l,m])}static decrypt(a,e,o={}){let r=coercion.coerceToUint8Array(a),n=coercion.coerceToUint8Array(e),c=t.resolveOptions(o),{algorithm:i}=c,p=r.subarray(0,c.salt);r=r.subarray(c.salt);let s=r.subarray(0,c.iv);r=r.subarray(c.iv);let h=u__default.default.scryptSync(n,p,c.length);if(t.isCBC(i)){let T=u__default.default.createDecipheriv(c.algorithm,h,s);return Buffer.concat([T.update(r),T.final()])}let y=c.aadLength,m=c.authTag,l=r.subarray(0,y);r=r.subarray(y);let d=r.subarray(0,m);r=r.subarray(m);let f=u__default.default.createDecipheriv(i,h,s,{authTagLength:m});return t.isCCM(i)&&f.setAAD(l,{plaintextLength:r.length}),(t.isGCM(i)||t.isOCB(i))&&f.setAAD(l),f.setAuthTag(d),Buffer.concat([f.update(r),f.final()])}static streamEncrypt(a,e){e.algorithm||(e.algorithm=t.DEFAULT_ALGORITHM.stream);let{Key:o,IV:r,options:{input:n,output:c,algorithm:i,salt:p,iv:s,authTag:h}}=t.newKeyIV(a,e),y=t.encrypt(Buffer.concat([o,r]),a,{algorithm:i,salt:p,iv:s,authTag:h});return t.stream({Key:o,IV:r,input:n,output:c,encryptedKey:y,algorithm:i})}static streamDecrypt(a,e){e.algorithm||(e.algorithm=t.DEFAULT_ALGORITHM.stream);let{input:o,output:r,algorithm:n,salt:c,iv:i,authTag:p,length:s}=t.resolveOptions(e),h=s+i+c+i+p;return t.extractKeyIV(o,h).then(([y,m])=>{if(m.errored)throw m.errored;let l=t.decrypt(y,a,{algorithm:n,salt:c,iv:i,authTag:p});return t.decipherStream({KeyIV:l,length:s,input:m,output:r,algorithm:n})})}static hybridEncrypt(a,e,o){o.algorithm||(o.algorithm=t.DEFAULT_ALGORITHM.stream);let{Key:r,IV:n,options:{algorithm:c,input:i,output:p}}=t.newKeyIV(a,o),s=u__default.default.publicEncrypt(e,Buffer.concat([r,n]));return t.stream({Key:r,IV:n,input:i,output:p,encryptedKey:s,algorithm:c})}static hybridDecrypt(a,e){e.algorithm||(e.algorithm=t.DEFAULT_ALGORITHM.stream);let{input:o,output:r,algorithm:n,length:c,rsaKeyLength:i}=t.resolveOptions(e);return t.extractKeyIV(o,i).then(([p,s])=>{if(s.errored)throw s.errored;let h=u__default.default.privateDecrypt(a,p);return t.decipherStream({KeyIV:h,length:c,input:s,output:r,algorithm:n})})}static stream(a){let{Key:e,IV:o,encryptedKey:r,input:n,output:c,algorithm:i}=a;return new Promise((p,s)=>{let h=u__default.default.createCipheriv(i,e,o);h.on("error",s),n.on("error",s),c.on("error",s),c.on("finish",p),c.write(r),n.pipe(h).pipe(c);})}static decipherStream(a){let{KeyIV:e,length:o,algorithm:r,input:n,output:c}=a;return new Promise((i,p)=>{let s=e.subarray(0,o),h=e.subarray(o),y=u__default.default.createDecipheriv(r,s,h);y.on("error",p),n.on("error",p),c.on("error",p),c.on("finish",i),n.pipe(y).pipe(c);})}static extractKeyIV(a,e){return new Promise((o,r)=>{let n=Buffer.alloc(0),c=0,i=false,p=new stream.Transform({async transform(s,h,y){if(n.length<e&&(n=Buffer.concat([n,s])),c+=s.length,n.length>e){let m=Math.max(0,n.length-e);n=n.subarray(0,e);let l=s.subarray(m*-1);return i||(o([n,this]),i=true),this.push(l,h),y()}return c>e&&(i||(o([n,this]),i=true),this.push(s,h)),y()},final(s){return n.length<e?s(new Error("The extracted KeyIV length is less than the expected length.")):s()}});p.on("error",r),a.on("error",s=>{p.destroy(s),r(s);}),a.pipe(p);})}static newKeyIV(a,e={}){let o=coercion.coerceToUint8Array(a),r=t.resolveOptions(e),n=u__default.default.randomBytes(r.salt),c=u__default.default.scryptSync(o,n,r.length),i=u__default.default.randomBytes(r.iv),p=r.aad||u__default.default.scryptSync(c,n,r.aadLength);return {options:r,Key:c,IV:i,AAD:p,salt:n}}static resolveOptions(a={}){let e={...a};e.aad&&(e.aad=coercion.coerceToUint8Array(e.aad)),e.salt||(e.salt=t.SALT_LENGTH.default),e.authTag||(e.authTag=t.AUTH_TAG_LENGTH.default),e.aadLength=e.aad?.length||e.aadLength||t.AAD_LENGTH.default,e.salt=Math.min(Math.max(e.salt,t.SALT_LENGTH.min),t.SALT_LENGTH.max),e.authTag=Math.min(Math.max(e.authTag,t.AUTH_TAG_LENGTH.min),t.AUTH_TAG_LENGTH.max),e.aad||(e.aadLength=Math.min(Math.max(e.aadLength,t.AAD_LENGTH.min),t.AAD_LENGTH.max));let{keyLength:o,algorithm:r}=t.getKeyLength(a.algorithm);return e.algorithm=r,e.length=o,e.iv=t.getIVLength(r,a),e}static getIVLength(a=t.DEFAULT_ALGORITHM.buffer,e={}){switch(a){case "aes-128-ccm":case "aes-128-ocb":case "aes-192-ccm":case "aes-192-ocb":case "aes-256-ccm":case "aes-256-ocb":return 8;case "chacha20-poly1305":return 12;default:return Math.min(Math.max(e.iv||t.IV_LENGTH.default,t.IV_LENGTH.min),t.IV_LENGTH.max)}}static getKeyLength(a=t.DEFAULT_ALGORITHM.buffer){switch(a){case "chacha20-poly1305":return {algorithm:a,keyLength:256/8};case "aes-128-cbc":case "aes-128-ccm":case "aes-128-gcm":case "aes-128-ocb":return {algorithm:a,keyLength:128/8};case "aes-192-cbc":case "aes-192-ccm":case "aes-192-gcm":case "aes-192-ocb":return {algorithm:a,keyLength:192/8};case "aes-256-cbc":case "aes-256-ccm":case "aes-256-gcm":case "aes-256-ocb":default:return {algorithm:a,keyLength:256/8}}}static isGCM(a){return a==="aes-128-gcm"||a==="aes-192-gcm"||a==="aes-256-gcm"}static isCCM(a){return a==="aes-128-ccm"||a==="aes-192-ccm"||a==="aes-256-ccm"||a==="chacha20-poly1305"}static isOCB(a){return a==="aes-128-ocb"||a==="aes-192-ocb"||a==="aes-256-ocb"}static isCBC(a){return a==="aes-128-cbc"||a==="aes-192-cbc"||a==="aes-256-cbc"}};t.SALT_LENGTH={min:16,max:64,default:32},t.IV_LENGTH={min:8,max:32,default:16},t.AUTH_TAG_LENGTH={min:4,max:16,default:16},t.AAD_LENGTH={min:16,max:128,default:32},t.DEFAULT_ALGORITHM={buffer:"aes-256-gcm",stream:"aes-256-cbc"},t.ALGORITHMS=["aes-128-cbc","aes-128-ccm","aes-128-gcm","aes-128-ocb","aes-192-cbc","aes-192-ccm","aes-192-gcm","aes-192-ocb","aes-256-cbc","aes-256-ccm","aes-256-gcm","aes-256-ocb","chacha20-poly1305"];var A=t;exports.Cipher=A;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import u from'crypto';import {Transform}from'stream';import {coerceToUint8Array}from'@alessiofrittoli/crypto-buffer/coercion';var t=class t{static encrypt(a,e,o={}){let r=coerceToUint8Array(a),{salt:n,Key:c,IV:i,AAD:p,options:{algorithm:s,authTag:h}}=t.newKeyIV(e,o);if(t.isCBC(s)){let d=u.createCipheriv(s,c,i),f=Buffer.concat([d.update(r),d.final()]);return Buffer.concat([n,i,f])}let y=u.createCipheriv(s,c,i,{authTagLength:h});t.isCCM(s)&&y.setAAD(p,{plaintextLength:r.length}),(t.isGCM(s)||t.isOCB(s))&&y.setAAD(p);let m=Buffer.concat([y.update(r),y.final()]),l=y.getAuthTag();return Buffer.concat([n,i,p,l,m])}static decrypt(a,e,o={}){let r=coerceToUint8Array(a),n=coerceToUint8Array(e),c=t.resolveOptions(o),{algorithm:i}=c,p=r.subarray(0,c.salt);r=r.subarray(c.salt);let s=r.subarray(0,c.iv);r=r.subarray(c.iv);let h=u.scryptSync(n,p,c.length);if(t.isCBC(i)){let T=u.createDecipheriv(c.algorithm,h,s);return Buffer.concat([T.update(r),T.final()])}let y=c.aadLength,m=c.authTag,l=r.subarray(0,y);r=r.subarray(y);let d=r.subarray(0,m);r=r.subarray(m);let f=u.createDecipheriv(i,h,s,{authTagLength:m});return t.isCCM(i)&&f.setAAD(l,{plaintextLength:r.length}),(t.isGCM(i)||t.isOCB(i))&&f.setAAD(l),f.setAuthTag(d),Buffer.concat([f.update(r),f.final()])}static streamEncrypt(a,e){e.algorithm||(e.algorithm=t.DEFAULT_ALGORITHM.stream);let{Key:o,IV:r,options:{input:n,output:c,algorithm:i,salt:p,iv:s,authTag:h}}=t.newKeyIV(a,e),y=t.encrypt(Buffer.concat([o,r]),a,{algorithm:i,salt:p,iv:s,authTag:h});return t.stream({Key:o,IV:r,input:n,output:c,encryptedKey:y,algorithm:i})}static streamDecrypt(a,e){e.algorithm||(e.algorithm=t.DEFAULT_ALGORITHM.stream);let{input:o,output:r,algorithm:n,salt:c,iv:i,authTag:p,length:s}=t.resolveOptions(e),h=s+i+c+i+p;return t.extractKeyIV(o,h).then(([y,m])=>{if(m.errored)throw m.errored;let l=t.decrypt(y,a,{algorithm:n,salt:c,iv:i,authTag:p});return t.decipherStream({KeyIV:l,length:s,input:m,output:r,algorithm:n})})}static hybridEncrypt(a,e,o){o.algorithm||(o.algorithm=t.DEFAULT_ALGORITHM.stream);let{Key:r,IV:n,options:{algorithm:c,input:i,output:p}}=t.newKeyIV(a,o),s=u.publicEncrypt(e,Buffer.concat([r,n]));return t.stream({Key:r,IV:n,input:i,output:p,encryptedKey:s,algorithm:c})}static hybridDecrypt(a,e){e.algorithm||(e.algorithm=t.DEFAULT_ALGORITHM.stream);let{input:o,output:r,algorithm:n,length:c,rsaKeyLength:i}=t.resolveOptions(e);return t.extractKeyIV(o,i).then(([p,s])=>{if(s.errored)throw s.errored;let h=u.privateDecrypt(a,p);return t.decipherStream({KeyIV:h,length:c,input:s,output:r,algorithm:n})})}static stream(a){let{Key:e,IV:o,encryptedKey:r,input:n,output:c,algorithm:i}=a;return new Promise((p,s)=>{let h=u.createCipheriv(i,e,o);h.on("error",s),n.on("error",s),c.on("error",s),c.on("finish",p),c.write(r),n.pipe(h).pipe(c);})}static decipherStream(a){let{KeyIV:e,length:o,algorithm:r,input:n,output:c}=a;return new Promise((i,p)=>{let s=e.subarray(0,o),h=e.subarray(o),y=u.createDecipheriv(r,s,h);y.on("error",p),n.on("error",p),c.on("error",p),c.on("finish",i),n.pipe(y).pipe(c);})}static extractKeyIV(a,e){return new Promise((o,r)=>{let n=Buffer.alloc(0),c=0,i=false,p=new Transform({async transform(s,h,y){if(n.length<e&&(n=Buffer.concat([n,s])),c+=s.length,n.length>e){let m=Math.max(0,n.length-e);n=n.subarray(0,e);let l=s.subarray(m*-1);return i||(o([n,this]),i=true),this.push(l,h),y()}return c>e&&(i||(o([n,this]),i=true),this.push(s,h)),y()},final(s){return n.length<e?s(new Error("The extracted KeyIV length is less than the expected length.")):s()}});p.on("error",r),a.on("error",s=>{p.destroy(s),r(s);}),a.pipe(p);})}static newKeyIV(a,e={}){let o=coerceToUint8Array(a),r=t.resolveOptions(e),n=u.randomBytes(r.salt),c=u.scryptSync(o,n,r.length),i=u.randomBytes(r.iv),p=r.aad||u.scryptSync(c,n,r.aadLength);return {options:r,Key:c,IV:i,AAD:p,salt:n}}static resolveOptions(a={}){let e={...a};e.aad&&(e.aad=coerceToUint8Array(e.aad)),e.salt||(e.salt=t.SALT_LENGTH.default),e.authTag||(e.authTag=t.AUTH_TAG_LENGTH.default),e.aadLength=e.aad?.length||e.aadLength||t.AAD_LENGTH.default,e.salt=Math.min(Math.max(e.salt,t.SALT_LENGTH.min),t.SALT_LENGTH.max),e.authTag=Math.min(Math.max(e.authTag,t.AUTH_TAG_LENGTH.min),t.AUTH_TAG_LENGTH.max),e.aad||(e.aadLength=Math.min(Math.max(e.aadLength,t.AAD_LENGTH.min),t.AAD_LENGTH.max));let{keyLength:o,algorithm:r}=t.getKeyLength(a.algorithm);return e.algorithm=r,e.length=o,e.iv=t.getIVLength(r,a),e}static getIVLength(a=t.DEFAULT_ALGORITHM.buffer,e={}){switch(a){case "aes-128-ccm":case "aes-128-ocb":case "aes-192-ccm":case "aes-192-ocb":case "aes-256-ccm":case "aes-256-ocb":return 8;case "chacha20-poly1305":return 12;default:return Math.min(Math.max(e.iv||t.IV_LENGTH.default,t.IV_LENGTH.min),t.IV_LENGTH.max)}}static getKeyLength(a=t.DEFAULT_ALGORITHM.buffer){switch(a){case "chacha20-poly1305":return {algorithm:a,keyLength:256/8};case "aes-128-cbc":case "aes-128-ccm":case "aes-128-gcm":case "aes-128-ocb":return {algorithm:a,keyLength:128/8};case "aes-192-cbc":case "aes-192-ccm":case "aes-192-gcm":case "aes-192-ocb":return {algorithm:a,keyLength:192/8};case "aes-256-cbc":case "aes-256-ccm":case "aes-256-gcm":case "aes-256-ocb":default:return {algorithm:a,keyLength:256/8}}}static isGCM(a){return a==="aes-128-gcm"||a==="aes-192-gcm"||a==="aes-256-gcm"}static isCCM(a){return a==="aes-128-ccm"||a==="aes-192-ccm"||a==="aes-256-ccm"||a==="chacha20-poly1305"}static isOCB(a){return a==="aes-128-ocb"||a==="aes-192-ocb"||a==="aes-256-ocb"}static isCBC(a){return a==="aes-128-cbc"||a==="aes-192-cbc"||a==="aes-256-cbc"}};t.SALT_LENGTH={min:16,max:64,default:32},t.IV_LENGTH={min:8,max:32,default:16},t.AUTH_TAG_LENGTH={min:4,max:16,default:16},t.AAD_LENGTH={min:16,max:128,default:32},t.DEFAULT_ALGORITHM={buffer:"aes-256-gcm",stream:"aes-256-cbc"},t.ALGORITHMS=["aes-128-cbc","aes-128-ccm","aes-128-gcm","aes-128-ocb","aes-192-cbc","aes-192-ccm","aes-192-gcm","aes-192-ocb","aes-256-cbc","aes-256-ccm","aes-256-gcm","aes-256-ocb","chacha20-poly1305"];var A=t;export{A as Cipher};
|
package/dist/types.d.mts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { CoerceToUint8ArrayInput } from '@alessiofrittoli/crypto-buffer';
|
|
2
|
+
import crypto from 'crypto';
|
|
3
|
+
import { Readable, Writable } from 'stream';
|
|
4
|
+
|
|
5
|
+
declare namespace Cph {
|
|
6
|
+
/** Cipher CBC algorithm types. */
|
|
7
|
+
type CBCTypes = 'aes-128-cbc' | 'aes-192-cbc' | 'aes-256-cbc';
|
|
8
|
+
/** Cipher supported algorithm types. */
|
|
9
|
+
type AesAlgorithm = (crypto.CipherCCMTypes | crypto.CipherGCMTypes | crypto.CipherOCBTypes | Cph.CBCTypes);
|
|
10
|
+
/**
|
|
11
|
+
* Common Cipher options.
|
|
12
|
+
* @template T The AES algorithm type.
|
|
13
|
+
*/
|
|
14
|
+
interface Options<T extends Cph.AesAlgorithm = Cph.AesAlgorithm> {
|
|
15
|
+
/** The Cipher algorithm to use. Default: `aes-256-gcm` | `aes-256-cbc`. */
|
|
16
|
+
algorithm?: T;
|
|
17
|
+
/** The salt length. Minimum: `16`, Maximum: `64`. Default: `32`. */
|
|
18
|
+
salt?: number;
|
|
19
|
+
/** The `IV` length. Minimum: `8`, Maximum: `32`. Default: `16`. */
|
|
20
|
+
iv?: number;
|
|
21
|
+
/** The `authTag` length. Minimum: `4`, Maximum: `16`. Default: `16`. */
|
|
22
|
+
authTag?: number;
|
|
23
|
+
/** Custom `Additional Authenticated Data`. */
|
|
24
|
+
aad?: CoerceToUint8ArrayInput;
|
|
25
|
+
/** The `AAD` length. Minimum: `16`, Maximum: `128`. Default: `32`. */
|
|
26
|
+
aadLength?: number;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Resolved Common options.
|
|
30
|
+
*/
|
|
31
|
+
interface ResolvedOptions<T extends Cph.AesAlgorithm = Cph.AesAlgorithm> extends Required<Omit<Cph.Options<T>, 'aad'>> {
|
|
32
|
+
/** The symmetric key length. */
|
|
33
|
+
length: number;
|
|
34
|
+
aad?: Uint8Array;
|
|
35
|
+
}
|
|
36
|
+
namespace Stream {
|
|
37
|
+
namespace Symmetric {
|
|
38
|
+
/**
|
|
39
|
+
* Options for encrypting a stream.
|
|
40
|
+
*/
|
|
41
|
+
interface EncryptOptions extends Cph.Options<Cph.CBCTypes> {
|
|
42
|
+
/** The `Readable` Stream from where raw data to encrypt is read. */
|
|
43
|
+
input: Readable;
|
|
44
|
+
/** The `Writable` Stream where encrypted data is written. */
|
|
45
|
+
output: Writable;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Resolved Stream Encrypt options (symmetric and hybrid).
|
|
49
|
+
*/
|
|
50
|
+
type EncryptResolvedOptions = (Cph.ResolvedOptions & Required<Cph.Stream.Symmetric.EncryptOptions>);
|
|
51
|
+
/**
|
|
52
|
+
* Options for decrypting a stream (symmetric).
|
|
53
|
+
*/
|
|
54
|
+
interface DecryptOptions extends Cph.Stream.Symmetric.EncryptOptions {
|
|
55
|
+
/** The `Readable` Stream from where encrypted data is read. */
|
|
56
|
+
input: Readable;
|
|
57
|
+
/** The `Writable` Stream where decrypted data is written. */
|
|
58
|
+
output: Writable;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Resolved Stream Decrypt options (symmetric).
|
|
62
|
+
*/
|
|
63
|
+
type DecryptResolvedOptions = (Cph.ResolvedOptions & Required<Cph.Stream.Symmetric.DecryptOptions>);
|
|
64
|
+
}
|
|
65
|
+
namespace Hybrid {
|
|
66
|
+
/**
|
|
67
|
+
* Alias for {@link Cph.Stream.Symmetric.EncryptOptions}
|
|
68
|
+
*/
|
|
69
|
+
type EncryptOptions = Cph.Stream.Symmetric.EncryptOptions;
|
|
70
|
+
/**
|
|
71
|
+
* Alias for {@link Cph.Stream.Symmetric.EncryptResolvedOptions}
|
|
72
|
+
*/
|
|
73
|
+
type EncryptResolvedOptions = Cph.Stream.Symmetric.EncryptResolvedOptions;
|
|
74
|
+
/**
|
|
75
|
+
* Options for decrypting a stream.
|
|
76
|
+
*/
|
|
77
|
+
interface DecryptOptions extends Cph.Stream.Symmetric.DecryptOptions {
|
|
78
|
+
/** The RSA key length in bytes used while encrypting data. This is used to properly extract the encrypted Cipher Key and Initialization Vector. */
|
|
79
|
+
rsaKeyLength: number;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Resolved Stream Decrypt options.
|
|
83
|
+
*/
|
|
84
|
+
type DecryptResolvedOptions = (Cph.ResolvedOptions & Required<Cph.Stream.Hybrid.DecryptOptions>);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export { Cph };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { CoerceToUint8ArrayInput } from '@alessiofrittoli/crypto-buffer';
|
|
2
|
+
import crypto from 'crypto';
|
|
3
|
+
import { Readable, Writable } from 'stream';
|
|
4
|
+
|
|
5
|
+
declare namespace Cph {
|
|
6
|
+
/** Cipher CBC algorithm types. */
|
|
7
|
+
type CBCTypes = 'aes-128-cbc' | 'aes-192-cbc' | 'aes-256-cbc';
|
|
8
|
+
/** Cipher supported algorithm types. */
|
|
9
|
+
type AesAlgorithm = (crypto.CipherCCMTypes | crypto.CipherGCMTypes | crypto.CipherOCBTypes | Cph.CBCTypes);
|
|
10
|
+
/**
|
|
11
|
+
* Common Cipher options.
|
|
12
|
+
* @template T The AES algorithm type.
|
|
13
|
+
*/
|
|
14
|
+
interface Options<T extends Cph.AesAlgorithm = Cph.AesAlgorithm> {
|
|
15
|
+
/** The Cipher algorithm to use. Default: `aes-256-gcm` | `aes-256-cbc`. */
|
|
16
|
+
algorithm?: T;
|
|
17
|
+
/** The salt length. Minimum: `16`, Maximum: `64`. Default: `32`. */
|
|
18
|
+
salt?: number;
|
|
19
|
+
/** The `IV` length. Minimum: `8`, Maximum: `32`. Default: `16`. */
|
|
20
|
+
iv?: number;
|
|
21
|
+
/** The `authTag` length. Minimum: `4`, Maximum: `16`. Default: `16`. */
|
|
22
|
+
authTag?: number;
|
|
23
|
+
/** Custom `Additional Authenticated Data`. */
|
|
24
|
+
aad?: CoerceToUint8ArrayInput;
|
|
25
|
+
/** The `AAD` length. Minimum: `16`, Maximum: `128`. Default: `32`. */
|
|
26
|
+
aadLength?: number;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Resolved Common options.
|
|
30
|
+
*/
|
|
31
|
+
interface ResolvedOptions<T extends Cph.AesAlgorithm = Cph.AesAlgorithm> extends Required<Omit<Cph.Options<T>, 'aad'>> {
|
|
32
|
+
/** The symmetric key length. */
|
|
33
|
+
length: number;
|
|
34
|
+
aad?: Uint8Array;
|
|
35
|
+
}
|
|
36
|
+
namespace Stream {
|
|
37
|
+
namespace Symmetric {
|
|
38
|
+
/**
|
|
39
|
+
* Options for encrypting a stream.
|
|
40
|
+
*/
|
|
41
|
+
interface EncryptOptions extends Cph.Options<Cph.CBCTypes> {
|
|
42
|
+
/** The `Readable` Stream from where raw data to encrypt is read. */
|
|
43
|
+
input: Readable;
|
|
44
|
+
/** The `Writable` Stream where encrypted data is written. */
|
|
45
|
+
output: Writable;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Resolved Stream Encrypt options (symmetric and hybrid).
|
|
49
|
+
*/
|
|
50
|
+
type EncryptResolvedOptions = (Cph.ResolvedOptions & Required<Cph.Stream.Symmetric.EncryptOptions>);
|
|
51
|
+
/**
|
|
52
|
+
* Options for decrypting a stream (symmetric).
|
|
53
|
+
*/
|
|
54
|
+
interface DecryptOptions extends Cph.Stream.Symmetric.EncryptOptions {
|
|
55
|
+
/** The `Readable` Stream from where encrypted data is read. */
|
|
56
|
+
input: Readable;
|
|
57
|
+
/** The `Writable` Stream where decrypted data is written. */
|
|
58
|
+
output: Writable;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Resolved Stream Decrypt options (symmetric).
|
|
62
|
+
*/
|
|
63
|
+
type DecryptResolvedOptions = (Cph.ResolvedOptions & Required<Cph.Stream.Symmetric.DecryptOptions>);
|
|
64
|
+
}
|
|
65
|
+
namespace Hybrid {
|
|
66
|
+
/**
|
|
67
|
+
* Alias for {@link Cph.Stream.Symmetric.EncryptOptions}
|
|
68
|
+
*/
|
|
69
|
+
type EncryptOptions = Cph.Stream.Symmetric.EncryptOptions;
|
|
70
|
+
/**
|
|
71
|
+
* Alias for {@link Cph.Stream.Symmetric.EncryptResolvedOptions}
|
|
72
|
+
*/
|
|
73
|
+
type EncryptResolvedOptions = Cph.Stream.Symmetric.EncryptResolvedOptions;
|
|
74
|
+
/**
|
|
75
|
+
* Options for decrypting a stream.
|
|
76
|
+
*/
|
|
77
|
+
interface DecryptOptions extends Cph.Stream.Symmetric.DecryptOptions {
|
|
78
|
+
/** The RSA key length in bytes used while encrypting data. This is used to properly extract the encrypted Cipher Key and Initialization Vector. */
|
|
79
|
+
rsaKeyLength: number;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Resolved Stream Decrypt options.
|
|
83
|
+
*/
|
|
84
|
+
type DecryptResolvedOptions = (Cph.ResolvedOptions & Required<Cph.Stream.Hybrid.DecryptOptions>);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export { Cph };
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';
|
package/dist/types.mjs
ADDED
|
File without changes
|
package/license.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Alessio Frittoli.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|