♻ Extract updateKeys into separate file

This commit is contained in:
C-3PO 2018-06-24 00:53:03 +02:00
parent 145e2ca2cb
commit a2044887e8
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
3 changed files with 33 additions and 26 deletions

View file

@ -1,16 +1,16 @@
import getCrc from './getCrc'; import updateKeys from './updateKeys';
import int32Mul from './int32Mul';
export default function decryptFile(dv: DataView, length: number, [key0, key1, key2]: [number, number, number]) { export default function decryptFile(dv: DataView, length: number, [key0, key1, key2]: [number, number, number]) {
for (let j = 0; j < length; j += 1) { for (let i = 0; i < length; i += 1) {
let testChar = dv.getUint8(j); //read and decrypt byte
const keyPart = (key2 | 2) & 0xFFFF; let curChar = dv.getUint8(i);
const decryptedByte = (keyPart * (keyPart ^ 1)) >>> 8; const keyPart = (key2 | 2) & 0xFFFF;
testChar ^= decryptedByte & 0xFF; const decryptedByte = (keyPart * (keyPart ^ 1)) >>> 8;
key0 = getCrc(key0, testChar); curChar ^= decryptedByte & 0xFF;
key1 = ((int32Mul(((key1 + (key0 & 0xFF)) >>> 0), 134775813) >>> 0) + 1) >>> 0; dv.setUint8(i, curChar);
key2 = getCrc(key2, key1 >>> 24);
dv.setUint8(j, testChar); //update keys
[key0, key1, key2] = updateKeys([key0, key1, key2], curChar);
} }
//if it was decrypted, we skip the first 12 bytes (random encryption header) //if it was decrypted, we skip the first 12 bytes (random encryption header)
return new DataView(dv.buffer, 12); return new DataView(dv.buffer, 12);

View file

@ -1,22 +1,19 @@
import getCrc from './getCrc'; import updateKeys from './updateKeys';
import int32Mul from './int32Mul';
export default function getDecryptionKeys(password: Uint8Array) { export default function getDecryptionKeys(password: Uint8Array): [number, number, number] {
const keys: [number, number, number] = [0x12345678, 0x23456789, 0x34567890]; let [key0, key1, key2] = [0x12345678, 0x23456789, 0x34567890];
const passwordLength = password.length; const passwordLength = password.length;
//update keys //read through password
for (let i = 0; i < passwordLength; i++) { for (let i = 0; i < passwordLength; i++) {
if (password[i] === 0) { break; } const curChar = password[i];
keys[0] = getCrc(keys[0], password[i]);
keys[1] = //Exit early if there's a zero byte in the password
(( if (curChar === 0) { break; }
int32Mul((
((keys[1] >>> 0) + (keys[0] & 0xFF)) >>> 0 //update keys
), 134775813) >>> 0 [key0, key1, key2] = updateKeys([key0, key1, key2], curChar);
) + 1) >>> 0;
keys[2] = getCrc(keys[2] >>> 0, keys[1] >>> 24);
} }
return keys; return [key0, key1, key2];
} }

View file

@ -0,0 +1,10 @@
import getCrc from './getCrc';
import int32Mul from './int32Mul';
export default function updateKeys([key0, key1, key2]: [number, number, number], curChar: number): [number, number, number] {
key0 = getCrc(key0, curChar);
key1 = ((int32Mul(((key1 + (key0 & 0xFF)) >>> 0), 134775813) >>> 0) + 1) >>> 0;
key2 = getCrc(key2, key1 >>> 24);
return [key0, key1, key2];
}