tiny-essentials 1.0.0 → 1.1.1
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 +22 -13
- package/dist/TinyEssentials.min.js +1 -1
- package/dist/v1/basics/objFilter.cjs +212 -14
- package/dist/v1/basics/objFilter.d.mts +60 -0
- package/dist/v1/basics/objFilter.mjs +180 -15
- package/dist/v1/index.cjs +5 -0
- package/dist/v1/index.d.mts +5 -1
- package/dist/v1/index.mjs +3 -2
- package/dist/v1/libs/TinyCrypto.cjs +594 -0
- package/dist/v1/libs/TinyCrypto.d.mts +267 -0
- package/dist/v1/libs/TinyCrypto.mjs +572 -0
- package/docs/README.md +39 -0
- package/docs/basics/array.md +46 -0
- package/docs/basics/clock.md +77 -0
- package/docs/basics/objFilter.md +120 -0
- package/docs/basics/simpleMath.md +65 -0
- package/docs/basics/text.md +38 -0
- package/docs/libs/TinyCrypto.md +177 -0
- package/package.json +16 -2
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
export default TinyCrypto;
|
|
2
|
+
/**
|
|
3
|
+
* TinyCrypto is a utility class that provides methods for secure key generation,
|
|
4
|
+
* encryption, and decryption of data. It also allows for serialization
|
|
5
|
+
* and deserialization of complex data types, and offers methods to save and load encryption
|
|
6
|
+
* configurations and keys from files.
|
|
7
|
+
*
|
|
8
|
+
* @class
|
|
9
|
+
*/
|
|
10
|
+
declare class TinyCrypto {
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new instance of the CryptoManager class with configurable options.
|
|
13
|
+
*
|
|
14
|
+
* @param {Object} [options={}] - Configuration options for encryption and decryption.
|
|
15
|
+
* @param {string} [options.algorithm='aes-256-gcm'] - The encryption algorithm to use. Recommended: 'aes-256-gcm' for authenticated encryption.
|
|
16
|
+
* @param {string} [options.outputEncoding='hex'] - The encoding used when returning encrypted data (e.g., 'hex', 'base64').
|
|
17
|
+
* @param {string} [options.inputEncoding='utf8'] - The encoding used for plaintext inputs (e.g., 'utf8').
|
|
18
|
+
* @param {number} [options.authTagLength=16] - The length of the authentication tag used in GCM mode. Usually 16 for AES-256-GCM.
|
|
19
|
+
* @param {Buffer} [options.key] - Optional 32-byte cryptographic key. If not provided, a random key is generated.
|
|
20
|
+
*
|
|
21
|
+
* @throws {Error} Throws if the provided key is not 32 bytes long.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* const crypto = new CryptoManager({
|
|
25
|
+
* algorithm: 'aes-256-gcm',
|
|
26
|
+
* outputEncoding: 'base64',
|
|
27
|
+
* key: crypto.randomBytes(32),
|
|
28
|
+
* });
|
|
29
|
+
*/
|
|
30
|
+
constructor(options?: {
|
|
31
|
+
algorithm?: string | undefined;
|
|
32
|
+
outputEncoding?: string | undefined;
|
|
33
|
+
inputEncoding?: string | undefined;
|
|
34
|
+
authTagLength?: number | undefined;
|
|
35
|
+
key?: Buffer<ArrayBufferLike> | undefined;
|
|
36
|
+
});
|
|
37
|
+
algorithm: string;
|
|
38
|
+
outputEncoding: string;
|
|
39
|
+
inputEncoding: string;
|
|
40
|
+
authTagLength: number;
|
|
41
|
+
key: Buffer<ArrayBufferLike>;
|
|
42
|
+
/**
|
|
43
|
+
* Generates a secure random cryptographic key.
|
|
44
|
+
*
|
|
45
|
+
* @param {number} [value=32] - The number of bytes to generate. Default is 32 bytes (256 bits), suitable for AES-256.
|
|
46
|
+
* @returns {Buffer} A securely generated random key as a Buffer.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* const key = cryptoManager.generateKey(); // Generates a 32-byte key
|
|
50
|
+
* const customKey = cryptoManager.generateKey(16); // Generates a 16-byte key (e.g. for AES-128)
|
|
51
|
+
*/
|
|
52
|
+
generateKey(value?: number): Buffer;
|
|
53
|
+
/**
|
|
54
|
+
* Generates a secure random Initialization Vector (IV).
|
|
55
|
+
*
|
|
56
|
+
* @param {number} [value=12] - The number of bytes to generate. Default is 12 bytes (96 bits), the recommended size for AES-GCM.
|
|
57
|
+
* @returns {Buffer} A securely generated IV as a Buffer.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* const iv = cryptoManager.generateIV(); // Generates a 12-byte IV
|
|
61
|
+
* const customIV = cryptoManager.generateIV(16); // Generates a 16-byte IV if needed for other algorithms
|
|
62
|
+
*/
|
|
63
|
+
generateIV(value?: number): Buffer;
|
|
64
|
+
/**
|
|
65
|
+
* Encrypts a given value (string, number, object, etc.)
|
|
66
|
+
*
|
|
67
|
+
* The value is first serialized de forma segura (preservando o tipo) antes da criptografia.
|
|
68
|
+
*
|
|
69
|
+
* @param {*} data - The data to encrypt. Can be of any supported type (string, number, boolean, Date, JSON, etc.).
|
|
70
|
+
* @param {Buffer} [iv=this.generateIV()] - Optional Initialization Vector (IV). If not provided, a secure random IV is generated.
|
|
71
|
+
* @returns {Object} An object containing:
|
|
72
|
+
* - `iv` {string} - The IV used, encoded with the output encoding.
|
|
73
|
+
* - `encrypted` {string} - The encrypted payload.
|
|
74
|
+
* - `authTag` {string} - The authentication tag used to verify the integrity of the ciphertext.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* const result = cryptoManager.encrypt('Hello, world!');
|
|
78
|
+
* // {
|
|
79
|
+
* // iv: 'b32a...',
|
|
80
|
+
* // encrypted: 'c1d5...',
|
|
81
|
+
* // authTag: 'aa93...'
|
|
82
|
+
* // }
|
|
83
|
+
*/
|
|
84
|
+
encrypt(data: any, iv?: Buffer): Object;
|
|
85
|
+
/**
|
|
86
|
+
* Decrypts a previously encrypted value.
|
|
87
|
+
*
|
|
88
|
+
* The method checks the integrity of the data using the authentication tag (`authTag`) and ensures the data is properly decrypted.
|
|
89
|
+
* After decryption, it automatically deserializes the data back to its original type.
|
|
90
|
+
*
|
|
91
|
+
* @param {Object} params - An object containing the encrypted data:
|
|
92
|
+
* - `iv` {string} - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
|
|
93
|
+
* - `encrypted` {string} - The encrypted data to decrypt, encoded with the output encoding.
|
|
94
|
+
* - `authTag` {string} - The authentication tag used to verify the integrity of the encrypted data.
|
|
95
|
+
* @param {string|null} [expectedType=null] - Optionally specify the expected type of the decrypted data. If provided, the method will validate the type of the deserialized value.
|
|
96
|
+
* @returns {*} The decrypted value, which will be the original type of the data before encryption.
|
|
97
|
+
* @throws {Error} Throws if the authentication tag doesn't match or the data has been tampered with.
|
|
98
|
+
* @throws {Error} Throws if the deserialized value doesn't match the `expectedType`.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* const encryptedData = {
|
|
102
|
+
* iv: 'b32a...',
|
|
103
|
+
* encrypted: 'c1d5...',
|
|
104
|
+
* authTag: 'aa93...'
|
|
105
|
+
* };
|
|
106
|
+
* const decrypted = cryptoManager.decrypt(encryptedData, 'string');
|
|
107
|
+
* console.log(decrypted); // Outputs: 'Hello, world!'
|
|
108
|
+
*/
|
|
109
|
+
decrypt({ iv, encrypted, authTag }: Object, expectedType?: string | null): any;
|
|
110
|
+
/**
|
|
111
|
+
* Retrieves the type of the original data from an encrypted object.
|
|
112
|
+
*
|
|
113
|
+
* This method decrypts the encrypted data and extracts its type information without fully deserializing the value.
|
|
114
|
+
* It is useful when you need to verify the type of the encrypted data before fully decrypting it.
|
|
115
|
+
*
|
|
116
|
+
* @param {Object} params - An object containing the encrypted data:
|
|
117
|
+
* - `iv` {string} - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
|
|
118
|
+
* - `encrypted` {string} - The encrypted data to decrypt, encoded with the output encoding.
|
|
119
|
+
* - `authTag` {string} - The authentication tag used to verify the integrity of the encrypted data.
|
|
120
|
+
* @returns {string} The type of the original data (e.g., 'string', 'number', 'date', etc.).
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* const encryptedData = {
|
|
124
|
+
* iv: 'b32a...',
|
|
125
|
+
* encrypted: 'c1d5...',
|
|
126
|
+
* authTag: 'aa93...'
|
|
127
|
+
* };
|
|
128
|
+
* const dataType = cryptoManager.getTypeFromEncrypted(encryptedData);
|
|
129
|
+
* console.log(dataType); // Outputs: 'string'
|
|
130
|
+
*/
|
|
131
|
+
getTypeFromEncrypted({ iv, encrypted, authTag }: Object): string;
|
|
132
|
+
/**
|
|
133
|
+
* Saves the cryptographic key to a file.
|
|
134
|
+
*
|
|
135
|
+
* If running in a browser, the method generates a download link for the key as a text file.
|
|
136
|
+
* If running in Node.js, the method saves the key to the specified file path.
|
|
137
|
+
*
|
|
138
|
+
* @param {string} [filename='secret.key'] - The name of the file to save the key. Defaults to 'secret.key'.
|
|
139
|
+
* @throws {Error} Throws an error if the file cannot be written in Node.js.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* // In a browser, triggers a download of the key
|
|
143
|
+
* cryptoManager.saveKeyToFile('myKey.key');
|
|
144
|
+
*
|
|
145
|
+
* // In Node.js, saves the key to 'myKey.key'
|
|
146
|
+
* cryptoManager.saveKeyToFile('myKey.key');
|
|
147
|
+
*/
|
|
148
|
+
saveKeyToFile(filename?: string): void;
|
|
149
|
+
/**
|
|
150
|
+
* Saves the current cryptographic configuration to a JSON file.
|
|
151
|
+
*
|
|
152
|
+
* If running in a browser, the method generates a download link for the configuration as a JSON file.
|
|
153
|
+
* If running in Node.js, the method saves the configuration to the specified file path.
|
|
154
|
+
*
|
|
155
|
+
* @param {string} [filename='crypto-config.json'] - The name of the file to save the configuration. Defaults to 'crypto-config.json'.
|
|
156
|
+
* @throws {Error} Throws an error if the file cannot be written in Node.js.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* // In a browser, triggers a download of the configuration
|
|
160
|
+
* cryptoManager.saveConfigToFile('myConfig.json');
|
|
161
|
+
*
|
|
162
|
+
* // In Node.js, saves the configuration to 'myConfig.json'
|
|
163
|
+
* cryptoManager.saveConfigToFile('myConfig.json');
|
|
164
|
+
*/
|
|
165
|
+
saveConfigToFile(filename?: string): void;
|
|
166
|
+
/**
|
|
167
|
+
* Loads and imports cryptographic configuration from a JSON file.
|
|
168
|
+
*
|
|
169
|
+
* If running in a browser, the method allows the user to select a file, reads the file as text,
|
|
170
|
+
* parses the JSON, and imports the configuration.
|
|
171
|
+
* If running in Node.js, the method reads the file synchronously and imports the configuration.
|
|
172
|
+
*
|
|
173
|
+
* @param {File|string} file - The file to load the configuration from. In the browser, this is a `File` object, and in Node.js, it's a file path.
|
|
174
|
+
* @returns {Promise<void>} A promise that resolves when the configuration is successfully loaded and imported.
|
|
175
|
+
* @throws {Error} Throws an error if the JSON file is invalid or the file cannot be read.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* // In a browser, prompt user to select a file and load the configuration
|
|
179
|
+
* cryptoManager.loadConfigFromFile(file)
|
|
180
|
+
* .then(() => console.log('Config loaded successfully'))
|
|
181
|
+
* .catch(err => console.error('Error loading config:', err));
|
|
182
|
+
*
|
|
183
|
+
* // In Node.js, load the configuration from a file path
|
|
184
|
+
* cryptoManager.loadConfigFromFile('myConfig.json')
|
|
185
|
+
* .then(() => console.log('Config loaded successfully'))
|
|
186
|
+
* .catch(err => console.error('Error loading config:', err));
|
|
187
|
+
*/
|
|
188
|
+
loadConfigFromFile(file: File | string): Promise<void>;
|
|
189
|
+
/**
|
|
190
|
+
* Loads a cryptographic key from a file and sets it for encryption/decryption.
|
|
191
|
+
*
|
|
192
|
+
* If running in a browser, the method allows the user to select a file, reads the file as text,
|
|
193
|
+
* and loads the key (in hexadecimal format) into the current instance.
|
|
194
|
+
* If running in Node.js, the method reads the file synchronously, parses the hexadecimal key,
|
|
195
|
+
* and loads it into the current instance.
|
|
196
|
+
*
|
|
197
|
+
* @param {File|string} file - The file to load the key from. In the browser, this is a `File` object, and in Node.js, it's a file path.
|
|
198
|
+
* @returns {Promise<Buffer>} A promise that resolves with the key as a `Buffer` when the file is successfully loaded.
|
|
199
|
+
* @throws {Error} Throws an error if the file cannot be read or if the key is invalid.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* // In a browser, prompt user to select a file and load the key
|
|
203
|
+
* cryptoManager.loadKeyFromFile(file)
|
|
204
|
+
* .then(key => console.log('Key loaded successfully:', key))
|
|
205
|
+
* .catch(err => console.error('Error loading key:', err));
|
|
206
|
+
*
|
|
207
|
+
* // In Node.js, load the key from a file path
|
|
208
|
+
* cryptoManager.loadKeyFromFile('myKey.key')
|
|
209
|
+
* .then(key => console.log('Key loaded successfully:', key))
|
|
210
|
+
* .catch(err => console.error('Error loading key:', err));
|
|
211
|
+
*/
|
|
212
|
+
loadKeyFromFile(file: File | string): Promise<Buffer>;
|
|
213
|
+
/**
|
|
214
|
+
* Exports the current cryptographic configuration as a JSON object.
|
|
215
|
+
*
|
|
216
|
+
* The exported configuration includes the encryption algorithm, output encoding format,
|
|
217
|
+
* input encoding format, the cryptographic key (in hexadecimal format), and the authentication tag length.
|
|
218
|
+
* This method does not include any sensitive data like the raw key, only its hexadecimal representation.
|
|
219
|
+
*
|
|
220
|
+
* @returns {Object} The exported configuration as a plain JavaScript object.
|
|
221
|
+
* @example
|
|
222
|
+
* const config = cryptoManager.exportConfig();
|
|
223
|
+
* console.log(config);
|
|
224
|
+
* // Example output:
|
|
225
|
+
* // {
|
|
226
|
+
* // algorithm: 'aes-256-gcm',
|
|
227
|
+
* // outputEncoding: 'hex',
|
|
228
|
+
* // inputEncoding: 'utf8',
|
|
229
|
+
* // key: 'abcdef1234567890...',
|
|
230
|
+
* // authTagLength: 16
|
|
231
|
+
* // }
|
|
232
|
+
*/
|
|
233
|
+
exportConfig(): Object;
|
|
234
|
+
/**
|
|
235
|
+
* Imports a cryptographic configuration from a JSON object.
|
|
236
|
+
*
|
|
237
|
+
* This method sets the configuration for the encryption process, including the algorithm, encoding formats,
|
|
238
|
+
* authentication tag length, and the cryptographic key (in hexadecimal string format).
|
|
239
|
+
* If any of the expected properties are missing or invalid, an error will be thrown.
|
|
240
|
+
*
|
|
241
|
+
* @param {Object} config - The configuration object to import.
|
|
242
|
+
* @param {string} config.algorithm - The encryption algorithm (e.g., 'aes-256-gcm').
|
|
243
|
+
* @param {string} config.outputEncoding - The output encoding format (e.g., 'hex').
|
|
244
|
+
* @param {string} config.inputEncoding - The input encoding format (e.g., 'utf8').
|
|
245
|
+
* @param {number} config.authTagLength - The authentication tag length (e.g., 16).
|
|
246
|
+
* @param {string} config.key - The cryptographic key in hexadecimal string format.
|
|
247
|
+
*
|
|
248
|
+
* @throws {Error} If any required property is missing or has an invalid type.
|
|
249
|
+
* @example
|
|
250
|
+
* const config = {
|
|
251
|
+
* algorithm: 'aes-256-gcm',
|
|
252
|
+
* outputEncoding: 'hex',
|
|
253
|
+
* inputEncoding: 'utf8',
|
|
254
|
+
* authTagLength: 16,
|
|
255
|
+
* key: 'abcdef1234567890abcdef1234567890',
|
|
256
|
+
* };
|
|
257
|
+
* cryptoManager.importConfig(config);
|
|
258
|
+
*/
|
|
259
|
+
importConfig(config: {
|
|
260
|
+
algorithm: string;
|
|
261
|
+
outputEncoding: string;
|
|
262
|
+
inputEncoding: string;
|
|
263
|
+
authTagLength: number;
|
|
264
|
+
key: string;
|
|
265
|
+
}): void;
|
|
266
|
+
#private;
|
|
267
|
+
}
|