tiny-essentials 1.2.0 → 1.2.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/dist/TinyBasicsEs.js +51 -14
- package/dist/TinyBasicsEs.min.js +1 -1
- package/dist/TinyCertCrypto.js +26497 -71362
- package/dist/TinyCertCrypto.min.js +1 -1
- package/dist/TinyCertCrypto.min.js.LICENSE.txt +0 -2
- package/dist/TinyCrypto.js +93 -70
- package/dist/TinyCrypto.min.js +1 -1
- package/dist/TinyEssentials.js +232 -136
- package/dist/TinyEssentials.min.js +1 -1
- package/dist/TinyLevelUp.js +9 -9
- package/dist/legacy/crypto/decrypt.cjs +1 -0
- package/dist/legacy/crypto/decrypt.mjs +1 -0
- package/dist/legacy/crypto/default.cjs +2 -0
- package/dist/legacy/crypto/default.mjs +1 -0
- package/dist/legacy/crypto/encrypt.cjs +2 -1
- package/dist/legacy/crypto/encrypt.mjs +1 -0
- package/dist/legacy/crypto/index.cjs +1 -1
- package/dist/legacy/crypto/index.mjs +1 -0
- package/dist/legacy/firebase/cookieSession.cjs +2 -0
- package/dist/legacy/firebase/cookieSession.mjs +1 -0
- package/dist/legacy/firebase/database/index.cjs +3 -0
- package/dist/legacy/firebase/database/index.mjs +1 -0
- package/dist/legacy/firebase/database/presence.cjs +3 -0
- package/dist/legacy/firebase/database/presence.mjs +1 -0
- package/dist/legacy/firebase/database/saveAsync.cjs +2 -0
- package/dist/legacy/firebase/database/saveAsync.mjs +1 -0
- package/dist/legacy/firebase/databaseEscape.cjs +3 -0
- package/dist/legacy/firebase/databaseEscape.mjs +1 -0
- package/dist/legacy/firebase/databaseLogger.cjs +3 -0
- package/dist/legacy/firebase/databaseLogger.mjs +1 -0
- package/dist/legacy/firebase/discord/api/addGuildMember.cjs +3 -0
- package/dist/legacy/firebase/discord/api/addGuildMember.mjs +1 -0
- package/dist/legacy/firebase/discord/api/getGuildWidget.cjs +3 -0
- package/dist/legacy/firebase/discord/api/getGuildWidget.mjs +1 -0
- package/dist/legacy/firebase/discord/api/getToken.cjs +3 -0
- package/dist/legacy/firebase/discord/api/getToken.mjs +1 -0
- package/dist/legacy/firebase/discord/api/getUser.cjs +3 -0
- package/dist/legacy/firebase/discord/api/getUser.mjs +1 -0
- package/dist/legacy/firebase/discord/api/getUserConnections.cjs +3 -0
- package/dist/legacy/firebase/discord/api/getUserConnections.mjs +1 -0
- package/dist/legacy/firebase/discord/api/getUserGuilds.cjs +3 -0
- package/dist/legacy/firebase/discord/api/getUserGuilds.mjs +1 -0
- package/dist/legacy/firebase/discord/api/index.cjs +3 -0
- package/dist/legacy/firebase/discord/api/index.mjs +1 -0
- package/dist/legacy/firebase/discord/api/refreshToken.cjs +3 -0
- package/dist/legacy/firebase/discord/api/refreshToken.mjs +1 -0
- package/dist/legacy/firebase/discord/api/revokeToken.cjs +3 -0
- package/dist/legacy/firebase/discord/api/revokeToken.mjs +1 -0
- package/dist/legacy/firebase/discord/config.cjs +2 -0
- package/dist/legacy/firebase/discord/config.mjs +1 -0
- package/dist/legacy/firebase/discord/firebase_redirect/index.cjs +3 -0
- package/dist/legacy/firebase/discord/firebase_redirect/index.mjs +1 -0
- package/dist/legacy/firebase/discord/firebase_redirect/login.cjs +2 -0
- package/dist/legacy/firebase/discord/firebase_redirect/login.mjs +1 -0
- package/dist/legacy/firebase/discord/firebase_redirect/logout.cjs +2 -0
- package/dist/legacy/firebase/discord/firebase_redirect/logout.mjs +1 -0
- package/dist/legacy/firebase/discord/get/authURLGenerator.cjs +3 -0
- package/dist/legacy/firebase/discord/get/authURLGenerator.mjs +1 -0
- package/dist/legacy/firebase/discord/get/cookie-session.cjs +3 -0
- package/dist/legacy/firebase/discord/get/cookie-session.mjs +1 -0
- package/dist/legacy/firebase/discord/get/credentials.cjs +2 -0
- package/dist/legacy/firebase/discord/get/credentials.mjs +1 -0
- package/dist/legacy/firebase/discord/get/errorValidator.cjs +3 -0
- package/dist/legacy/firebase/discord/get/errorValidator.mjs +1 -0
- package/dist/legacy/firebase/discord/get/index.cjs +3 -0
- package/dist/legacy/firebase/discord/get/index.mjs +1 -0
- package/dist/legacy/firebase/discord/get/randomAvatar.cjs +3 -0
- package/dist/legacy/firebase/discord/get/randomAvatar.mjs +1 -0
- package/dist/legacy/firebase/discord/http/index.cjs +3 -0
- package/dist/legacy/firebase/discord/http/index.mjs +1 -0
- package/dist/legacy/firebase/discord/http/login.cjs +3 -0
- package/dist/legacy/firebase/discord/http/login.mjs +1 -0
- package/dist/legacy/firebase/discord/http/logout.cjs +3 -0
- package/dist/legacy/firebase/discord/http/logout.mjs +1 -0
- package/dist/legacy/firebase/discord/http/redirect.cjs +3 -0
- package/dist/legacy/firebase/discord/http/redirect.mjs +1 -0
- package/dist/legacy/firebase/discord/http/refreshToken.cjs +3 -0
- package/dist/legacy/firebase/discord/http/refreshToken.mjs +1 -0
- package/dist/legacy/firebase/discord/index.cjs +3 -0
- package/dist/legacy/firebase/discord/index.mjs +1 -0
- package/dist/legacy/firebase/discord/template/cookie-session.cjs +3 -0
- package/dist/legacy/firebase/discord/template/cookie-session.mjs +1 -0
- package/dist/legacy/firebase/domainRedirect.cjs +2 -0
- package/dist/legacy/firebase/domainRedirect.mjs +1 -0
- package/dist/legacy/firebase/escape.cjs +2 -0
- package/dist/legacy/firebase/escape.mjs +1 -0
- package/dist/legacy/firebase/getDBAsync.cjs +2 -0
- package/dist/legacy/firebase/getDBAsync.mjs +1 -0
- package/dist/legacy/firebase/getDBData.cjs +3 -0
- package/dist/legacy/firebase/getDBData.mjs +1 -0
- package/dist/legacy/firebase/getDBValue.cjs +2 -0
- package/dist/legacy/firebase/getDBValue.mjs +1 -0
- package/dist/legacy/firebase/index.cjs +3 -0
- package/dist/legacy/firebase/index.mjs +1 -0
- package/dist/legacy/firebase/isEmulator.cjs +2 -0
- package/dist/legacy/firebase/isEmulator.mjs +1 -0
- package/dist/legacy/firebase/logger.cjs +3 -0
- package/dist/legacy/firebase/logger.mjs +1 -0
- package/dist/legacy/firebase/mySQL.cjs +3 -0
- package/dist/legacy/firebase/mySQL.mjs +1 -0
- package/dist/legacy/firebase/mysqlConnector/create.cjs +2 -0
- package/dist/legacy/firebase/mysqlConnector/create.mjs +1 -0
- package/dist/legacy/firebase/mysqlConnector/index.cjs +3 -0
- package/dist/legacy/firebase/mysqlConnector/index.mjs +1 -0
- package/dist/legacy/firebase/mysqlConnector/nextPrev.cjs +2 -0
- package/dist/legacy/firebase/mysqlConnector/nextPrev.mjs +1 -0
- package/dist/legacy/firebase/mysqlConnector/pagination.cjs +2 -0
- package/dist/legacy/firebase/mysqlConnector/pagination.mjs +1 -0
- package/dist/legacy/firebase/mysqlConnector/sameUser.cjs +3 -0
- package/dist/legacy/firebase/mysqlConnector/sameUser.mjs +1 -0
- package/dist/legacy/firebase/transactionDBAsync.cjs +2 -0
- package/dist/legacy/firebase/transactionDBAsync.mjs +1 -0
- package/dist/legacy/get/countObj.cjs +3 -0
- package/dist/legacy/get/countObj.mjs +1 -0
- package/dist/legacy/get/decimalColor.cjs +3 -0
- package/dist/legacy/get/decimalColor.mjs +1 -0
- package/dist/legacy/get/objType.cjs +2 -0
- package/dist/legacy/get/objType.mjs +1 -0
- package/dist/legacy/get/pagination.cjs +3 -0
- package/dist/legacy/get/pagination.mjs +1 -0
- package/dist/legacy/get/queryUrlByName.cjs +2 -0
- package/dist/legacy/get/queryUrlByName.mjs +1 -0
- package/dist/legacy/get/queryUrlJSON.cjs +2 -0
- package/dist/legacy/get/queryUrlJSON.mjs +1 -0
- package/dist/legacy/get/super_string_filter.cjs +3 -0
- package/dist/legacy/get/super_string_filter.mjs +1 -0
- package/dist/legacy/get/versionCheck.cjs +2 -1
- package/dist/legacy/get/versionCheck.mjs +1 -0
- package/dist/legacy/http/HTTP-1.0.cjs +2 -0
- package/dist/legacy/http/HTTP-1.0.mjs +1 -0
- package/dist/legacy/http/auth.cjs +2 -0
- package/dist/legacy/http/auth.mjs +1 -0
- package/dist/legacy/http/check_domain.cjs +2 -0
- package/dist/legacy/http/check_domain.mjs +1 -0
- package/dist/legacy/http/csrfTokenAnalyze.cjs +2 -0
- package/dist/legacy/http/csrfTokenAnalyze.mjs +1 -0
- package/dist/legacy/http/domainValidator.cjs +3 -0
- package/dist/legacy/http/domainValidator.mjs +1 -0
- package/dist/legacy/http/errorsCallback.cjs +2 -0
- package/dist/legacy/http/errorsCallback.mjs +1 -0
- package/dist/legacy/http/fetch/json.cjs +3 -0
- package/dist/legacy/http/fetch/json.mjs +1 -0
- package/dist/legacy/http/fetch/text.cjs +3 -0
- package/dist/legacy/http/fetch/text.mjs +1 -0
- package/dist/legacy/http/fileCache.cjs +3 -0
- package/dist/legacy/http/fileCache.mjs +1 -0
- package/dist/legacy/http/getDomainURL.cjs +3 -0
- package/dist/legacy/http/getDomainURL.mjs +1 -0
- package/dist/legacy/http/userIP.cjs +3 -0
- package/dist/legacy/http/userIP.mjs +1 -0
- package/dist/legacy/index.cjs +3 -0
- package/dist/legacy/index.mjs +1 -0
- package/dist/legacy/libs/arraySortPositions.cjs +2 -0
- package/dist/legacy/libs/arraySortPositions.mjs +1 -0
- package/dist/legacy/libs/capitalize.cjs +2 -0
- package/dist/legacy/libs/capitalize.mjs +1 -0
- package/dist/legacy/libs/convertBytes.cjs +2 -0
- package/dist/legacy/libs/convertBytes.mjs +1 -0
- package/dist/legacy/libs/custom_module_loader.cjs +3 -0
- package/dist/legacy/libs/custom_module_loader.mjs +1 -0
- package/dist/legacy/libs/dice.cjs +2 -0
- package/dist/legacy/libs/dice.mjs +1 -0
- package/dist/legacy/libs/markdown.cjs +2 -0
- package/dist/legacy/libs/markdown.mjs +1 -0
- package/dist/legacy/libs/percentage.cjs +2 -0
- package/dist/legacy/libs/percentage.mjs +1 -0
- package/dist/legacy/libs/regex/getLetter.cjs +2 -0
- package/dist/legacy/libs/regex/getLetter.mjs +1 -0
- package/dist/legacy/libs/replaceAsync.cjs +3 -0
- package/dist/legacy/libs/replaceAsync.mjs +3 -0
- package/dist/legacy/libs/rule3.cjs +2 -0
- package/dist/legacy/libs/rule3.mjs +1 -0
- package/dist/legacy/libs/userLevel.cjs +9 -9
- package/dist/legacy/libs/userLevel.d.mts +39 -16
- package/dist/legacy/libs/userLevel.mjs +9 -9
- package/dist/legacy/momentjs/getAge.cjs +3 -0
- package/dist/legacy/momentjs/getAge.mjs +1 -0
- package/dist/legacy/momentjs/index.cjs +1 -1
- package/dist/legacy/momentjs/index.mjs +1 -0
- package/dist/legacy/momentjs/timeDuration.cjs +3 -0
- package/dist/legacy/momentjs/timeDuration.mjs +1 -0
- package/dist/legacy/socket.io/antiFlood/index.cjs +1 -1
- package/dist/legacy/socket.io/antiFlood/index.mjs +1 -0
- package/dist/legacy/socket.io/antiFlood/install.cjs +3 -0
- package/dist/legacy/socket.io/antiFlood/install.mjs +1 -0
- package/dist/legacy/socket.io/antiFlood/verify.cjs +3 -0
- package/dist/legacy/socket.io/antiFlood/verify.mjs +1 -0
- package/dist/legacy/socket.io/cookie-session.cjs +2 -0
- package/dist/legacy/socket.io/cookie-session.mjs +1 -0
- package/dist/legacy/socket.io/discord.cjs +3 -0
- package/dist/legacy/socket.io/discord.mjs +1 -0
- package/dist/legacy/socket.io/index.cjs +3 -0
- package/dist/legacy/socket.io/index.mjs +1 -0
- package/dist/v1/basics/clock.cjs +43 -13
- package/dist/v1/basics/clock.mjs +31 -13
- package/dist/v1/basics/objFilter.cjs +4 -0
- package/dist/v1/basics/objFilter.mjs +3 -0
- package/dist/v1/basics/simpleMath.cjs +1 -1
- package/dist/v1/basics/simpleMath.mjs +1 -1
- package/dist/v1/libs/TinyCertCrypto.cjs +104 -45
- package/dist/v1/libs/TinyCertCrypto.d.mts +18 -16
- package/dist/v1/libs/TinyCertCrypto.mjs +68 -37
- package/dist/v1/libs/TinyCrypto.cjs +70 -33
- package/dist/v1/libs/TinyCrypto.d.mts +64 -26
- package/dist/v1/libs/TinyCrypto.mjs +54 -38
- package/package.json +1 -1
- package/webpack.config.mjs +0 -69
|
@@ -13,8 +13,8 @@ declare class TinyCrypto {
|
|
|
13
13
|
*
|
|
14
14
|
* @param {Object} [options={}] - Configuration options for encryption and decryption.
|
|
15
15
|
* @param {string} [options.algorithm='aes-256-gcm'] - The encryption algorithm to use. Recommended: 'aes-256-gcm' for authenticated encryption.
|
|
16
|
-
* @param {
|
|
17
|
-
* @param {
|
|
16
|
+
* @param {BufferEncoding} [options.outputEncoding='hex'] - The encoding used when returning encrypted data (e.g., 'hex', 'base64').
|
|
17
|
+
* @param {BufferEncoding} [options.inputEncoding='utf8'] - The encoding used for plaintext inputs (e.g., 'utf8').
|
|
18
18
|
* @param {number} [options.authTagLength=16] - The length of the authentication tag used in GCM mode. Usually 16 for AES-256-GCM.
|
|
19
19
|
* @param {Buffer} [options.key] - Optional 32-byte cryptographic key. If not provided, a random key is generated.
|
|
20
20
|
*
|
|
@@ -24,21 +24,23 @@ declare class TinyCrypto {
|
|
|
24
24
|
* const crypto = new CryptoManager({
|
|
25
25
|
* algorithm: 'aes-256-gcm',
|
|
26
26
|
* outputEncoding: 'base64',
|
|
27
|
-
* key:
|
|
27
|
+
* key: randomBytes(32),
|
|
28
28
|
* });
|
|
29
29
|
*/
|
|
30
30
|
constructor(options?: {
|
|
31
31
|
algorithm?: string | undefined;
|
|
32
|
-
outputEncoding?:
|
|
33
|
-
inputEncoding?:
|
|
32
|
+
outputEncoding?: BufferEncoding | undefined;
|
|
33
|
+
inputEncoding?: BufferEncoding | undefined;
|
|
34
34
|
authTagLength?: number | undefined;
|
|
35
35
|
key?: Buffer<ArrayBufferLike> | undefined;
|
|
36
36
|
});
|
|
37
37
|
algorithm: string;
|
|
38
|
-
outputEncoding: string;
|
|
39
|
-
inputEncoding: string;
|
|
40
38
|
authTagLength: number;
|
|
41
39
|
key: Buffer<ArrayBufferLike>;
|
|
40
|
+
/** @type {BufferEncoding} */
|
|
41
|
+
outputEncoding: BufferEncoding;
|
|
42
|
+
/** @type {BufferEncoding} */
|
|
43
|
+
inputEncoding: BufferEncoding;
|
|
42
44
|
/**
|
|
43
45
|
* Generates a secure random cryptographic key.
|
|
44
46
|
*
|
|
@@ -68,10 +70,7 @@ declare class TinyCrypto {
|
|
|
68
70
|
*
|
|
69
71
|
* @param {*} data - The data to encrypt. Can be of any supported type (string, number, boolean, Date, JSON, etc.).
|
|
70
72
|
* @param {Buffer} [iv=this.generateIV()] - Optional Initialization Vector (IV). If not provided, a secure random IV is generated.
|
|
71
|
-
* @returns {
|
|
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.
|
|
73
|
+
* @returns {EncryptedDataParams} An object containing the encrypted data.
|
|
75
74
|
*
|
|
76
75
|
* @example
|
|
77
76
|
* const result = cryptoManager.encrypt('Hello, world!');
|
|
@@ -81,17 +80,33 @@ declare class TinyCrypto {
|
|
|
81
80
|
* // authTag: 'aa93...'
|
|
82
81
|
* // }
|
|
83
82
|
*/
|
|
84
|
-
encrypt(data: any, iv?: Buffer):
|
|
83
|
+
encrypt(data: any, iv?: Buffer): {
|
|
84
|
+
/**
|
|
85
|
+
* - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
|
|
86
|
+
*/
|
|
87
|
+
iv: string;
|
|
88
|
+
/**
|
|
89
|
+
* - The encrypted data to decrypt, encoded with the output encoding.
|
|
90
|
+
*/
|
|
91
|
+
encrypted: string;
|
|
92
|
+
/**
|
|
93
|
+
* - The authentication tag used to verify the integrity of the encrypted data.
|
|
94
|
+
*/
|
|
95
|
+
authTag: string;
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* @typedef {Object} EncryptedDataParams
|
|
99
|
+
* @property {string} iv - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
|
|
100
|
+
* @property {string} encrypted - The encrypted data to decrypt, encoded with the output encoding.
|
|
101
|
+
* @property {string} authTag - The authentication tag used to verify the integrity of the encrypted data.
|
|
102
|
+
*/
|
|
85
103
|
/**
|
|
86
104
|
* Decrypts a previously encrypted value.
|
|
87
105
|
*
|
|
88
106
|
* The method checks the integrity of the data using the authentication tag (`authTag`) and ensures the data is properly decrypted.
|
|
89
107
|
* After decryption, it automatically deserializes the data back to its original type.
|
|
90
108
|
*
|
|
91
|
-
* @param {
|
|
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.
|
|
109
|
+
* @param {EncryptedDataParams} params - An object containing the encrypted data.
|
|
95
110
|
* @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
111
|
* @returns {*} The decrypted value, which will be the original type of the data before encryption.
|
|
97
112
|
* @throws {Error} Throws if the authentication tag doesn't match or the data has been tampered with.
|
|
@@ -106,17 +121,27 @@ declare class TinyCrypto {
|
|
|
106
121
|
* const decrypted = cryptoManager.decrypt(encryptedData, 'string');
|
|
107
122
|
* console.log(decrypted); // Outputs: 'Hello, world!'
|
|
108
123
|
*/
|
|
109
|
-
decrypt({ iv, encrypted, authTag }:
|
|
124
|
+
decrypt({ iv, encrypted, authTag }: {
|
|
125
|
+
/**
|
|
126
|
+
* - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
|
|
127
|
+
*/
|
|
128
|
+
iv: string;
|
|
129
|
+
/**
|
|
130
|
+
* - The encrypted data to decrypt, encoded with the output encoding.
|
|
131
|
+
*/
|
|
132
|
+
encrypted: string;
|
|
133
|
+
/**
|
|
134
|
+
* - The authentication tag used to verify the integrity of the encrypted data.
|
|
135
|
+
*/
|
|
136
|
+
authTag: string;
|
|
137
|
+
}, expectedType?: string | null): any;
|
|
110
138
|
/**
|
|
111
139
|
* Retrieves the type of the original data from an encrypted object.
|
|
112
140
|
*
|
|
113
141
|
* This method decrypts the encrypted data and extracts its type information without fully deserializing the value.
|
|
114
142
|
* It is useful when you need to verify the type of the encrypted data before fully decrypting it.
|
|
115
143
|
*
|
|
116
|
-
* @param {
|
|
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.
|
|
144
|
+
* @param {EncryptedDataParams} params - An object containing the encrypted data.
|
|
120
145
|
* @returns {string} The type of the original data (e.g., 'string', 'number', 'date', etc.).
|
|
121
146
|
*
|
|
122
147
|
* @example
|
|
@@ -128,7 +153,20 @@ declare class TinyCrypto {
|
|
|
128
153
|
* const dataType = cryptoManager.getTypeFromEncrypted(encryptedData);
|
|
129
154
|
* console.log(dataType); // Outputs: 'string'
|
|
130
155
|
*/
|
|
131
|
-
getTypeFromEncrypted({ iv, encrypted, authTag }:
|
|
156
|
+
getTypeFromEncrypted({ iv, encrypted, authTag }: {
|
|
157
|
+
/**
|
|
158
|
+
* - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
|
|
159
|
+
*/
|
|
160
|
+
iv: string;
|
|
161
|
+
/**
|
|
162
|
+
* - The encrypted data to decrypt, encoded with the output encoding.
|
|
163
|
+
*/
|
|
164
|
+
encrypted: string;
|
|
165
|
+
/**
|
|
166
|
+
* - The authentication tag used to verify the integrity of the encrypted data.
|
|
167
|
+
*/
|
|
168
|
+
authTag: string;
|
|
169
|
+
}): string;
|
|
132
170
|
/**
|
|
133
171
|
* Saves the cryptographic key to a file.
|
|
134
172
|
*
|
|
@@ -240,8 +278,8 @@ declare class TinyCrypto {
|
|
|
240
278
|
*
|
|
241
279
|
* @param {Object} config - The configuration object to import.
|
|
242
280
|
* @param {string} config.algorithm - The encryption algorithm (e.g., 'aes-256-gcm').
|
|
243
|
-
* @param {
|
|
244
|
-
* @param {
|
|
281
|
+
* @param {BufferEncoding} config.outputEncoding - The output encoding format (e.g., 'hex').
|
|
282
|
+
* @param {BufferEncoding} config.inputEncoding - The input encoding format (e.g., 'utf8').
|
|
245
283
|
* @param {number} config.authTagLength - The authentication tag length (e.g., 16).
|
|
246
284
|
* @param {string} config.key - The cryptographic key in hexadecimal string format.
|
|
247
285
|
*
|
|
@@ -258,8 +296,8 @@ declare class TinyCrypto {
|
|
|
258
296
|
*/
|
|
259
297
|
importConfig(config: {
|
|
260
298
|
algorithm: string;
|
|
261
|
-
outputEncoding:
|
|
262
|
-
inputEncoding:
|
|
299
|
+
outputEncoding: BufferEncoding;
|
|
300
|
+
inputEncoding: BufferEncoding;
|
|
263
301
|
authTagLength: number;
|
|
264
302
|
key: string;
|
|
265
303
|
}): void;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import fs from 'fs';
|
|
1
|
+
import { randomBytes, createDecipheriv, createCipheriv } from 'crypto';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
3
|
import { Buffer } from 'buffer';
|
|
4
4
|
import { objType } from '../../v1/basics/objFilter.mjs';
|
|
5
5
|
/**
|
|
@@ -26,8 +26,8 @@ class TinyCrypto {
|
|
|
26
26
|
*
|
|
27
27
|
* @param {Object} [options={}] - Configuration options for encryption and decryption.
|
|
28
28
|
* @param {string} [options.algorithm='aes-256-gcm'] - The encryption algorithm to use. Recommended: 'aes-256-gcm' for authenticated encryption.
|
|
29
|
-
* @param {
|
|
30
|
-
* @param {
|
|
29
|
+
* @param {BufferEncoding} [options.outputEncoding='hex'] - The encoding used when returning encrypted data (e.g., 'hex', 'base64').
|
|
30
|
+
* @param {BufferEncoding} [options.inputEncoding='utf8'] - The encoding used for plaintext inputs (e.g., 'utf8').
|
|
31
31
|
* @param {number} [options.authTagLength=16] - The length of the authentication tag used in GCM mode. Usually 16 for AES-256-GCM.
|
|
32
32
|
* @param {Buffer} [options.key] - Optional 32-byte cryptographic key. If not provided, a random key is generated.
|
|
33
33
|
*
|
|
@@ -37,15 +37,17 @@ class TinyCrypto {
|
|
|
37
37
|
* const crypto = new CryptoManager({
|
|
38
38
|
* algorithm: 'aes-256-gcm',
|
|
39
39
|
* outputEncoding: 'base64',
|
|
40
|
-
* key:
|
|
40
|
+
* key: randomBytes(32),
|
|
41
41
|
* });
|
|
42
42
|
*/
|
|
43
43
|
constructor(options = {}) {
|
|
44
44
|
this.algorithm = options.algorithm || 'aes-256-gcm';
|
|
45
|
-
this.outputEncoding = options.outputEncoding || 'hex';
|
|
46
|
-
this.inputEncoding = options.inputEncoding || 'utf8';
|
|
47
45
|
this.authTagLength = options.authTagLength || 16;
|
|
48
46
|
this.key = options.key || this.generateKey();
|
|
47
|
+
/** @type {BufferEncoding} */
|
|
48
|
+
this.outputEncoding = options.outputEncoding || 'hex';
|
|
49
|
+
/** @type {BufferEncoding} */
|
|
50
|
+
this.inputEncoding = options.inputEncoding || 'utf8';
|
|
49
51
|
}
|
|
50
52
|
/**
|
|
51
53
|
* Generates a secure random cryptographic key.
|
|
@@ -58,7 +60,7 @@ class TinyCrypto {
|
|
|
58
60
|
* const customKey = cryptoManager.generateKey(16); // Generates a 16-byte key (e.g. for AES-128)
|
|
59
61
|
*/
|
|
60
62
|
generateKey(value = 32) {
|
|
61
|
-
return
|
|
63
|
+
return randomBytes(value); // 256-bit
|
|
62
64
|
}
|
|
63
65
|
/**
|
|
64
66
|
* Generates a secure random Initialization Vector (IV).
|
|
@@ -71,7 +73,7 @@ class TinyCrypto {
|
|
|
71
73
|
* const customIV = cryptoManager.generateIV(16); // Generates a 16-byte IV if needed for other algorithms
|
|
72
74
|
*/
|
|
73
75
|
generateIV(value = 12) {
|
|
74
|
-
return
|
|
76
|
+
return randomBytes(value); // 96-bit padrão para GCM
|
|
75
77
|
}
|
|
76
78
|
/**
|
|
77
79
|
* Encrypts a given value (string, number, object, etc.)
|
|
@@ -80,10 +82,7 @@ class TinyCrypto {
|
|
|
80
82
|
*
|
|
81
83
|
* @param {*} data - The data to encrypt. Can be of any supported type (string, number, boolean, Date, JSON, etc.).
|
|
82
84
|
* @param {Buffer} [iv=this.generateIV()] - Optional Initialization Vector (IV). If not provided, a secure random IV is generated.
|
|
83
|
-
* @returns {
|
|
84
|
-
* - `iv` {string} - The IV used, encoded with the output encoding.
|
|
85
|
-
* - `encrypted` {string} - The encrypted payload.
|
|
86
|
-
* - `authTag` {string} - The authentication tag used to verify the integrity of the ciphertext.
|
|
85
|
+
* @returns {EncryptedDataParams} An object containing the encrypted data.
|
|
87
86
|
*
|
|
88
87
|
* @example
|
|
89
88
|
* const result = cryptoManager.encrypt('Hello, world!');
|
|
@@ -95,28 +94,37 @@ class TinyCrypto {
|
|
|
95
94
|
*/
|
|
96
95
|
encrypt(data, iv = this.generateIV()) {
|
|
97
96
|
const plainText = this.#serialize(data);
|
|
98
|
-
const cipher =
|
|
97
|
+
const cipher = createCipheriv(this.algorithm, this.key, iv, {
|
|
98
|
+
// @ts-ignore
|
|
99
99
|
authTagLength: this.authTagLength,
|
|
100
100
|
});
|
|
101
|
+
// @ts-ignore
|
|
101
102
|
let encrypted = cipher.update(plainText, this.inputEncoding);
|
|
103
|
+
// @ts-ignore
|
|
102
104
|
encrypted = Buffer.concat([encrypted, cipher.final()]);
|
|
103
105
|
const authTag = cipher.getAuthTag();
|
|
104
106
|
return {
|
|
107
|
+
// @ts-ignore
|
|
105
108
|
iv: iv.toString(this.outputEncoding),
|
|
109
|
+
// @ts-ignore
|
|
106
110
|
encrypted: encrypted.toString(this.outputEncoding),
|
|
111
|
+
// @ts-ignore
|
|
107
112
|
authTag: authTag.toString(this.outputEncoding),
|
|
108
113
|
};
|
|
109
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* @typedef {Object} EncryptedDataParams
|
|
117
|
+
* @property {string} iv - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
|
|
118
|
+
* @property {string} encrypted - The encrypted data to decrypt, encoded with the output encoding.
|
|
119
|
+
* @property {string} authTag - The authentication tag used to verify the integrity of the encrypted data.
|
|
120
|
+
*/
|
|
110
121
|
/**
|
|
111
122
|
* Decrypts a previously encrypted value.
|
|
112
123
|
*
|
|
113
124
|
* The method checks the integrity of the data using the authentication tag (`authTag`) and ensures the data is properly decrypted.
|
|
114
125
|
* After decryption, it automatically deserializes the data back to its original type.
|
|
115
126
|
*
|
|
116
|
-
* @param {
|
|
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.
|
|
127
|
+
* @param {EncryptedDataParams} params - An object containing the encrypted data.
|
|
120
128
|
* @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.
|
|
121
129
|
* @returns {*} The decrypted value, which will be the original type of the data before encryption.
|
|
122
130
|
* @throws {Error} Throws if the authentication tag doesn't match or the data has been tampered with.
|
|
@@ -135,11 +143,15 @@ class TinyCrypto {
|
|
|
135
143
|
const ivBuffer = Buffer.from(iv, this.outputEncoding);
|
|
136
144
|
const encryptedBuffer = Buffer.from(encrypted, this.outputEncoding);
|
|
137
145
|
const authTagBuffer = Buffer.from(authTag, this.outputEncoding);
|
|
138
|
-
const decipher =
|
|
146
|
+
const decipher = createDecipheriv(this.algorithm, this.key, ivBuffer, {
|
|
147
|
+
// @ts-ignore
|
|
139
148
|
authTagLength: this.authTagLength,
|
|
140
149
|
});
|
|
141
150
|
decipher.setAuthTag(authTagBuffer);
|
|
151
|
+
/** @type {string} */
|
|
152
|
+
// @ts-ignore
|
|
142
153
|
let decrypted = decipher.update(encryptedBuffer, null, this.inputEncoding);
|
|
154
|
+
// @ts-ignore
|
|
143
155
|
decrypted += decipher.final(this.inputEncoding);
|
|
144
156
|
const { value, type } = this.#deserialize(decrypted);
|
|
145
157
|
if (expectedType)
|
|
@@ -152,10 +164,7 @@ class TinyCrypto {
|
|
|
152
164
|
* This method decrypts the encrypted data and extracts its type information without fully deserializing the value.
|
|
153
165
|
* It is useful when you need to verify the type of the encrypted data before fully decrypting it.
|
|
154
166
|
*
|
|
155
|
-
* @param {
|
|
156
|
-
* - `iv` {string} - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
|
|
157
|
-
* - `encrypted` {string} - The encrypted data to decrypt, encoded with the output encoding.
|
|
158
|
-
* - `authTag` {string} - The authentication tag used to verify the integrity of the encrypted data.
|
|
167
|
+
* @param {EncryptedDataParams} params - An object containing the encrypted data.
|
|
159
168
|
* @returns {string} The type of the original data (e.g., 'string', 'number', 'date', etc.).
|
|
160
169
|
*
|
|
161
170
|
* @example
|
|
@@ -171,11 +180,14 @@ class TinyCrypto {
|
|
|
171
180
|
const ivBuffer = Buffer.from(iv, this.outputEncoding);
|
|
172
181
|
const encryptedBuffer = Buffer.from(encrypted, this.outputEncoding);
|
|
173
182
|
const authTagBuffer = Buffer.from(authTag, this.outputEncoding);
|
|
174
|
-
const decipher =
|
|
183
|
+
const decipher = createDecipheriv(this.algorithm, this.key, ivBuffer, {
|
|
184
|
+
// @ts-ignore
|
|
175
185
|
authTagLength: this.authTagLength,
|
|
176
186
|
});
|
|
177
187
|
decipher.setAuthTag(authTagBuffer);
|
|
188
|
+
// @ts-ignore
|
|
178
189
|
let decrypted = decipher.update(encryptedBuffer, null, this.inputEncoding);
|
|
190
|
+
// @ts-ignore
|
|
179
191
|
decrypted += decipher.final(this.inputEncoding);
|
|
180
192
|
const { type } = this.#deserialize(decrypted);
|
|
181
193
|
return typeof type === 'string' ? type : 'unknown';
|
|
@@ -265,10 +277,12 @@ class TinyCrypto {
|
|
|
265
277
|
async loadConfigFromFile(file) {
|
|
266
278
|
if (isBrowser) {
|
|
267
279
|
return new Promise((resolve, reject) => {
|
|
280
|
+
if (!(file instanceof File))
|
|
281
|
+
return reject(new Error('In browser, the file must be a File object'));
|
|
268
282
|
const reader = new FileReader();
|
|
269
283
|
reader.onload = () => {
|
|
270
284
|
try {
|
|
271
|
-
const config = JSON.parse(reader.result);
|
|
285
|
+
const config = typeof reader.result === 'string' ? JSON.parse(reader.result) : {};
|
|
272
286
|
resolve(this.importConfig(config));
|
|
273
287
|
}
|
|
274
288
|
catch (err) {
|
|
@@ -280,7 +294,7 @@ class TinyCrypto {
|
|
|
280
294
|
});
|
|
281
295
|
}
|
|
282
296
|
else {
|
|
283
|
-
const raw = fs.readFileSync(file, 'utf8');
|
|
297
|
+
const raw = fs.readFileSync(/** @type {string} */ (file), 'utf8');
|
|
284
298
|
const config = JSON.parse(raw);
|
|
285
299
|
return this.importConfig(config);
|
|
286
300
|
}
|
|
@@ -311,9 +325,11 @@ class TinyCrypto {
|
|
|
311
325
|
async loadKeyFromFile(file) {
|
|
312
326
|
if (isBrowser) {
|
|
313
327
|
return new Promise((resolve, reject) => {
|
|
328
|
+
if (!(file instanceof File))
|
|
329
|
+
return reject(new Error('In browser, the file must be a File object'));
|
|
314
330
|
const reader = new FileReader();
|
|
315
331
|
reader.onload = () => {
|
|
316
|
-
const hexKey = reader.result.trim();
|
|
332
|
+
const hexKey = typeof reader.result === 'string' ? reader.result.trim() : '';
|
|
317
333
|
const keyBuffer = Buffer.from(hexKey, 'hex');
|
|
318
334
|
this.key = keyBuffer;
|
|
319
335
|
resolve(keyBuffer);
|
|
@@ -323,7 +339,7 @@ class TinyCrypto {
|
|
|
323
339
|
});
|
|
324
340
|
}
|
|
325
341
|
else {
|
|
326
|
-
const hexKey = fs.readFileSync(file, 'utf8');
|
|
342
|
+
const hexKey = fs.readFileSync(/** @type {string} */ (file), 'utf8');
|
|
327
343
|
const keyBuffer = Buffer.from(hexKey, 'hex');
|
|
328
344
|
this.key = keyBuffer;
|
|
329
345
|
return keyBuffer;
|
|
@@ -367,8 +383,8 @@ class TinyCrypto {
|
|
|
367
383
|
*
|
|
368
384
|
* @param {Object} config - The configuration object to import.
|
|
369
385
|
* @param {string} config.algorithm - The encryption algorithm (e.g., 'aes-256-gcm').
|
|
370
|
-
* @param {
|
|
371
|
-
* @param {
|
|
386
|
+
* @param {BufferEncoding} config.outputEncoding - The output encoding format (e.g., 'hex').
|
|
387
|
+
* @param {BufferEncoding} config.inputEncoding - The input encoding format (e.g., 'utf8').
|
|
372
388
|
* @param {number} config.authTagLength - The authentication tag length (e.g., 16).
|
|
373
389
|
* @param {string} config.key - The cryptographic key in hexadecimal string format.
|
|
374
390
|
*
|
|
@@ -415,7 +431,7 @@ class TinyCrypto {
|
|
|
415
431
|
* Each key corresponds to a specific data type (e.g., 'number', 'date', 'buffer', etc.),
|
|
416
432
|
* and the value is a function that serializes the data to a specific format.
|
|
417
433
|
*
|
|
418
|
-
* @type {
|
|
434
|
+
* @type {Record<string, (data: any) => string>}
|
|
419
435
|
* @property {Function} weakmap - Throws an error as WeakMap cannot be serialized.
|
|
420
436
|
* @property {Function} weakset - Throws an error as WeakSet cannot be serialized.
|
|
421
437
|
* @property {Function} promise - Throws an error as Promise cannot be serialized.
|
|
@@ -456,8 +472,8 @@ class TinyCrypto {
|
|
|
456
472
|
number: (data) => JSON.stringify({ __type: 'number', value: data }),
|
|
457
473
|
boolean: (data) => JSON.stringify({ __type: 'boolean', value: data }),
|
|
458
474
|
string: (data) => JSON.stringify({ __type: 'string', value: data }),
|
|
459
|
-
null: (
|
|
460
|
-
undefined: (
|
|
475
|
+
null: () => JSON.stringify({ __type: 'null' }),
|
|
476
|
+
undefined: () => JSON.stringify({ __type: 'undefined' }),
|
|
461
477
|
map: (data) => JSON.stringify({
|
|
462
478
|
__type: 'map',
|
|
463
479
|
value: Array.from(data.entries()),
|
|
@@ -479,7 +495,7 @@ class TinyCrypto {
|
|
|
479
495
|
* Each key corresponds to a specific data type (e.g., 'Date', 'BigInt', 'Buffer', etc.),
|
|
480
496
|
* and the value is a function that deserializes the value to its original format.
|
|
481
497
|
*
|
|
482
|
-
* @type {
|
|
498
|
+
* @type {Record<string, (data: any) => any>}
|
|
483
499
|
* @property {Function} regexp - Deserializes a regular expression from its string representation (e.g., `/pattern/flags`).
|
|
484
500
|
* @property {Function} htmlelement - Deserializes an HTML element from its serialized outerHTML string (only works in browser environments).
|
|
485
501
|
* @property {Function} date - Deserializes a date from its ISO string representation.
|
|
@@ -506,14 +522,14 @@ class TinyCrypto {
|
|
|
506
522
|
throw new Error('HTMLElement deserialization is only supported in browsers');
|
|
507
523
|
const div = document.createElement('div');
|
|
508
524
|
div.innerHTML = value;
|
|
509
|
-
return div
|
|
525
|
+
return div;
|
|
510
526
|
},
|
|
511
527
|
date: (value) => new Date(value),
|
|
512
528
|
bigint: (value) => BigInt(value),
|
|
513
529
|
number: (value) => Number(value),
|
|
514
530
|
boolean: (value) => Boolean(value),
|
|
515
|
-
null: (
|
|
516
|
-
undefined: (
|
|
531
|
+
null: () => null,
|
|
532
|
+
undefined: () => undefined,
|
|
517
533
|
map: (value) => new Map(value),
|
|
518
534
|
set: (value) => new Set(value),
|
|
519
535
|
symbol: (value) => Symbol(value),
|
|
@@ -533,7 +549,7 @@ class TinyCrypto {
|
|
|
533
549
|
*/
|
|
534
550
|
#serialize(data) {
|
|
535
551
|
const type = objType(data) || 'undefined';
|
|
536
|
-
if (this.#valueConvertTypes[type])
|
|
552
|
+
if (typeof type === 'string' && this.#valueConvertTypes[type])
|
|
537
553
|
return this.#valueConvertTypes[type](data);
|
|
538
554
|
throw new Error(`Unsupported data type for encryption: ${type}`);
|
|
539
555
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tiny-essentials",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "npm run test:mjs && npm run test:cjs && npm run test:js",
|
package/webpack.config.mjs
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import { fileURLToPath } from 'url';
|
|
3
|
-
import webpack from 'webpack';
|
|
4
|
-
import NodePolyfillPlugin from 'node-polyfill-webpack-plugin';
|
|
5
|
-
|
|
6
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
7
|
-
const __dirname = path.dirname(__filename);
|
|
8
|
-
|
|
9
|
-
// Add modules
|
|
10
|
-
const modules = [];
|
|
11
|
-
const addModule = (entry, library, isClass = false) => {
|
|
12
|
-
const baseConfig = {
|
|
13
|
-
entry,
|
|
14
|
-
output: {
|
|
15
|
-
path: path.resolve(__dirname, 'dist'),
|
|
16
|
-
library,
|
|
17
|
-
libraryTarget: 'window',
|
|
18
|
-
libraryExport: isClass ? library : undefined,
|
|
19
|
-
},
|
|
20
|
-
optimization: {
|
|
21
|
-
runtimeChunk: false,
|
|
22
|
-
splitChunks: false,
|
|
23
|
-
},
|
|
24
|
-
plugins: [
|
|
25
|
-
new NodePolyfillPlugin(),
|
|
26
|
-
new webpack.ProvidePlugin({
|
|
27
|
-
process: 'process/browser',
|
|
28
|
-
}),
|
|
29
|
-
],
|
|
30
|
-
};
|
|
31
|
-
modules.push(
|
|
32
|
-
// Non-minified version
|
|
33
|
-
{
|
|
34
|
-
...baseConfig,
|
|
35
|
-
mode: 'development',
|
|
36
|
-
output: {
|
|
37
|
-
...baseConfig.output,
|
|
38
|
-
filename: `${library}.js`,
|
|
39
|
-
},
|
|
40
|
-
optimization: {
|
|
41
|
-
...baseConfig.optimization,
|
|
42
|
-
minimize: false,
|
|
43
|
-
},
|
|
44
|
-
},
|
|
45
|
-
// Minified version
|
|
46
|
-
{
|
|
47
|
-
...baseConfig,
|
|
48
|
-
mode: 'production',
|
|
49
|
-
output: {
|
|
50
|
-
...baseConfig.output,
|
|
51
|
-
filename: `${library}.min.js`,
|
|
52
|
-
},
|
|
53
|
-
optimization: {
|
|
54
|
-
...baseConfig.optimization,
|
|
55
|
-
minimize: true,
|
|
56
|
-
},
|
|
57
|
-
},
|
|
58
|
-
);
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
// Main
|
|
62
|
-
addModule('./src/v1/index.mjs', 'TinyEssentials');
|
|
63
|
-
addModule('./src/v1/basics/index.mjs', 'TinyBasicsEs');
|
|
64
|
-
|
|
65
|
-
addModule('./src/v1/build/TinyLevelUp.mjs', 'TinyLevelUp', true);
|
|
66
|
-
addModule('./src/v1/build/TinyCertCrypto.mjs', 'TinyCertCrypto', true);
|
|
67
|
-
addModule('./src/v1/build/TinyCrypto.mjs', 'TinyCrypto', true);
|
|
68
|
-
|
|
69
|
-
export default modules;
|