mirror of
https://github.com/daydreamer-json/ak-endfield-api-archive.git
synced 2026-04-13 11:02:21 +02:00
refactor: deduplicate cipher logic
This commit is contained in:
@@ -1,28 +1,20 @@
|
|||||||
function decryptResIndex(encData: Uint8Array, key: string): Uint8Array {
|
function processResIndex(data: Uint8Array, key: string, isEncrypt: boolean): Uint8Array {
|
||||||
const keyBytes = Buffer.from(key, 'utf-8');
|
const keyBytes = Buffer.from(key, 'utf-8');
|
||||||
const keyLength = keyBytes.length;
|
const result = new Uint8Array(data.length);
|
||||||
const result = new Uint8Array(encData.length);
|
|
||||||
for (let i = 0; i < encData.length; i++) {
|
for (let i = 0; i < data.length; i++) {
|
||||||
const encByte = encData[i]!;
|
const keyByte = keyBytes[i % keyBytes.length]!;
|
||||||
const keyByte = keyBytes[i % keyLength]!;
|
result[i] = isEncrypt ? (data[i]! + keyByte) % 256 : (data[i]! - keyByte + 256) % 256;
|
||||||
result[i] = (encByte - keyByte + 256) % 256;
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function decryptResIndex(encData: Uint8Array, key: string): Uint8Array {
|
||||||
|
return processResIndex(encData, key, false);
|
||||||
|
}
|
||||||
|
|
||||||
function encryptResIndex(plainData: Uint8Array, key: string): Uint8Array {
|
function encryptResIndex(plainData: Uint8Array, key: string): Uint8Array {
|
||||||
const keyBytes = Buffer.from(key, 'utf-8');
|
return processResIndex(plainData, key, true);
|
||||||
const keyLength = keyBytes.length;
|
|
||||||
const result = new Uint8Array(plainData.length);
|
|
||||||
for (let i = 0; i < plainData.length; i++) {
|
|
||||||
const plainByte = plainData[i]!;
|
|
||||||
const keyByte = keyBytes[i % keyLength]!;
|
|
||||||
result[i] = (plainByte + keyByte) % 256;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default { decryptResIndex, encryptResIndex };
|
||||||
decryptResIndex,
|
|
||||||
encryptResIndex,
|
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user