From cc63ffa6bbdb0ead3cd03ec8294dc52cfc13e3e8 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 13 Mar 2025 09:57:50 +0100 Subject: [PATCH] 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. --- src/core/crypto.js | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/core/crypto.js b/src/core/crypto.js index c5ebdefbc..e11f579e0 100644 --- a/src/core/crypto.js +++ b/src/core/crypto.js @@ -916,23 +916,15 @@ class CipherTransformFactory { cipher = new ARCFourCipher(derivedKey); checkData = cipher.encryptBlock(checkData); } - for (j = 0, n = checkData.length; j < n; ++j) { - if (userPassword[j] !== checkData[j]) { - return null; - } - } } else { cipher = new ARCFourCipher(encryptionKey); checkData = cipher.encryptBlock( 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) {