feat(kgm): kgm v5 (aka. kgg) support

This commit is contained in:
鲁树人
2025-02-25 07:05:31 +09:00
parent acb7a634b1
commit c71078f5da
24 changed files with 553 additions and 39 deletions

View File

@@ -1,11 +1,17 @@
import { getFileName } from './pathHelper';
import { SQLDatabase, SQLStatic, loadSQL } from './sqlite';
import { KuGou } from '@unlock-music/crypto';
export interface QMAndroidKeyEntry {
name: string;
ekey: string;
}
export type KugouKeyEntry = {
audioHash: string;
ekey: string;
};
export class DatabaseKeyExtractor {
private static _instance: DatabaseKeyExtractor;
@@ -52,4 +58,34 @@ export class DatabaseKeyExtractor {
db?.close();
}
}
extractKugouKeyFromEncryptedDb(buffer: ArrayBuffer): null | KugouKeyEntry[] {
const dbBuffer = new Uint8Array(buffer);
let db: SQLDatabase | null = null;
try {
KuGou.decryptDatabase(dbBuffer);
db = new this.SQL.Database(dbBuffer);
let sql: undefined | string;
if (this.hasTable(db, 'ShareFileItems')) {
sql = `select EncryptionKeyId, EncryptionKey from ShareFileItems where EncryptionKey != '' group by EncryptionKeyId`;
}
if (!sql) return null;
const result = db.exec(sql);
if (result.length === 0) {
return [];
}
const keys = result[0].values;
return keys.map(([audioHash, ekey]) => ({
// strip dir name
audioHash: String(audioHash).normalize(),
ekey: String(ekey).normalize(),
}));
} finally {
db?.close();
}
}
}