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:
parent
061637d3f4
commit
9a9a5b2365
@ -17,6 +17,7 @@
|
|||||||
import {
|
import {
|
||||||
bytesToString,
|
bytesToString,
|
||||||
FormatError,
|
FormatError,
|
||||||
|
isArrayEqual,
|
||||||
PasswordException,
|
PasswordException,
|
||||||
PasswordResponses,
|
PasswordResponses,
|
||||||
stringToBytes,
|
stringToBytes,
|
||||||
@ -1262,18 +1263,6 @@ class AES256Cipher extends AESBaseCipher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var PDF17 = (function PDF17Closure() {
|
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
|
// eslint-disable-next-line no-shadow
|
||||||
function PDF17() {}
|
function PDF17() {}
|
||||||
|
|
||||||
@ -1289,7 +1278,7 @@ var PDF17 = (function PDF17Closure() {
|
|||||||
hashData.set(ownerValidationSalt, password.length);
|
hashData.set(ownerValidationSalt, password.length);
|
||||||
hashData.set(userBytes, password.length + ownerValidationSalt.length);
|
hashData.set(userBytes, password.length + ownerValidationSalt.length);
|
||||||
var result = calculateSHA256(hashData, 0, hashData.length);
|
var result = calculateSHA256(hashData, 0, hashData.length);
|
||||||
return compareByteArrays(result, ownerPassword);
|
return isArrayEqual(result, ownerPassword);
|
||||||
},
|
},
|
||||||
checkUserPassword: function PDF17_checkUserPassword(
|
checkUserPassword: function PDF17_checkUserPassword(
|
||||||
password,
|
password,
|
||||||
@ -1300,7 +1289,7 @@ var PDF17 = (function PDF17Closure() {
|
|||||||
hashData.set(password, 0);
|
hashData.set(password, 0);
|
||||||
hashData.set(userValidationSalt, password.length);
|
hashData.set(userValidationSalt, password.length);
|
||||||
var result = calculateSHA256(hashData, 0, hashData.length);
|
var result = calculateSHA256(hashData, 0, hashData.length);
|
||||||
return compareByteArrays(result, userPassword);
|
return isArrayEqual(result, userPassword);
|
||||||
},
|
},
|
||||||
getOwnerKey: function PDF17_getOwnerKey(
|
getOwnerKey: function PDF17_getOwnerKey(
|
||||||
password,
|
password,
|
||||||
@ -1385,18 +1374,6 @@ var PDF20 = (function PDF20Closure() {
|
|||||||
// eslint-disable-next-line no-shadow
|
// eslint-disable-next-line no-shadow
|
||||||
function PDF20() {}
|
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 = {
|
PDF20.prototype = {
|
||||||
hash: function PDF20_hash(password, concatBytes, userBytes) {
|
hash: function PDF20_hash(password, concatBytes, userBytes) {
|
||||||
return calculatePDF20Hash(password, concatBytes, userBytes);
|
return calculatePDF20Hash(password, concatBytes, userBytes);
|
||||||
@ -1412,7 +1389,7 @@ var PDF20 = (function PDF20Closure() {
|
|||||||
hashData.set(ownerValidationSalt, password.length);
|
hashData.set(ownerValidationSalt, password.length);
|
||||||
hashData.set(userBytes, password.length + ownerValidationSalt.length);
|
hashData.set(userBytes, password.length + ownerValidationSalt.length);
|
||||||
var result = calculatePDF20Hash(password, hashData, userBytes);
|
var result = calculatePDF20Hash(password, hashData, userBytes);
|
||||||
return compareByteArrays(result, ownerPassword);
|
return isArrayEqual(result, ownerPassword);
|
||||||
},
|
},
|
||||||
checkUserPassword: function PDF20_checkUserPassword(
|
checkUserPassword: function PDF20_checkUserPassword(
|
||||||
password,
|
password,
|
||||||
@ -1423,7 +1400,7 @@ var PDF20 = (function PDF20Closure() {
|
|||||||
hashData.set(password, 0);
|
hashData.set(password, 0);
|
||||||
hashData.set(userValidationSalt, password.length);
|
hashData.set(userValidationSalt, password.length);
|
||||||
var result = calculatePDF20Hash(password, hashData, []);
|
var result = calculatePDF20Hash(password, hashData, []);
|
||||||
return compareByteArrays(result, userPassword);
|
return isArrayEqual(result, userPassword);
|
||||||
},
|
},
|
||||||
getOwnerKey: function PDF20_getOwnerKey(
|
getOwnerKey: function PDF20_getOwnerKey(
|
||||||
password,
|
password,
|
||||||
|
@ -884,9 +884,12 @@ function isArrayEqual(arr1, arr2) {
|
|||||||
if (arr1.length !== arr2.length) {
|
if (arr1.length !== arr2.length) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return arr1.every(function (element, index) {
|
for (let i = 0, ii = arr1.length; i < ii; i++) {
|
||||||
return element === arr2[index];
|
if (arr1[i] !== arr2[i]) {
|
||||||
});
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getModificationDate(date = new Date()) {
|
function getModificationDate(date = new Date()) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user