itrm-tools 1.4.8 → 1.4.9
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.
|
@@ -1,34 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PasswordUtils = void 0;
|
|
4
|
-
const
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
5
|
+
const SALT_BYTES = 16;
|
|
6
|
+
const KEY_BYTES = 64;
|
|
7
|
+
const HASH_ALGORITHM = "sha256";
|
|
8
|
+
const DEFAULT_ITERATIONS = 600_000;
|
|
5
9
|
class PasswordUtils {
|
|
6
10
|
iterations;
|
|
7
|
-
constructor(iterations =
|
|
11
|
+
constructor(iterations = DEFAULT_ITERATIONS) {
|
|
8
12
|
this.iterations = iterations;
|
|
9
13
|
}
|
|
10
14
|
async hashPassword(password) {
|
|
11
|
-
const salt =
|
|
12
|
-
const hash =
|
|
13
|
-
|
|
14
|
-
iterations: this.iterations,
|
|
15
|
-
hasher: crypto_js_1.algo.SHA256,
|
|
16
|
-
});
|
|
17
|
-
return `${crypto_js_1.enc.Hex.stringify(salt)}:${crypto_js_1.enc.Hex.stringify(hash)}`;
|
|
15
|
+
const salt = (0, node_crypto_1.randomBytes)(SALT_BYTES);
|
|
16
|
+
const hash = await this.derive(password, salt);
|
|
17
|
+
return `${salt.toString("hex")}:${hash.toString("hex")}`;
|
|
18
18
|
}
|
|
19
19
|
async comparePasswords(password, storedHash) {
|
|
20
|
-
const [saltHex, originalHashHex] = storedHash.split(
|
|
20
|
+
const [saltHex, originalHashHex] = storedHash.split(":");
|
|
21
21
|
if (!saltHex || !originalHashHex) {
|
|
22
22
|
throw new Error("Invalid stored hash format. Expected 'salt:hash'.");
|
|
23
23
|
}
|
|
24
|
-
const salt =
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
const salt = Buffer.from(saltHex, "hex");
|
|
25
|
+
const expected = Buffer.from(originalHashHex, "hex");
|
|
26
|
+
if (salt.length !== SALT_BYTES || expected.length !== KEY_BYTES)
|
|
27
|
+
return false;
|
|
28
|
+
const actual = await this.derive(password, salt);
|
|
29
|
+
return (0, node_crypto_1.timingSafeEqual)(actual, expected);
|
|
30
|
+
}
|
|
31
|
+
derive(password, salt) {
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
(0, node_crypto_1.pbkdf2)(password, salt, this.iterations, KEY_BYTES, HASH_ALGORITHM, (error, key) => error ? reject(error) : resolve(key));
|
|
29
34
|
});
|
|
30
|
-
const inputPasswordHashHex = crypto_js_1.enc.Hex.stringify(hash);
|
|
31
|
-
return inputPasswordHashHex === originalHashHex;
|
|
32
35
|
}
|
|
33
36
|
}
|
|
34
37
|
exports.PasswordUtils = PasswordUtils;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PasswordUtils.js","sourceRoot":"","sources":["../../../src/cryptography/password/PasswordUtils.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"PasswordUtils.js","sourceRoot":"","sources":["../../../src/cryptography/password/PasswordUtils.ts"],"names":[],"mappings":";;;AAAA,6CAAmE;AAEnE,MAAM,UAAU,GAAG,EAAE,CAAC;AACtB,MAAM,SAAS,GAAG,EAAE,CAAC;AACrB,MAAM,cAAc,GAAG,QAAQ,CAAC;AAChC,MAAM,kBAAkB,GAAG,OAAO,CAAC;AAEnC,MAAa,aAAa;IACP,UAAU,CAAS;IAEpC,YAAY,aAAqB,kBAAkB;QACjD,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;IAEM,KAAK,CAAC,YAAY,CAAC,QAAgB;QACxC,MAAM,IAAI,GAAG,IAAA,yBAAW,EAAC,UAAU,CAAC,CAAC;QACrC,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;QAC/C,OAAO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