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.
This commit is contained in:
Jonas Jenwald 2021-02-26 15:51:32 +01:00
parent 061637d3f4
commit 9a9a5b2365
2 changed files with 11 additions and 31 deletions

View File

@ -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,

View File

@ -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()) {