Skip to content

Commit 7fc925f

Browse files
committed
feat: Add PBKDF2 password hashing support and consistently store the generated hashed password.
1 parent 02f2d6b commit 7fc925f

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

main.js

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const utils = require("@iobroker/adapter-core");
44
const WebSocket = require("ws");
5-
const { createHash, createHmac } = require("crypto");
5+
const { createHash, createHmac, pbkdf2Sync } = require("crypto");
66
const bcrypt = require("bcryptjs");
77

88
// --- Constants ---
@@ -442,24 +442,40 @@ class FroniusWattpilot extends utils.Adapter {
442442
const ran = this.__randomBigInt(80);
443443
// === Python: "%064x" % ran
444444
let token3 = this.__formatHex(ran).slice(0, 32);
445+
let hashedPassword;
446+
447+
if (message.hash === "pbkdf2") {
448+
const iterations = 100000;
449+
const keylen = 256;
450+
const digest = "sha512";
451+
const derivedKey = pbkdf2Sync(
452+
this.config.pass,
453+
this.sseToken,
454+
iterations,
455+
keylen,
456+
digest,
457+
);
458+
this.hashedPassword = derivedKey.toString("base64").substring(0, 32);
459+
hashedPassword = this.hashedPassword;
460+
} else {
461+
const passwordHashSha256 = createHash("sha256")
462+
.update(this.config.pass, "utf8")
463+
.digest("hex");
445464

446-
// === Python: bcrypt hashing logic ===
447-
const passwordHashSha256 = createHash("sha256")
448-
.update(this.config.pass, "utf8")
449-
.digest("hex");
465+
const serial = String(this.sseToken || "");
466+
const serialB64 = this.__bcryptjs_encodeBase64(serial, 16);
450467

451-
const serial = String(this.sseToken || "");
452-
const serialB64 = this.__bcryptjs_encodeBase64(serial, 16);
468+
const iterations = 8;
469+
let salt = "$2a$";
470+
if (iterations < 10) {
471+
salt += "0";
472+
}
473+
salt += `${iterations}$${serialB64}`;
453474

454-
const iterations = 8;
455-
let salt = "$2a$";
456-
if (iterations < 10) {
457-
salt += "0";
475+
const pwhash = bcrypt.hashSync(passwordHashSha256, salt);
476+
this.hashedPassword = pwhash.slice(salt.length);
477+
hashedPassword = this.hashedPassword;
458478
}
459-
salt += `${iterations}$${serialB64}`;
460-
461-
const pwhash = bcrypt.hashSync(passwordHashSha256, salt);
462-
const hashedPassword = pwhash.slice(salt.length);
463479

464480
// === Python: hash1 = sha256(token1 + hashedPassword)
465481
const hash1 = createHash("sha256")

0 commit comments

Comments
 (0)