Avoid some repeated stringToBytes-calls in the src/core/crypto.js file

Currently we repeatedly lookup, and convert to bytes, the "O" and "U" encryption-dictionary entries.
This commit is contained in:
Jonas Jenwald 2023-04-26 17:52:46 +02:00
parent 598408b7af
commit e12535457f

View File

@ -1715,9 +1715,11 @@ const CipherTransformFactory = (function CipherTransformFactoryClosure() {
throw new FormatError("invalid key length"); throw new FormatError("invalid key length");
} }
const ownerBytes = stringToBytes(dict.get("O")),
userBytes = stringToBytes(dict.get("U"));
// prepare keys // prepare keys
const ownerPassword = stringToBytes(dict.get("O")).subarray(0, 32); const ownerPassword = ownerBytes.subarray(0, 32);
const userPassword = stringToBytes(dict.get("U")).subarray(0, 32); const userPassword = userBytes.subarray(0, 32);
const flags = dict.get("P"); const flags = dict.get("P");
const revision = dict.get("R"); const revision = dict.get("R");
// meaningful when V is 4 or 5 // meaningful when V is 4 or 5
@ -1734,8 +1736,7 @@ const CipherTransformFactory = (function CipherTransformFactoryClosure() {
password = utf8StringToString(password); password = utf8StringToString(password);
} catch (ex) { } catch (ex) {
warn( warn(
"CipherTransformFactory: " + "CipherTransformFactory: Unable to convert UTF8 encoded password."
"Unable to convert UTF8 encoded password."
); );
} }
} }
@ -1755,17 +1756,11 @@ const CipherTransformFactory = (function CipherTransformFactoryClosure() {
encryptMetadata encryptMetadata
); );
} else { } else {
const ownerValidationSalt = stringToBytes(dict.get("O")).subarray( const ownerValidationSalt = ownerBytes.subarray(32, 40);
32, const ownerKeySalt = ownerBytes.subarray(40, 48);
40 const uBytes = userBytes.subarray(0, 48);
); const userValidationSalt = userBytes.subarray(32, 40);
const ownerKeySalt = stringToBytes(dict.get("O")).subarray(40, 48); const userKeySalt = userBytes.subarray(40, 48);
const uBytes = stringToBytes(dict.get("U")).subarray(0, 48);
const userValidationSalt = stringToBytes(dict.get("U")).subarray(
32,
40
);
const userKeySalt = stringToBytes(dict.get("U")).subarray(40, 48);
const ownerEncryption = stringToBytes(dict.get("OE")); const ownerEncryption = stringToBytes(dict.get("OE"));
const userEncryption = stringToBytes(dict.get("UE")); const userEncryption = stringToBytes(dict.get("UE"));
const perms = stringToBytes(dict.get("Perms")); const perms = stringToBytes(dict.get("Perms"));