align API response format with maqt.top real specs: weapon-categories, weapons, schemes, schemes_aob, favorites check, encryption key derivation
This commit is contained in:
@@ -1,15 +1,10 @@
|
||||
import crypto from 'crypto';
|
||||
|
||||
const ALGORITHM = 'aes-256-cbc';
|
||||
const KEY = (() => {
|
||||
const raw = process.env.ENCRYPTION_KEY || '0123456789abcdef0123456789abcdef';
|
||||
const buf = Buffer.from(raw, 'utf-8');
|
||||
if (buf.length !== 32) {
|
||||
console.error(`ENCRYPTION_KEY must be exactly 32 bytes, got ${buf.length}`);
|
||||
process.exit(1);
|
||||
}
|
||||
return buf;
|
||||
})();
|
||||
|
||||
// 与前端对齐: SHA-256(KEY_STRING) → 32 bytes
|
||||
const RAW_KEY = process.env.ENCRYPTION_KEY || 'maqt-delta-force-2024-secret-key-32';
|
||||
const KEY = crypto.createHash('sha256').update(RAW_KEY).digest();
|
||||
|
||||
export interface EncryptedData {
|
||||
encrypted: boolean;
|
||||
@@ -25,7 +20,7 @@ export function encrypt(text: string): EncryptedData {
|
||||
const cipher = crypto.createCipheriv(ALGORITHM, KEY, iv);
|
||||
let encrypted = cipher.update(text, 'utf-8', 'hex');
|
||||
encrypted += cipher.final('hex');
|
||||
|
||||
|
||||
return {
|
||||
encrypted: true,
|
||||
iv: iv.toString('hex'),
|
||||
@@ -33,6 +28,18 @@ export function encrypt(text: string): EncryptedData {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密整个 JSON 响应体(对齐前端 decryptData 格式)
|
||||
*/
|
||||
export function encryptResponse(payload: object): EncryptedData {
|
||||
const json = JSON.stringify(payload);
|
||||
const iv = crypto.randomBytes(16);
|
||||
const cipher = crypto.createCipheriv(ALGORITHM, KEY, iv);
|
||||
let encrypted = cipher.update(json, 'utf-8', 'hex');
|
||||
encrypted += cipher.final('hex');
|
||||
return { encrypted: true, iv: iv.toString('hex'), data: encrypted };
|
||||
}
|
||||
|
||||
/**
|
||||
* AES 解密
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user