tiny-essentials 1.1.1 → 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/README.md +3 -1
- package/dist/TinyBasicsEs.js +2821 -0
- package/dist/TinyBasicsEs.min.js +2 -0
- package/dist/TinyBasicsEs.min.js.LICENSE.txt +8 -0
- package/dist/TinyCertCrypto.js +31364 -0
- package/dist/TinyCertCrypto.min.js +2 -0
- package/dist/TinyCertCrypto.min.js.LICENSE.txt +8 -0
- package/dist/TinyCrypto.js +48138 -0
- package/dist/TinyCrypto.min.js +2 -0
- package/dist/TinyCrypto.min.js.LICENSE.txt +10 -0
- package/dist/TinyEssentials.js +77695 -0
- package/dist/TinyEssentials.min.js +2 -1
- package/dist/TinyEssentials.min.js.LICENSE.txt +10 -0
- package/dist/TinyLevelUp.js +173 -0
- package/dist/TinyLevelUp.min.js +1 -0
- 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/index.cjs +27 -0
- package/dist/v1/basics/index.d.mts +17 -0
- package/dist/v1/basics/index.mjs +7 -0
- package/dist/v1/basics/objFilter.cjs +7 -1
- package/dist/v1/basics/objFilter.mjs +4 -0
- package/dist/v1/basics/simpleMath.cjs +1 -1
- package/dist/v1/basics/simpleMath.mjs +1 -1
- package/dist/v1/build/TinyCertCrypto.cjs +7 -0
- package/dist/v1/build/TinyCertCrypto.d.mts +2 -0
- package/dist/v1/build/TinyCertCrypto.mjs +2 -0
- package/dist/v1/build/TinyCrypto.cjs +7 -0
- package/dist/v1/build/TinyCrypto.d.mts +2 -0
- package/dist/v1/build/TinyCrypto.mjs +2 -0
- package/dist/v1/build/TinyLevelUp.cjs +7 -0
- package/dist/v1/build/TinyLevelUp.d.mts +2 -0
- package/dist/v1/build/TinyLevelUp.mjs +2 -0
- package/dist/v1/index.cjs +2 -0
- package/dist/v1/index.d.mts +2 -1
- package/dist/v1/index.mjs +2 -1
- package/dist/v1/libs/TinyCertCrypto.cjs +573 -0
- package/dist/v1/libs/TinyCertCrypto.d.mts +193 -0
- package/dist/v1/libs/TinyCertCrypto.mjs +481 -0
- package/dist/v1/libs/TinyCrypto.cjs +91 -45
- package/dist/v1/libs/TinyCrypto.d.mts +65 -26
- package/dist/v1/libs/TinyCrypto.mjs +64 -39
- package/docs/README.md +2 -0
- package/docs/libs/TinyCertCrypto.md +202 -0
- package/package.json +11 -6
|
@@ -2,9 +2,37 @@
|
|
|
2
2
|
|
|
3
3
|
var crypto = require('crypto');
|
|
4
4
|
var fs = require('fs');
|
|
5
|
+
var buffer = require('buffer');
|
|
5
6
|
var objFilter = require('../basics/objFilter.cjs');
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
function _interopNamespaceDefault(e) {
|
|
9
|
+
var n = Object.create(null);
|
|
10
|
+
if (e) {
|
|
11
|
+
Object.keys(e).forEach(function (k) {
|
|
12
|
+
if (k !== 'default') {
|
|
13
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
14
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: function () { return e[k]; }
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
n.default = e;
|
|
22
|
+
return Object.freeze(n);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Determines if the environment is a browser.
|
|
29
|
+
*
|
|
30
|
+
* This constant checks if the code is running in a browser environment by verifying if
|
|
31
|
+
* the `window` object and the `window.document` object are available. It will return `true`
|
|
32
|
+
* if the environment is a browser, and `false` otherwise (e.g., in a Node.js environment).
|
|
33
|
+
*
|
|
34
|
+
* @constant {boolean}
|
|
35
|
+
*/
|
|
8
36
|
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
|
|
9
37
|
|
|
10
38
|
/**
|
|
@@ -21,8 +49,8 @@ class TinyCrypto {
|
|
|
21
49
|
*
|
|
22
50
|
* @param {Object} [options={}] - Configuration options for encryption and decryption.
|
|
23
51
|
* @param {string} [options.algorithm='aes-256-gcm'] - The encryption algorithm to use. Recommended: 'aes-256-gcm' for authenticated encryption.
|
|
24
|
-
* @param {
|
|
25
|
-
* @param {
|
|
52
|
+
* @param {BufferEncoding} [options.outputEncoding='hex'] - The encoding used when returning encrypted data (e.g., 'hex', 'base64').
|
|
53
|
+
* @param {BufferEncoding} [options.inputEncoding='utf8'] - The encoding used for plaintext inputs (e.g., 'utf8').
|
|
26
54
|
* @param {number} [options.authTagLength=16] - The length of the authentication tag used in GCM mode. Usually 16 for AES-256-GCM.
|
|
27
55
|
* @param {Buffer} [options.key] - Optional 32-byte cryptographic key. If not provided, a random key is generated.
|
|
28
56
|
*
|
|
@@ -32,15 +60,17 @@ class TinyCrypto {
|
|
|
32
60
|
* const crypto = new CryptoManager({
|
|
33
61
|
* algorithm: 'aes-256-gcm',
|
|
34
62
|
* outputEncoding: 'base64',
|
|
35
|
-
* key:
|
|
63
|
+
* key: randomBytes(32),
|
|
36
64
|
* });
|
|
37
65
|
*/
|
|
38
66
|
constructor(options = {}) {
|
|
39
67
|
this.algorithm = options.algorithm || 'aes-256-gcm';
|
|
40
|
-
this.outputEncoding = options.outputEncoding || 'hex';
|
|
41
|
-
this.inputEncoding = options.inputEncoding || 'utf8';
|
|
42
68
|
this.authTagLength = options.authTagLength || 16;
|
|
43
69
|
this.key = options.key || this.generateKey();
|
|
70
|
+
/** @type {BufferEncoding} */
|
|
71
|
+
this.outputEncoding = options.outputEncoding || 'hex';
|
|
72
|
+
/** @type {BufferEncoding} */
|
|
73
|
+
this.inputEncoding = options.inputEncoding || 'utf8';
|
|
44
74
|
}
|
|
45
75
|
|
|
46
76
|
/**
|
|
@@ -78,10 +108,7 @@ class TinyCrypto {
|
|
|
78
108
|
*
|
|
79
109
|
* @param {*} data - The data to encrypt. Can be of any supported type (string, number, boolean, Date, JSON, etc.).
|
|
80
110
|
* @param {Buffer} [iv=this.generateIV()] - Optional Initialization Vector (IV). If not provided, a secure random IV is generated.
|
|
81
|
-
* @returns {
|
|
82
|
-
* - `iv` {string} - The IV used, encoded with the output encoding.
|
|
83
|
-
* - `encrypted` {string} - The encrypted payload.
|
|
84
|
-
* - `authTag` {string} - The authentication tag used to verify the integrity of the ciphertext.
|
|
111
|
+
* @returns {EncryptedDataParams} An object containing the encrypted data.
|
|
85
112
|
*
|
|
86
113
|
* @example
|
|
87
114
|
* const result = cryptoManager.encrypt('Hello, world!');
|
|
@@ -95,30 +122,40 @@ class TinyCrypto {
|
|
|
95
122
|
const plainText = this.#serialize(data);
|
|
96
123
|
|
|
97
124
|
const cipher = crypto.createCipheriv(this.algorithm, this.key, iv, {
|
|
125
|
+
// @ts-ignore
|
|
98
126
|
authTagLength: this.authTagLength,
|
|
99
127
|
});
|
|
100
128
|
|
|
129
|
+
// @ts-ignore
|
|
101
130
|
let encrypted = cipher.update(plainText, this.inputEncoding);
|
|
102
|
-
|
|
131
|
+
// @ts-ignore
|
|
132
|
+
encrypted = buffer.Buffer.concat([encrypted, cipher.final()]);
|
|
103
133
|
const authTag = cipher.getAuthTag();
|
|
104
134
|
|
|
105
135
|
return {
|
|
136
|
+
// @ts-ignore
|
|
106
137
|
iv: iv.toString(this.outputEncoding),
|
|
138
|
+
// @ts-ignore
|
|
107
139
|
encrypted: encrypted.toString(this.outputEncoding),
|
|
140
|
+
// @ts-ignore
|
|
108
141
|
authTag: authTag.toString(this.outputEncoding),
|
|
109
142
|
};
|
|
110
143
|
}
|
|
111
144
|
|
|
145
|
+
/**
|
|
146
|
+
* @typedef {Object} EncryptedDataParams
|
|
147
|
+
* @property {string} iv - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
|
|
148
|
+
* @property {string} encrypted - The encrypted data to decrypt, encoded with the output encoding.
|
|
149
|
+
* @property {string} authTag - The authentication tag used to verify the integrity of the encrypted data.
|
|
150
|
+
*/
|
|
151
|
+
|
|
112
152
|
/**
|
|
113
153
|
* Decrypts a previously encrypted value.
|
|
114
154
|
*
|
|
115
155
|
* The method checks the integrity of the data using the authentication tag (`authTag`) and ensures the data is properly decrypted.
|
|
116
156
|
* After decryption, it automatically deserializes the data back to its original type.
|
|
117
157
|
*
|
|
118
|
-
* @param {
|
|
119
|
-
* - `iv` {string} - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
|
|
120
|
-
* - `encrypted` {string} - The encrypted data to decrypt, encoded with the output encoding.
|
|
121
|
-
* - `authTag` {string} - The authentication tag used to verify the integrity of the encrypted data.
|
|
158
|
+
* @param {EncryptedDataParams} params - An object containing the encrypted data.
|
|
122
159
|
* @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.
|
|
123
160
|
* @returns {*} The decrypted value, which will be the original type of the data before encryption.
|
|
124
161
|
* @throws {Error} Throws if the authentication tag doesn't match or the data has been tampered with.
|
|
@@ -134,17 +171,21 @@ class TinyCrypto {
|
|
|
134
171
|
* console.log(decrypted); // Outputs: 'Hello, world!'
|
|
135
172
|
*/
|
|
136
173
|
decrypt({ iv, encrypted, authTag }, expectedType = null) {
|
|
137
|
-
const ivBuffer = Buffer.from(iv, this.outputEncoding);
|
|
138
|
-
const encryptedBuffer = Buffer.from(encrypted, this.outputEncoding);
|
|
139
|
-
const authTagBuffer = Buffer.from(authTag, this.outputEncoding);
|
|
174
|
+
const ivBuffer = buffer.Buffer.from(iv, this.outputEncoding);
|
|
175
|
+
const encryptedBuffer = buffer.Buffer.from(encrypted, this.outputEncoding);
|
|
176
|
+
const authTagBuffer = buffer.Buffer.from(authTag, this.outputEncoding);
|
|
140
177
|
|
|
141
178
|
const decipher = crypto.createDecipheriv(this.algorithm, this.key, ivBuffer, {
|
|
179
|
+
// @ts-ignore
|
|
142
180
|
authTagLength: this.authTagLength,
|
|
143
181
|
});
|
|
144
182
|
|
|
145
183
|
decipher.setAuthTag(authTagBuffer);
|
|
146
184
|
|
|
185
|
+
/** @type {string} */
|
|
186
|
+
// @ts-ignore
|
|
147
187
|
let decrypted = decipher.update(encryptedBuffer, null, this.inputEncoding);
|
|
188
|
+
// @ts-ignore
|
|
148
189
|
decrypted += decipher.final(this.inputEncoding);
|
|
149
190
|
const { value, type } = this.#deserialize(decrypted);
|
|
150
191
|
|
|
@@ -158,10 +199,7 @@ class TinyCrypto {
|
|
|
158
199
|
* This method decrypts the encrypted data and extracts its type information without fully deserializing the value.
|
|
159
200
|
* It is useful when you need to verify the type of the encrypted data before fully decrypting it.
|
|
160
201
|
*
|
|
161
|
-
* @param {
|
|
162
|
-
* - `iv` {string} - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
|
|
163
|
-
* - `encrypted` {string} - The encrypted data to decrypt, encoded with the output encoding.
|
|
164
|
-
* - `authTag` {string} - The authentication tag used to verify the integrity of the encrypted data.
|
|
202
|
+
* @param {EncryptedDataParams} params - An object containing the encrypted data.
|
|
165
203
|
* @returns {string} The type of the original data (e.g., 'string', 'number', 'date', etc.).
|
|
166
204
|
*
|
|
167
205
|
* @example
|
|
@@ -174,17 +212,20 @@ class TinyCrypto {
|
|
|
174
212
|
* console.log(dataType); // Outputs: 'string'
|
|
175
213
|
*/
|
|
176
214
|
getTypeFromEncrypted({ iv, encrypted, authTag }) {
|
|
177
|
-
const ivBuffer = Buffer.from(iv, this.outputEncoding);
|
|
178
|
-
const encryptedBuffer = Buffer.from(encrypted, this.outputEncoding);
|
|
179
|
-
const authTagBuffer = Buffer.from(authTag, this.outputEncoding);
|
|
215
|
+
const ivBuffer = buffer.Buffer.from(iv, this.outputEncoding);
|
|
216
|
+
const encryptedBuffer = buffer.Buffer.from(encrypted, this.outputEncoding);
|
|
217
|
+
const authTagBuffer = buffer.Buffer.from(authTag, this.outputEncoding);
|
|
180
218
|
|
|
181
219
|
const decipher = crypto.createDecipheriv(this.algorithm, this.key, ivBuffer, {
|
|
220
|
+
// @ts-ignore
|
|
182
221
|
authTagLength: this.authTagLength,
|
|
183
222
|
});
|
|
184
223
|
|
|
185
224
|
decipher.setAuthTag(authTagBuffer);
|
|
186
225
|
|
|
226
|
+
// @ts-ignore
|
|
187
227
|
let decrypted = decipher.update(encryptedBuffer, null, this.inputEncoding);
|
|
228
|
+
// @ts-ignore
|
|
188
229
|
decrypted += decipher.final(this.inputEncoding);
|
|
189
230
|
|
|
190
231
|
const { type } = this.#deserialize(decrypted);
|
|
@@ -217,7 +258,7 @@ class TinyCrypto {
|
|
|
217
258
|
a.download = filename;
|
|
218
259
|
a.click();
|
|
219
260
|
URL.revokeObjectURL(url);
|
|
220
|
-
} else
|
|
261
|
+
} else fs__namespace.writeFileSync(filename, data);
|
|
221
262
|
}
|
|
222
263
|
|
|
223
264
|
/**
|
|
@@ -246,7 +287,7 @@ class TinyCrypto {
|
|
|
246
287
|
a.download = filename;
|
|
247
288
|
a.click();
|
|
248
289
|
URL.revokeObjectURL(url);
|
|
249
|
-
} else
|
|
290
|
+
} else fs__namespace.writeFileSync(filename, configData);
|
|
250
291
|
}
|
|
251
292
|
|
|
252
293
|
/**
|
|
@@ -274,10 +315,12 @@ class TinyCrypto {
|
|
|
274
315
|
async loadConfigFromFile(file) {
|
|
275
316
|
if (isBrowser) {
|
|
276
317
|
return new Promise((resolve, reject) => {
|
|
318
|
+
if (!(file instanceof File))
|
|
319
|
+
return reject(new Error('In browser, the file must be a File object'));
|
|
277
320
|
const reader = new FileReader();
|
|
278
321
|
reader.onload = () => {
|
|
279
322
|
try {
|
|
280
|
-
const config = JSON.parse(reader.result);
|
|
323
|
+
const config = typeof reader.result === 'string' ? JSON.parse(reader.result) : {};
|
|
281
324
|
resolve(this.importConfig(config));
|
|
282
325
|
} catch (err) {
|
|
283
326
|
reject(new Error('Invalid config JSON file'));
|
|
@@ -287,7 +330,7 @@ class TinyCrypto {
|
|
|
287
330
|
reader.readAsText(file);
|
|
288
331
|
});
|
|
289
332
|
} else {
|
|
290
|
-
const raw =
|
|
333
|
+
const raw = fs__namespace.readFileSync(/** @type {string} */ (file), 'utf8');
|
|
291
334
|
const config = JSON.parse(raw);
|
|
292
335
|
return this.importConfig(config);
|
|
293
336
|
}
|
|
@@ -319,10 +362,12 @@ class TinyCrypto {
|
|
|
319
362
|
async loadKeyFromFile(file) {
|
|
320
363
|
if (isBrowser) {
|
|
321
364
|
return new Promise((resolve, reject) => {
|
|
365
|
+
if (!(file instanceof File))
|
|
366
|
+
return reject(new Error('In browser, the file must be a File object'));
|
|
322
367
|
const reader = new FileReader();
|
|
323
368
|
reader.onload = () => {
|
|
324
|
-
const hexKey = reader.result.trim();
|
|
325
|
-
const keyBuffer = Buffer.from(hexKey, 'hex');
|
|
369
|
+
const hexKey = typeof reader.result === 'string' ? reader.result.trim() : '';
|
|
370
|
+
const keyBuffer = buffer.Buffer.from(hexKey, 'hex');
|
|
326
371
|
this.key = keyBuffer;
|
|
327
372
|
resolve(keyBuffer);
|
|
328
373
|
};
|
|
@@ -330,8 +375,8 @@ class TinyCrypto {
|
|
|
330
375
|
reader.readAsText(file);
|
|
331
376
|
});
|
|
332
377
|
} else {
|
|
333
|
-
const hexKey =
|
|
334
|
-
const keyBuffer = Buffer.from(hexKey, 'hex');
|
|
378
|
+
const hexKey = fs__namespace.readFileSync(/** @type {string} */ (file), 'utf8');
|
|
379
|
+
const keyBuffer = buffer.Buffer.from(hexKey, 'hex');
|
|
335
380
|
this.key = keyBuffer;
|
|
336
381
|
return keyBuffer;
|
|
337
382
|
}
|
|
@@ -376,8 +421,8 @@ class TinyCrypto {
|
|
|
376
421
|
*
|
|
377
422
|
* @param {Object} config - The configuration object to import.
|
|
378
423
|
* @param {string} config.algorithm - The encryption algorithm (e.g., 'aes-256-gcm').
|
|
379
|
-
* @param {
|
|
380
|
-
* @param {
|
|
424
|
+
* @param {BufferEncoding} config.outputEncoding - The output encoding format (e.g., 'hex').
|
|
425
|
+
* @param {BufferEncoding} config.inputEncoding - The input encoding format (e.g., 'utf8').
|
|
381
426
|
* @param {number} config.authTagLength - The authentication tag length (e.g., 16).
|
|
382
427
|
* @param {string} config.key - The cryptographic key in hexadecimal string format.
|
|
383
428
|
*
|
|
@@ -409,7 +454,7 @@ class TinyCrypto {
|
|
|
409
454
|
else if (typeof config.authTagLength !== 'undefined')
|
|
410
455
|
throw new Error('Invalid or missing "authTagLength" property. Expected a number.');
|
|
411
456
|
|
|
412
|
-
if (typeof config.key === 'string') this.key = Buffer.from(config.key, 'hex');
|
|
457
|
+
if (typeof config.key === 'string') this.key = buffer.Buffer.from(config.key, 'hex');
|
|
413
458
|
else if (typeof config.key !== 'undefined')
|
|
414
459
|
throw new Error('Invalid or missing "key" property. Expected a hexadecimal string.');
|
|
415
460
|
}
|
|
@@ -424,7 +469,7 @@ class TinyCrypto {
|
|
|
424
469
|
* Each key corresponds to a specific data type (e.g., 'number', 'date', 'buffer', etc.),
|
|
425
470
|
* and the value is a function that serializes the data to a specific format.
|
|
426
471
|
*
|
|
427
|
-
* @type {
|
|
472
|
+
* @type {Record<string, (data: any) => string>}
|
|
428
473
|
* @property {Function} weakmap - Throws an error as WeakMap cannot be serialized.
|
|
429
474
|
* @property {Function} weakset - Throws an error as WeakSet cannot be serialized.
|
|
430
475
|
* @property {Function} promise - Throws an error as Promise cannot be serialized.
|
|
@@ -465,8 +510,8 @@ class TinyCrypto {
|
|
|
465
510
|
number: (data) => JSON.stringify({ __type: 'number', value: data }),
|
|
466
511
|
boolean: (data) => JSON.stringify({ __type: 'boolean', value: data }),
|
|
467
512
|
string: (data) => JSON.stringify({ __type: 'string', value: data }),
|
|
468
|
-
null: (
|
|
469
|
-
undefined: (
|
|
513
|
+
null: () => JSON.stringify({ __type: 'null' }),
|
|
514
|
+
undefined: () => JSON.stringify({ __type: 'undefined' }),
|
|
470
515
|
map: (data) =>
|
|
471
516
|
JSON.stringify({
|
|
472
517
|
__type: 'map',
|
|
@@ -491,7 +536,7 @@ class TinyCrypto {
|
|
|
491
536
|
* Each key corresponds to a specific data type (e.g., 'Date', 'BigInt', 'Buffer', etc.),
|
|
492
537
|
* and the value is a function that deserializes the value to its original format.
|
|
493
538
|
*
|
|
494
|
-
* @type {
|
|
539
|
+
* @type {Record<string, (data: any) => any>}
|
|
495
540
|
* @property {Function} regexp - Deserializes a regular expression from its string representation (e.g., `/pattern/flags`).
|
|
496
541
|
* @property {Function} htmlelement - Deserializes an HTML element from its serialized outerHTML string (only works in browser environments).
|
|
497
542
|
* @property {Function} date - Deserializes a date from its ISO string representation.
|
|
@@ -518,21 +563,21 @@ class TinyCrypto {
|
|
|
518
563
|
throw new Error('HTMLElement deserialization is only supported in browsers');
|
|
519
564
|
const div = document.createElement('div');
|
|
520
565
|
div.innerHTML = value;
|
|
521
|
-
return div
|
|
566
|
+
return div;
|
|
522
567
|
},
|
|
523
568
|
date: (value) => new Date(value),
|
|
524
569
|
bigint: (value) => BigInt(value),
|
|
525
570
|
number: (value) => Number(value),
|
|
526
571
|
boolean: (value) => Boolean(value),
|
|
527
|
-
null: (
|
|
528
|
-
undefined: (
|
|
572
|
+
null: () => null,
|
|
573
|
+
undefined: () => undefined,
|
|
529
574
|
map: (value) => new Map(value),
|
|
530
575
|
set: (value) => new Set(value),
|
|
531
576
|
symbol: (value) => Symbol(value),
|
|
532
577
|
array: (value) => value,
|
|
533
578
|
object: (value) => value,
|
|
534
579
|
string: (value) => String(value),
|
|
535
|
-
buffer: (value) => Buffer.from(value, 'base64'),
|
|
580
|
+
buffer: (value) => buffer.Buffer.from(value, 'base64'),
|
|
536
581
|
};
|
|
537
582
|
|
|
538
583
|
/**
|
|
@@ -546,7 +591,8 @@ class TinyCrypto {
|
|
|
546
591
|
*/
|
|
547
592
|
#serialize(data) {
|
|
548
593
|
const type = objFilter.objType(data) || 'undefined';
|
|
549
|
-
if (
|
|
594
|
+
if (typeof type === 'string' && this.#valueConvertTypes[type])
|
|
595
|
+
return this.#valueConvertTypes[type](data);
|
|
550
596
|
throw new Error(`Unsupported data type for encryption: ${type}`);
|
|
551
597
|
}
|
|
552
598
|
|
|
@@ -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,10 +296,11 @@ 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;
|
|
266
304
|
#private;
|
|
267
305
|
}
|
|
306
|
+
import { Buffer } from 'buffer';
|