Replace loops with TypedArray.prototype.set in the CipherTransformFactory class

Also, adds `TypedArray.prototype.fill` usage in one spot.
This commit is contained in:
Jonas Jenwald 2025-03-08 15:56:44 +01:00
parent c65f1b0dae
commit 67065e43f5

View File

@ -894,21 +894,17 @@ class CipherTransformFactory {
hashData[i++] = CipherTransformFactory._defaultPasswordBytes[j++]; hashData[i++] = CipherTransformFactory._defaultPasswordBytes[j++];
} }
// as now the padded password in the hashData[0..i] // as now the padded password in the hashData[0..i]
for (j = 0, n = ownerPassword.length; j < n; ++j) { hashData.set(ownerPassword, i);
hashData[i++] = ownerPassword[j]; i += ownerPassword.length;
}
hashData[i++] = flags & 0xff; hashData[i++] = flags & 0xff;
hashData[i++] = (flags >> 8) & 0xff; hashData[i++] = (flags >> 8) & 0xff;
hashData[i++] = (flags >> 16) & 0xff; hashData[i++] = (flags >> 16) & 0xff;
hashData[i++] = (flags >>> 24) & 0xff; hashData[i++] = (flags >>> 24) & 0xff;
for (j = 0, n = fileId.length; j < n; ++j) { hashData.set(fileId, i);
hashData[i++] = fileId[j]; i += fileId.length;
}
if (revision >= 4 && !encryptMetadata) { if (revision >= 4 && !encryptMetadata) {
hashData[i++] = 0xff; hashData.fill(0xff, i, i + 4);
hashData[i++] = 0xff; i += 4;
hashData[i++] = 0xff;
hashData[i++] = 0xff;
} }
let hash = calculateMD5(hashData, 0, i); let hash = calculateMD5(hashData, 0, i);
const keyLengthInBytes = keyLength >> 3; const keyLengthInBytes = keyLength >> 3;
@ -921,12 +917,12 @@ class CipherTransformFactory {
let cipher, checkData; let cipher, checkData;
if (revision >= 3) { if (revision >= 3) {
for (i = 0; i < 32; ++i) { i = 0;
hashData[i] = CipherTransformFactory._defaultPasswordBytes[i]; hashData.set(CipherTransformFactory._defaultPasswordBytes, i);
} i += 32;
for (j = 0, n = fileId.length; j < n; ++j) { hashData.set(fileId, i);
hashData[i++] = fileId[j]; i += fileId.length;
}
cipher = new ARCFourCipher(encryptionKey); cipher = new ARCFourCipher(encryptionKey);
checkData = cipher.encryptBlock(calculateMD5(hashData, 0, i)); checkData = cipher.encryptBlock(calculateMD5(hashData, 0, i));
n = encryptionKey.length; n = encryptionKey.length;