From 9a9a5b236545838436169fccc6ac7658e04eb7f6 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 26 Feb 2021 15:51:32 +0100 Subject: [PATCH] Replace the `compareByteArrays` functions, in `src/core/crypto.js`, with the `isArrayEqual` helper function The `compareByteArrays` is first of all duplicated in multiple closures in the `src/core/crypto.js` file. Secondly, despite its name, it's also functionally equivalent to the now existing `isArrayEqual` helper function. The `isArrayEqual` helper function is changed to use a standard `for`-loop, rather than `Array.prototype.every`, since that ought to be slightly more efficient given that we're now using it with (potentially) larger data. --- src/core/crypto.js | 33 +++++---------------------------- src/shared/util.js | 9 ++++++--- 2 files changed, 11 insertions(+), 31 deletions(-) diff --git a/src/core/crypto.js b/src/core/crypto.js index 45d439a2e..dfd88f06c 100644 --- a/src/core/crypto.js +++ b/src/core/crypto.js @@ -17,6 +17,7 @@ import { bytesToString, FormatError, + isArrayEqual, PasswordException, PasswordResponses, stringToBytes, @@ -1262,18 +1263,6 @@ class AES256Cipher extends AESBaseCipher { } var PDF17 = (function PDF17Closure() { - function compareByteArrays(array1, array2) { - if (array1.length !== array2.length) { - return false; - } - for (var i = 0; i < array1.length; i++) { - if (array1[i] !== array2[i]) { - return false; - } - } - return true; - } - // eslint-disable-next-line no-shadow function PDF17() {} @@ -1289,7 +1278,7 @@ var PDF17 = (function PDF17Closure() { hashData.set(ownerValidationSalt, password.length); hashData.set(userBytes, password.length + ownerValidationSalt.length); var result = calculateSHA256(hashData, 0, hashData.length); - return compareByteArrays(result, ownerPassword); + return isArrayEqual(result, ownerPassword); }, checkUserPassword: function PDF17_checkUserPassword( password, @@ -1300,7 +1289,7 @@ var PDF17 = (function PDF17Closure() { hashData.set(password, 0); hashData.set(userValidationSalt, password.length); var result = calculateSHA256(hashData, 0, hashData.length); - return compareByteArrays(result, userPassword); + return isArrayEqual(result, userPassword); }, getOwnerKey: function PDF17_getOwnerKey( password, @@ -1385,18 +1374,6 @@ var PDF20 = (function PDF20Closure() { // eslint-disable-next-line no-shadow function PDF20() {} - function compareByteArrays(array1, array2) { - if (array1.length !== array2.length) { - return false; - } - for (var i = 0; i < array1.length; i++) { - if (array1[i] !== array2[i]) { - return false; - } - } - return true; - } - PDF20.prototype = { hash: function PDF20_hash(password, concatBytes, userBytes) { return calculatePDF20Hash(password, concatBytes, userBytes); @@ -1412,7 +1389,7 @@ var PDF20 = (function PDF20Closure() { hashData.set(ownerValidationSalt, password.length); hashData.set(userBytes, password.length + ownerValidationSalt.length); var result = calculatePDF20Hash(password, hashData, userBytes); - return compareByteArrays(result, ownerPassword); + return isArrayEqual(result, ownerPassword); }, checkUserPassword: function PDF20_checkUserPassword( password, @@ -1423,7 +1400,7 @@ var PDF20 = (function PDF20Closure() { hashData.set(password, 0); hashData.set(userValidationSalt, password.length); var result = calculatePDF20Hash(password, hashData, []); - return compareByteArrays(result, userPassword); + return isArrayEqual(result, userPassword); }, getOwnerKey: function PDF20_getOwnerKey( password, diff --git a/src/shared/util.js b/src/shared/util.js index a5fbd5220..c175f0a2b 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -884,9 +884,12 @@ function isArrayEqual(arr1, arr2) { if (arr1.length !== arr2.length) { return false; } - return arr1.every(function (element, index) { - return element === arr2[index]; - }); + for (let i = 0, ii = arr1.length; i < ii; i++) { + if (arr1[i] !== arr2[i]) { + return false; + } + } + return true; } function getModificationDate(date = new Date()) {