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.
@@ -3,4 +3,5 @@ export declare class PasswordUtils {
3
3
  constructor(iterations?: number);
4
4
  hashPassword(password: string): Promise<string>;
5
5
  comparePasswords(password: string, storedHash: string): Promise<boolean>;
6
+ private derive;
6
7
  }
@@ -1,34 +1,37 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PasswordUtils = void 0;
4
- const crypto_js_1 = require("crypto-js");
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 = 600000) {
11
+ constructor(iterations = DEFAULT_ITERATIONS) {
8
12
  this.iterations = iterations;
9
13
  }
10
14
  async hashPassword(password) {
11
- const salt = crypto_js_1.lib.WordArray.random(128 / 8); // 128-bit salt is standard
12
- const hash = (0, crypto_js_1.PBKDF2)(password, salt, {
13
- keySize: 512 / 32, // 512-bit key
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 = crypto_js_1.enc.Hex.parse(saltHex);
25
- const hash = (0, crypto_js_1.PBKDF2)(password, salt, {
26
- keySize: 512 / 32,
27
- iterations: this.iterations,
28
- hasher: crypto_js_1.algo.SHA256,
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,yCAAmD;AAEnD,MAAa,aAAa;IACP,UAAU,CAAS;IAEpC,YAAY,aAAqB,MAAM;QACrC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;IAEM,KAAK,CAAC,YAAY,CAAC,QAAgB;QACxC,MAAM,IAAI,GAAG,eAAG,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,2BAA2B;QACvE,MAAM,IAAI,GAAG,IAAA,kBAAM,EAAC,QAAQ,EAAE,IAAI,EAAE;YAClC,OAAO,EAAE,GAAG,GAAG,EAAE,EAAE,cAAc;YACjC,UAAU,EAAE,IAAI,CAAC,UAAU;YAC3B,MAAM,EAAE,gBAAI,CAAC,MAAM;SACpB,CAAC,CAAC;QACH,OAAO,GAAG,eAAG,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,eAAG,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC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