Files
NapCatQQ/packages/napcat-database/src/crypto.ts
T
手瓜一十雪 b9dcedacf5 Add napcat-database and DatabaseApi support
Introduce a new napcat-database package that implements SQLCipher/NTQQ DB handling (constants, key derivation, page decryption, file-level decrypt, and a reader that can list tables using node:sqlite). Add NTQQDatabaseApi to napcat-core (APIs export, NapCatCore field & container) to expose high-level helpers (isSqliteAvailable, readDatabase, scanDatabases, decryptDatabase, formatResults). Add protobuf definitions for OidbSvcTrpcTcp.0xcde_2 and hook packet handlers in framework and shell to capture the DB passphrase and set core.dbPassphrase. Update package.json files and vite configs to include napcat-database/napcat-protobuf and allow node:sqlite, and update pnpm-lock accordingly.
2026-03-04 10:56:18 +08:00

30 lines
771 B
TypeScript

/**
* 密钥派生
* enc_key = PBKDF2_HMAC_SHA512(passphrase, salt, 4000, 32)
* hmac_key = PBKDF2_HMAC_SHA512(enc_key, salt ^ 0x3a, 2, 32)
*/
import { pbkdf2Sync } from 'node:crypto';
import {
KEY_SIZE,
KDF_ITERATIONS,
FAST_ITERATIONS,
HMAC_MASK,
} from './constants';
export interface DerivedKeys {
encKey: Buffer;
hmacKey: Buffer;
}
export function deriveKeys (passphrase: Buffer, salt: Buffer): DerivedKeys {
const encKey = pbkdf2Sync(passphrase, salt, KDF_ITERATIONS, KEY_SIZE, 'sha512');
const hmacSalt = Buffer.alloc(salt.length);
for (let i = 0; i < salt.length; i++) {
hmacSalt[i] = salt[i]! ^ HMAC_MASK;
}
const hmacKey = pbkdf2Sync(encKey, hmacSalt, FAST_ITERATIONS, KEY_SIZE, 'sha512');
return { encKey, hmacKey };
}