@catbee/utils 2.0.0-next.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +102 -52
- package/array/index.cjs +215 -74
- package/array/index.d.ts +345 -2
- package/array/index.mjs +201 -74
- package/async/index.cjs +116 -39
- package/async/index.d.ts +292 -2
- package/async/index.mjs +116 -40
- package/cache/index.cjs +2 -2
- package/cache/index.d.ts +156 -2
- package/cache/index.mjs +3 -3
- package/config/index.cjs +80 -66
- package/config/index.d.ts +65 -3
- package/config/index.mjs +77 -65
- package/context-store/index.cjs +2 -3
- package/context-store/index.d.ts +193 -2
- package/context-store/index.mjs +2 -3
- package/crypto/index.cjs +55 -5
- package/crypto/index.d.ts +225 -2
- package/crypto/index.mjs +52 -7
- package/date/index.cjs +676 -2
- package/date/index.d.ts +676 -2
- package/date/index.mjs +665 -3
- package/decorator/index.cjs +2172 -0
- package/{decorators/decorators.utils.d.ts → decorator/index.d.ts} +58 -54
- package/decorator/index.mjs +2131 -0
- package/{dir → directory}/index.cjs +5 -4
- package/{dir/dir.utils.d.ts → directory/index.d.ts} +24 -21
- package/{dir → directory}/index.mjs +5 -4
- package/env/index.cjs +100 -68
- package/env/index.d.ts +391 -2
- package/env/index.mjs +100 -68
- package/exception/index.cjs +1 -1
- package/exception/index.d.ts +233 -2
- package/exception/index.mjs +1 -1
- package/fs/index.cjs +71 -37
- package/fs/index.d.ts +206 -2
- package/fs/index.mjs +65 -35
- package/http-status-codes/index.cjs +1 -1
- package/http-status-codes/index.d.ts +268 -2
- package/http-status-codes/index.mjs +1 -1
- package/id/index.cjs +1 -1
- package/id/index.d.ts +38 -2
- package/id/index.mjs +1 -1
- package/index.cjs +13 -13
- package/index.d.ts +5 -5
- package/index.mjs +5 -5
- package/logger/index.cjs +13 -15
- package/logger/index.d.ts +190 -2
- package/logger/index.mjs +14 -16
- package/middleware/index.cjs +1 -1
- package/middleware/index.d.ts +104 -2
- package/middleware/index.mjs +1 -1
- package/object/index.cjs +379 -0
- package/{obj/obj.utils.d.ts → object/index.d.ts} +73 -33
- package/object/index.mjs +360 -0
- package/package.json +41 -23
- package/performance/index.cjs +4 -4
- package/performance/index.d.ts +139 -2
- package/performance/index.mjs +4 -4
- package/request/index.cjs +37 -25
- package/request/index.d.ts +242 -3
- package/request/index.mjs +37 -25
- package/response/index.cjs +1 -1
- package/response/index.d.ts +319 -3
- package/response/index.mjs +1 -1
- package/server/index.cjs +249 -146
- package/server/index.d.ts +866 -5
- package/server/index.mjs +248 -144
- package/stream/index.cjs +1 -1
- package/stream/index.d.ts +91 -2
- package/stream/index.mjs +1 -1
- package/string/index.cjs +34 -1
- package/string/index.d.ts +146 -2
- package/string/index.mjs +31 -2
- package/type/index.cjs +19 -2
- package/type/index.d.ts +144 -2
- package/type/index.mjs +17 -3
- package/types/index.cjs +1 -1
- package/types/index.d.ts +775 -5
- package/types/index.mjs +1 -1
- package/url/index.cjs +63 -5
- package/url/index.d.ts +200 -2
- package/url/index.mjs +59 -6
- package/{validate → validation}/index.cjs +91 -44
- package/{validate/validate.utils.d.ts → validation/index.d.ts} +33 -24
- package/{validate → validation}/index.mjs +87 -44
- package/array/array.utils.d.ts +0 -191
- package/async/async.utils.d.ts +0 -296
- package/cache/cache.utils.d.ts +0 -176
- package/config/config.d.ts +0 -57
- package/context-store/context-store.utils.d.ts +0 -212
- package/crypto/crypto.utils.d.ts +0 -183
- package/date/date.utils.d.ts +0 -190
- package/decorators/index.cjs +0 -913
- package/decorators/index.d.ts +0 -25
- package/decorators/index.mjs +0 -872
- package/dir/index.d.ts +0 -25
- package/env/env.utils.d.ts +0 -400
- package/exception/exception.utils.d.ts +0 -253
- package/fs/fs.utils.d.ts +0 -196
- package/http-status-codes/http-status-codes.d.ts +0 -289
- package/id/id.utils.d.ts +0 -59
- package/logger/logger.utils.d.ts +0 -210
- package/middleware/middleware.utils.d.ts +0 -123
- package/obj/index.cjs +0 -317
- package/obj/index.d.ts +0 -25
- package/obj/index.mjs +0 -301
- package/performance/performance.utils.d.ts +0 -159
- package/request/request.utils.d.ts +0 -109
- package/response/response.utils.d.ts +0 -186
- package/server/server.builder.d.ts +0 -531
- package/server/server.d.ts +0 -303
- package/stream/stream.utils.d.ts +0 -111
- package/string/string.utils.d.ts +0 -124
- package/type/type.utils.d.ts +0 -129
- package/types/api-response.d.ts +0 -175
- package/types/common.d.ts +0 -148
- package/types/config.d.ts +0 -88
- package/types/server.d.ts +0 -291
- package/url/url.utils.d.ts +0 -164
- package/validate/index.d.ts +0 -25
package/crypto/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -22,12 +22,14 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import { scrypt, createHmac, createHash, randomBytes, timingSafeEqual, createCipheriv, createDecipheriv } from 'crypto';
|
|
25
|
+
import { pbkdf2, scrypt, createHmac, createHash, randomBytes, timingSafeEqual, createCipheriv, createDecipheriv } from 'crypto';
|
|
26
26
|
import { promisify } from 'util';
|
|
27
27
|
import { uuid } from '@catbee/utils/id';
|
|
28
28
|
|
|
29
29
|
var __defProp = Object.defineProperty;
|
|
30
30
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
31
|
+
var pbkdf2Async = promisify(pbkdf2);
|
|
32
|
+
var scryptAsync = promisify(scrypt);
|
|
31
33
|
function hmac(algorithm, input, secret, encoding = "hex") {
|
|
32
34
|
return createHmac(algorithm, secret).update(input).digest(encoding);
|
|
33
35
|
}
|
|
@@ -83,13 +85,13 @@ function safeCompare(a, b) {
|
|
|
83
85
|
throw new Error("Cannot compare: inputs must be strings, Buffers, or Uint8Arrays");
|
|
84
86
|
}
|
|
85
87
|
__name(safeCompare, "safeCompare");
|
|
86
|
-
var scryptAsync = promisify(scrypt);
|
|
87
88
|
async function encrypt(data, key, options = {}) {
|
|
88
89
|
const algorithm = options.algorithm || "aes-256-gcm";
|
|
89
90
|
const inputEncoding = options.inputEncoding || "utf8";
|
|
90
91
|
const outputEncoding = options.outputEncoding || "hex";
|
|
91
92
|
const iv = randomBytes(16);
|
|
92
|
-
const
|
|
93
|
+
const salt = randomBytes(8);
|
|
94
|
+
const derivedKey = typeof key === "string" ? await scryptAsync(key, salt, 32) : key;
|
|
93
95
|
const cipher = createCipheriv(algorithm, derivedKey, iv);
|
|
94
96
|
let ciphertext;
|
|
95
97
|
if (typeof data === "string") {
|
|
@@ -107,7 +109,8 @@ async function encrypt(data, key, options = {}) {
|
|
|
107
109
|
ciphertext,
|
|
108
110
|
iv,
|
|
109
111
|
authTag,
|
|
110
|
-
algorithm
|
|
112
|
+
algorithm,
|
|
113
|
+
salt
|
|
111
114
|
};
|
|
112
115
|
}
|
|
113
116
|
__name(encrypt, "encrypt");
|
|
@@ -115,7 +118,7 @@ async function decrypt(encryptedData, key, options = {}) {
|
|
|
115
118
|
const algorithm = options.algorithm || encryptedData.algorithm || "aes-256-gcm";
|
|
116
119
|
const inputEncoding = options.inputEncoding || "hex";
|
|
117
120
|
const outputEncoding = options.outputEncoding || "utf8";
|
|
118
|
-
const derivedKey = typeof key === "string" ? await scryptAsync(key, encryptedData.
|
|
121
|
+
const derivedKey = typeof key === "string" ? await scryptAsync(key, encryptedData.salt, 32) : key;
|
|
119
122
|
const decipher = createDecipheriv(algorithm, derivedKey, encryptedData.iv);
|
|
120
123
|
if (encryptedData.authTag && algorithm.includes("gcm")) {
|
|
121
124
|
decipher.setAuthTag(encryptedData.authTag);
|
|
@@ -162,5 +165,47 @@ function verifySignedToken(token, secret) {
|
|
|
162
165
|
}
|
|
163
166
|
}
|
|
164
167
|
__name(verifySignedToken, "verifySignedToken");
|
|
168
|
+
async function pbkdf2Hash(password, salt, keyLength = 32, iterations = 31e4) {
|
|
169
|
+
return pbkdf2Async(password, salt, iterations, keyLength, "sha256");
|
|
170
|
+
}
|
|
171
|
+
__name(pbkdf2Hash, "pbkdf2Hash");
|
|
172
|
+
function generateNonce(byteLength = 16, encoding = "hex") {
|
|
173
|
+
return randomBytes(byteLength).toString(encoding);
|
|
174
|
+
}
|
|
175
|
+
__name(generateNonce, "generateNonce");
|
|
176
|
+
function secureRandomInt(min, max) {
|
|
177
|
+
if (min > max) throw new Error("min must be less than or equal to max");
|
|
178
|
+
const range = max - min + 1;
|
|
179
|
+
const bytesNeeded = Math.ceil(Math.log2(range) / 8);
|
|
180
|
+
const maxValid = Math.floor(256 ** bytesNeeded / range) * range;
|
|
181
|
+
let randomValue;
|
|
182
|
+
do {
|
|
183
|
+
const bytes = randomBytes(bytesNeeded);
|
|
184
|
+
randomValue = bytes.reduce((acc, byte, i) => acc + byte * 256 ** i, 0);
|
|
185
|
+
} while (randomValue >= maxValid);
|
|
186
|
+
return min + randomValue % range;
|
|
187
|
+
}
|
|
188
|
+
__name(secureRandomInt, "secureRandomInt");
|
|
189
|
+
async function hashPassword(password, saltLength = 16, keyLength = 32) {
|
|
190
|
+
const salt = randomBytes(saltLength);
|
|
191
|
+
const scryptAsync2 = promisify(scrypt);
|
|
192
|
+
const derivedKey = await scryptAsync2(password, salt, keyLength);
|
|
193
|
+
return `${salt.toString("base64")}:${derivedKey.toString("base64")}`;
|
|
194
|
+
}
|
|
195
|
+
__name(hashPassword, "hashPassword");
|
|
196
|
+
async function verifyPassword(password, hash2) {
|
|
197
|
+
try {
|
|
198
|
+
const [saltB64, keyB64] = hash2.split(":");
|
|
199
|
+
if (!saltB64 || !keyB64) return false;
|
|
200
|
+
const salt = Buffer.from(saltB64, "base64");
|
|
201
|
+
const key = Buffer.from(keyB64, "base64");
|
|
202
|
+
const scryptAsync2 = promisify(scrypt);
|
|
203
|
+
const derivedKey = await scryptAsync2(password, salt, key.length);
|
|
204
|
+
return timingSafeEqual(key, derivedKey);
|
|
205
|
+
} catch {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
__name(verifyPassword, "verifyPassword");
|
|
165
210
|
|
|
166
|
-
export { createSignedToken, decrypt, encrypt, generateApiKey, generateRandomBytes, generateRandomBytesAsString, hash, hmac, md5, randomString, safeCompare, sha1, sha256, sha256Hmac, verifySignedToken };
|
|
211
|
+
export { createSignedToken, decrypt, encrypt, generateApiKey, generateNonce, generateRandomBytes, generateRandomBytesAsString, hash, hashPassword, hmac, md5, pbkdf2Hash, randomString, safeCompare, secureRandomInt, sha1, sha256, sha256Hmac, verifyPassword, verifySignedToken };
|