Reduce duplication when checking the userPassword, in CipherTransformFactory.prototype.#prepareKeyData

Currently we duplicate the exact same code in both the `if`- and `else`-branches, which seems unnecessary, and we can also replace the manual loop.
This commit is contained in:
Jonas Jenwald 2025-03-13 09:57:50 +01:00
parent a8ad7d6485
commit cc63ffa6bb

View File

@ -916,23 +916,15 @@ class CipherTransformFactory {
cipher = new ARCFourCipher(derivedKey); cipher = new ARCFourCipher(derivedKey);
checkData = cipher.encryptBlock(checkData); checkData = cipher.encryptBlock(checkData);
} }
for (j = 0, n = checkData.length; j < n; ++j) {
if (userPassword[j] !== checkData[j]) {
return null;
}
}
} else { } else {
cipher = new ARCFourCipher(encryptionKey); cipher = new ARCFourCipher(encryptionKey);
checkData = cipher.encryptBlock( checkData = cipher.encryptBlock(
CipherTransformFactory._defaultPasswordBytes CipherTransformFactory._defaultPasswordBytes
); );
for (j = 0, n = checkData.length; j < n; ++j) {
if (userPassword[j] !== checkData[j]) {
return null;
}
}
} }
return encryptionKey; return checkData.every((data, k) => userPassword[k] === data)
? encryptionKey
: null;
} }
#decodeUserPassword(password, ownerPassword, revision, keyLength) { #decodeUserPassword(password, ownerPassword, revision, keyLength) {