@catbee/utils 2.0.0-next.1 → 2.0.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/LICENSE +1 -1
- package/README.md +55 -38
- package/array/index.cjs +37 -5
- package/array/index.d.ts +59 -8
- package/array/index.mjs +32 -4
- package/async/index.cjs +24 -3
- package/async/index.d.ts +18 -2
- package/async/index.mjs +24 -4
- package/cache/index.cjs +1 -1
- package/cache/index.d.ts +1 -1
- package/cache/index.mjs +1 -1
- package/config/index.cjs +8 -8
- package/config/index.d.ts +1 -1
- package/config/index.mjs +4 -4
- package/context-store/index.cjs +2 -3
- package/context-store/index.d.ts +1 -1
- package/context-store/index.mjs +2 -3
- package/crypto/index.cjs +55 -5
- package/crypto/index.d.ts +63 -2
- package/crypto/index.mjs +52 -7
- package/date/index.cjs +630 -1
- package/date/index.d.ts +487 -2
- package/date/index.mjs +621 -2
- package/{decorators → decorator}/index.cjs +425 -304
- package/{decorators → decorator}/index.d.ts +3 -2
- package/{decorators → decorator}/index.mjs +425 -304
- package/{dir → directory}/index.cjs +1 -1
- package/{dir → directory}/index.d.ts +1 -1
- package/{dir → directory}/index.mjs +1 -1
- package/env/index.cjs +90 -42
- package/env/index.d.ts +12 -1
- package/env/index.mjs +90 -42
- package/exception/index.cjs +1 -1
- package/exception/index.d.ts +1 -1
- package/exception/index.mjs +1 -1
- package/fs/index.cjs +1 -1
- package/fs/index.d.ts +1 -1
- package/fs/index.mjs +1 -1
- package/http-status-codes/index.cjs +1 -1
- package/http-status-codes/index.d.ts +1 -1
- package/http-status-codes/index.mjs +1 -1
- package/id/index.cjs +1 -1
- package/id/index.d.ts +1 -1
- package/id/index.mjs +1 -1
- package/index.cjs +10 -10
- package/index.d.ts +4 -4
- package/index.mjs +4 -4
- package/logger/index.cjs +2 -4
- package/logger/index.d.ts +2 -2
- package/logger/index.mjs +2 -4
- package/middleware/index.cjs +1 -1
- package/middleware/index.d.ts +1 -1
- package/middleware/index.mjs +1 -1
- package/{obj → object}/index.cjs +182 -108
- package/{obj → object}/index.d.ts +38 -2
- package/{obj → object}/index.mjs +180 -109
- package/package.json +31 -13
- package/performance/index.cjs +2 -2
- package/performance/index.d.ts +1 -1
- package/performance/index.mjs +2 -2
- package/request/index.cjs +36 -24
- package/request/index.d.ts +1 -1
- package/request/index.mjs +36 -24
- package/response/index.cjs +1 -1
- package/response/index.d.ts +1 -1
- package/response/index.mjs +1 -1
- package/server/index.cjs +233 -131
- package/server/index.d.ts +81 -1
- package/server/index.mjs +229 -127
- package/stream/index.cjs +1 -1
- package/stream/index.d.ts +1 -1
- package/stream/index.mjs +1 -1
- package/string/index.cjs +34 -1
- package/string/index.d.ts +45 -2
- package/string/index.mjs +31 -2
- package/type/index.cjs +18 -1
- package/type/index.d.ts +38 -2
- package/type/index.mjs +16 -2
- package/types/index.cjs +1 -1
- package/types/index.d.ts +10 -3
- package/types/index.mjs +1 -1
- package/url/index.cjs +61 -1
- package/url/index.d.ts +59 -2
- package/url/index.mjs +57 -2
- package/validation/index.cjs +3 -3
- package/validation/index.d.ts +1 -1
- package/validation/index.mjs +3 -3
package/config/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
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
|
|
25
25
|
import { Env } from '@catbee/utils/env';
|
|
26
26
|
import { uuid } from '@catbee/utils/id';
|
|
27
|
-
import { deepClone, deepObjMerge } from '@catbee/utils/
|
|
27
|
+
import { deepClone, deepObjMerge } from '@catbee/utils/object';
|
|
28
28
|
|
|
29
29
|
var __defProp = Object.defineProperty;
|
|
30
30
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
@@ -100,9 +100,9 @@ var defaultServerConfig = {
|
|
|
100
100
|
serviceVersion: {
|
|
101
101
|
enable: Env.getBoolean("SERVER_SERVICE_VERSION_ENABLE", false),
|
|
102
102
|
headerName: Env.get("SERVER_SERVICE_VERSION_HEADER_NAME", "x-service-version"),
|
|
103
|
-
version: Env.get("
|
|
103
|
+
version: Env.get("${npm_package_version}", "0.0.0")
|
|
104
104
|
},
|
|
105
|
-
|
|
105
|
+
skipHealthzChecksValidation: Env.getBoolean("SERVER_SKIP_HEALTHZ_CHECKS_VALIDATION", false)
|
|
106
106
|
};
|
|
107
107
|
var defaultCatbeeConfig = {
|
|
108
108
|
logger: {
|
package/context-store/index.cjs
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
|
|
@@ -231,8 +231,7 @@ var TypedContextKey = class {
|
|
|
231
231
|
* @returns The value or defaultValue if not found
|
|
232
232
|
*/
|
|
233
233
|
get() {
|
|
234
|
-
|
|
235
|
-
return value !== void 0 ? value : this.defaultValue;
|
|
234
|
+
return ContextStore.get(this.symbol) ?? this.defaultValue;
|
|
236
235
|
}
|
|
237
236
|
/**
|
|
238
237
|
* Sets the value for this key.
|
package/context-store/index.d.ts
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
|
package/context-store/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
|
|
@@ -229,8 +229,7 @@ var TypedContextKey = class {
|
|
|
229
229
|
* @returns The value or defaultValue if not found
|
|
230
230
|
*/
|
|
231
231
|
get() {
|
|
232
|
-
|
|
233
|
-
return value !== void 0 ? value : this.defaultValue;
|
|
232
|
+
return ContextStore.get(this.symbol) ?? this.defaultValue;
|
|
234
233
|
}
|
|
235
234
|
/**
|
|
236
235
|
* Sets the value for this key.
|
package/crypto/index.cjs
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
|
|
@@ -30,6 +30,8 @@ var id = require('@catbee/utils/id');
|
|
|
30
30
|
|
|
31
31
|
var __defProp = Object.defineProperty;
|
|
32
32
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
33
|
+
var pbkdf2Async = util.promisify(crypto.pbkdf2);
|
|
34
|
+
var scryptAsync = util.promisify(crypto.scrypt);
|
|
33
35
|
function hmac(algorithm, input, secret, encoding = "hex") {
|
|
34
36
|
return crypto.createHmac(algorithm, secret).update(input).digest(encoding);
|
|
35
37
|
}
|
|
@@ -85,13 +87,13 @@ function safeCompare(a, b) {
|
|
|
85
87
|
throw new Error("Cannot compare: inputs must be strings, Buffers, or Uint8Arrays");
|
|
86
88
|
}
|
|
87
89
|
__name(safeCompare, "safeCompare");
|
|
88
|
-
var scryptAsync = util.promisify(crypto.scrypt);
|
|
89
90
|
async function encrypt(data, key, options = {}) {
|
|
90
91
|
const algorithm = options.algorithm || "aes-256-gcm";
|
|
91
92
|
const inputEncoding = options.inputEncoding || "utf8";
|
|
92
93
|
const outputEncoding = options.outputEncoding || "hex";
|
|
93
94
|
const iv = crypto.randomBytes(16);
|
|
94
|
-
const
|
|
95
|
+
const salt = crypto.randomBytes(8);
|
|
96
|
+
const derivedKey = typeof key === "string" ? await scryptAsync(key, salt, 32) : key;
|
|
95
97
|
const cipher = crypto.createCipheriv(algorithm, derivedKey, iv);
|
|
96
98
|
let ciphertext;
|
|
97
99
|
if (typeof data === "string") {
|
|
@@ -109,7 +111,8 @@ async function encrypt(data, key, options = {}) {
|
|
|
109
111
|
ciphertext,
|
|
110
112
|
iv,
|
|
111
113
|
authTag,
|
|
112
|
-
algorithm
|
|
114
|
+
algorithm,
|
|
115
|
+
salt
|
|
113
116
|
};
|
|
114
117
|
}
|
|
115
118
|
__name(encrypt, "encrypt");
|
|
@@ -117,7 +120,7 @@ async function decrypt(encryptedData, key, options = {}) {
|
|
|
117
120
|
const algorithm = options.algorithm || encryptedData.algorithm || "aes-256-gcm";
|
|
118
121
|
const inputEncoding = options.inputEncoding || "hex";
|
|
119
122
|
const outputEncoding = options.outputEncoding || "utf8";
|
|
120
|
-
const derivedKey = typeof key === "string" ? await scryptAsync(key, encryptedData.
|
|
123
|
+
const derivedKey = typeof key === "string" ? await scryptAsync(key, encryptedData.salt, 32) : key;
|
|
121
124
|
const decipher = crypto.createDecipheriv(algorithm, derivedKey, encryptedData.iv);
|
|
122
125
|
if (encryptedData.authTag && algorithm.includes("gcm")) {
|
|
123
126
|
decipher.setAuthTag(encryptedData.authTag);
|
|
@@ -164,19 +167,66 @@ function verifySignedToken(token, secret) {
|
|
|
164
167
|
}
|
|
165
168
|
}
|
|
166
169
|
__name(verifySignedToken, "verifySignedToken");
|
|
170
|
+
async function pbkdf2Hash(password, salt, keyLength = 32, iterations = 31e4) {
|
|
171
|
+
return pbkdf2Async(password, salt, iterations, keyLength, "sha256");
|
|
172
|
+
}
|
|
173
|
+
__name(pbkdf2Hash, "pbkdf2Hash");
|
|
174
|
+
function generateNonce(byteLength = 16, encoding = "hex") {
|
|
175
|
+
return crypto.randomBytes(byteLength).toString(encoding);
|
|
176
|
+
}
|
|
177
|
+
__name(generateNonce, "generateNonce");
|
|
178
|
+
function secureRandomInt(min, max) {
|
|
179
|
+
if (min > max) throw new Error("min must be less than or equal to max");
|
|
180
|
+
const range = max - min + 1;
|
|
181
|
+
const bytesNeeded = Math.ceil(Math.log2(range) / 8);
|
|
182
|
+
const maxValid = Math.floor(256 ** bytesNeeded / range) * range;
|
|
183
|
+
let randomValue;
|
|
184
|
+
do {
|
|
185
|
+
const bytes = crypto.randomBytes(bytesNeeded);
|
|
186
|
+
randomValue = bytes.reduce((acc, byte, i) => acc + byte * 256 ** i, 0);
|
|
187
|
+
} while (randomValue >= maxValid);
|
|
188
|
+
return min + randomValue % range;
|
|
189
|
+
}
|
|
190
|
+
__name(secureRandomInt, "secureRandomInt");
|
|
191
|
+
async function hashPassword(password, saltLength = 16, keyLength = 32) {
|
|
192
|
+
const salt = crypto.randomBytes(saltLength);
|
|
193
|
+
const scryptAsync2 = util.promisify(crypto.scrypt);
|
|
194
|
+
const derivedKey = await scryptAsync2(password, salt, keyLength);
|
|
195
|
+
return `${salt.toString("base64")}:${derivedKey.toString("base64")}`;
|
|
196
|
+
}
|
|
197
|
+
__name(hashPassword, "hashPassword");
|
|
198
|
+
async function verifyPassword(password, hash2) {
|
|
199
|
+
try {
|
|
200
|
+
const [saltB64, keyB64] = hash2.split(":");
|
|
201
|
+
if (!saltB64 || !keyB64) return false;
|
|
202
|
+
const salt = Buffer.from(saltB64, "base64");
|
|
203
|
+
const key = Buffer.from(keyB64, "base64");
|
|
204
|
+
const scryptAsync2 = util.promisify(crypto.scrypt);
|
|
205
|
+
const derivedKey = await scryptAsync2(password, salt, key.length);
|
|
206
|
+
return crypto.timingSafeEqual(key, derivedKey);
|
|
207
|
+
} catch {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
__name(verifyPassword, "verifyPassword");
|
|
167
212
|
|
|
168
213
|
exports.createSignedToken = createSignedToken;
|
|
169
214
|
exports.decrypt = decrypt;
|
|
170
215
|
exports.encrypt = encrypt;
|
|
171
216
|
exports.generateApiKey = generateApiKey;
|
|
217
|
+
exports.generateNonce = generateNonce;
|
|
172
218
|
exports.generateRandomBytes = generateRandomBytes;
|
|
173
219
|
exports.generateRandomBytesAsString = generateRandomBytesAsString;
|
|
174
220
|
exports.hash = hash;
|
|
221
|
+
exports.hashPassword = hashPassword;
|
|
175
222
|
exports.hmac = hmac;
|
|
176
223
|
exports.md5 = md5;
|
|
224
|
+
exports.pbkdf2Hash = pbkdf2Hash;
|
|
177
225
|
exports.randomString = randomString;
|
|
178
226
|
exports.safeCompare = safeCompare;
|
|
227
|
+
exports.secureRandomInt = secureRandomInt;
|
|
179
228
|
exports.sha1 = sha1;
|
|
180
229
|
exports.sha256 = sha256;
|
|
181
230
|
exports.sha256Hmac = sha256Hmac;
|
|
231
|
+
exports.verifyPassword = verifyPassword;
|
|
182
232
|
exports.verifySignedToken = verifySignedToken;
|
package/crypto/index.d.ts
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
|
|
@@ -146,6 +146,8 @@ interface EncryptionResult {
|
|
|
146
146
|
authTag?: Buffer;
|
|
147
147
|
/** Algorithm used */
|
|
148
148
|
algorithm: string;
|
|
149
|
+
/** Salt used for key derivation */
|
|
150
|
+
salt: Buffer;
|
|
149
151
|
}
|
|
150
152
|
/**
|
|
151
153
|
* Encrypts data using a symmetric key with secure defaults (AES-256-GCM).
|
|
@@ -182,6 +184,65 @@ declare function createSignedToken(payload: Record<string, any>, secret: string,
|
|
|
182
184
|
* @returns {object | null} Decoded payload if valid, null if invalid
|
|
183
185
|
*/
|
|
184
186
|
declare function verifySignedToken(token: string, secret: string): Record<string, any> | null;
|
|
187
|
+
/**
|
|
188
|
+
* Derives a cryptographic key using PBKDF2 (SHA-256).
|
|
189
|
+
*
|
|
190
|
+
* @param password - Password to derive key from
|
|
191
|
+
* @param salt - Cryptographic salt (use unique per password)
|
|
192
|
+
* @param keyLength - Output key length in bytes (default 32)
|
|
193
|
+
* @param iterations - Number of hashing iterations (default 310000, OWASP recommended)
|
|
194
|
+
* @returns Derived key as Buffer
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* const key = await pbkdf2Hash('myPassword', 'mySalt');
|
|
198
|
+
*/
|
|
199
|
+
declare function pbkdf2Hash(password: string, salt: string | Buffer, keyLength?: number, iterations?: number): Promise<Buffer>;
|
|
200
|
+
/**
|
|
201
|
+
* Generates a cryptographically secure nonce (number used once).
|
|
202
|
+
*
|
|
203
|
+
* @param {number} [byteLength=16] - Length of nonce in bytes.
|
|
204
|
+
* @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding.
|
|
205
|
+
* @returns {string} Nonce string.
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* const nonce = generateNonce(16, 'base64'); // Random nonce
|
|
209
|
+
*/
|
|
210
|
+
declare function generateNonce(byteLength?: number, encoding?: BinaryToTextEncoding): string;
|
|
211
|
+
/**
|
|
212
|
+
* Generates a cryptographically secure random integer in a range.
|
|
213
|
+
*
|
|
214
|
+
* @param {number} min - Minimum value (inclusive).
|
|
215
|
+
* @param {number} max - Maximum value (inclusive).
|
|
216
|
+
* @returns {number} Random integer.
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* const random = secureRandomInt(1, 100); // Random number 1-100
|
|
220
|
+
*/
|
|
221
|
+
declare function secureRandomInt(min: number, max: number): number;
|
|
222
|
+
/**
|
|
223
|
+
* Hashes a password using scrypt (memory-hard function).
|
|
224
|
+
*
|
|
225
|
+
* @param {string} password - The password to hash.
|
|
226
|
+
* @param {number} [saltLength=16] - Length of salt in bytes.
|
|
227
|
+
* @param {number} [keyLength=32] - Length of derived key.
|
|
228
|
+
* @returns {Promise<string>} Hash string containing salt and key.
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* const hash = await hashPassword('myPassword');
|
|
232
|
+
* // Returns format: salt:hash (both base64)
|
|
233
|
+
*/
|
|
234
|
+
declare function hashPassword(password: string, saltLength?: number, keyLength?: number): Promise<string>;
|
|
235
|
+
/**
|
|
236
|
+
* Verifies a password against a scrypt hash.
|
|
237
|
+
*
|
|
238
|
+
* @param {string} password - The password to verify.
|
|
239
|
+
* @param {string} hash - The hash to verify against (from hashPassword).
|
|
240
|
+
* @returns {Promise<boolean>} True if password matches.
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* const isValid = await verifyPassword('myPassword', storedHash);
|
|
244
|
+
*/
|
|
245
|
+
declare function verifyPassword(password: string, hash: string): Promise<boolean>;
|
|
185
246
|
|
|
186
|
-
export { createSignedToken, decrypt, encrypt, generateApiKey, generateRandomBytes, generateRandomBytesAsString, hash, hmac, md5, randomString, safeCompare, sha1, sha256, sha256Hmac, verifySignedToken };
|
|
247
|
+
export { createSignedToken, decrypt, encrypt, generateApiKey, generateNonce, generateRandomBytes, generateRandomBytesAsString, hash, hashPassword, hmac, md5, pbkdf2Hash, randomString, safeCompare, secureRandomInt, sha1, sha256, sha256Hmac, verifyPassword, verifySignedToken };
|
|
187
248
|
export type { BufferEncoding, DecryptionOptions, EncryptionOptions, EncryptionResult };
|
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 };
|