feat: import ekey from Android db (#20)

This commit is contained in:
鲁树人
2023-06-11 16:21:10 +01:00
parent ec27a6f699
commit b78399eddb
16 changed files with 1080 additions and 108 deletions

View File

@@ -0,0 +1,44 @@
import { SQLDatabase, SQLStatic, loadSQL } from './sqlite';
export interface QMAndroidKeyEntry {
name: string;
key: string;
}
export class DatabaseKeyExtractor {
private static _instance: DatabaseKeyExtractor;
static async getInstance() {
if (!DatabaseKeyExtractor._instance) {
DatabaseKeyExtractor._instance = new DatabaseKeyExtractor(await loadSQL());
}
return DatabaseKeyExtractor._instance;
}
constructor(private SQL: SQLStatic) {}
private hasTable(db: SQLDatabase, name: string): boolean {
const tables = db.exec('SELECT name FROM sqlite_master WHERE type="table"')[0].values.map((x) => x[0]);
return tables.includes(name);
}
extractQmAndroidDbKeys(buffer: ArrayBuffer): null | QMAndroidKeyEntry[] {
let db: SQLDatabase | null = null;
try {
db = new this.SQL.Database(new Uint8Array(buffer));
if (!this.hasTable(db, 'audio_file_ekey_table')) {
return null;
}
const keys = db.exec('select file_path, ekey from `audio_file_ekey_table`')[0].values;
return keys.map(([path, key]) => ({
// strip dir name
name: String(path).replace(/.+\//, ''),
key: String(key),
}));
} finally {
db?.close();
}
}
}