feat: print performance logs to console.

This commit is contained in:
鲁树人
2023-05-21 17:58:54 +01:00
parent c4e3999546
commit 4cfc672646
12 changed files with 171 additions and 92 deletions

View File

@@ -1,10 +1,10 @@
export interface CryptoBase {
/**
* When returning false, a successful decryption should be checked by its decrypted content instead.
*/
hasSignature(): boolean;
isSupported(blob: Blob): Promise<boolean>;
decrypt(blob: Blob): Promise<Blob>;
cryptoName: string;
checkByDecryptHeader: boolean;
decryptTargetAudio: boolean;
checkBySignature?: (buffer: ArrayBuffer) => Promise<boolean>;
decrypt(buffer: ArrayBuffer, blob: Blob): Promise<Blob | ArrayBuffer>;
}
export type CryptoFactory = () => CryptoBase;

View File

@@ -0,0 +1,19 @@
import { CryptoFactory } from './CryptoBase';
import { QMC1Crypto } from './qmc/qmc_v1';
import { QMC2Crypto } from './qmc/qmc_v2';
import { XiamiCrypto } from './xiami/xiami';
export const allCryptoFactories: CryptoFactory[] = [
// Xiami (*.xm)
() => new XiamiCrypto(),
// QMCv2 (*.mflac)
() => new QMC2Crypto(),
// Crypto that does not implement "checkBySignature" or need to decrypt the entire file and then check audio type,
// should be moved to the bottom of the list for performance reasons.
// QMCv1 (*.qmcflac)
() => new QMC1Crypto(),
];

View File

@@ -3,15 +3,11 @@ import type { CryptoBase } from '../CryptoBase';
import key from './qmc_v1.key.ts';
export class QMC1Crypto implements CryptoBase {
hasSignature(): boolean {
return false;
}
cryptoName = 'QMCv1';
checkByDecryptHeader = true;
decryptTargetAudio = true;
async isSupported(): Promise<boolean> {
return true;
}
async decrypt(blob: Blob): Promise<Blob> {
async decrypt(_buffer: ArrayBuffer, blob: Blob): Promise<Blob> {
return transformBlob(blob, (p) => p.make.QMCv1(key));
}
}

View File

@@ -3,15 +3,11 @@ import type { CryptoBase } from '../CryptoBase';
import { SEED, ENC_V2_KEY_1, ENC_V2_KEY_2 } from './qmc_v2.key.ts';
export class QMC2Crypto implements CryptoBase {
hasSignature(): boolean {
return false;
}
cryptoName = 'QMCv2';
checkByDecryptHeader = false;
decryptTargetAudio = true;
async isSupported(): Promise<boolean> {
return true;
}
async decrypt(blob: Blob): Promise<Blob> {
async decrypt(_buffer: ArrayBuffer, blob: Blob): Promise<Blob> {
return transformBlob(blob, (p) => p.make.QMCv2(p.make.QMCv2FooterParser(SEED, ENC_V2_KEY_1, ENC_V2_KEY_2)));
}
}

View File

@@ -11,8 +11,9 @@
import type { CryptoBase } from '../CryptoBase';
const XIAMI_FILE_MAGIC = new Uint8Array('ifmt'.split('').map((x) => x.charCodeAt(0)));
const XIAMI_EXPECTED_PADDING = new Uint8Array([0xfe, 0xfe, 0xfe, 0xfe]);
// little endian
const XIAMI_FILE_MAGIC = 0x746d6669;
const XIAMI_EXPECTED_PADDING = 0xfefefefe;
const u8Sub = (a: number, b: number) => {
if (a > b) {
@@ -23,29 +24,25 @@ const u8Sub = (a: number, b: number) => {
};
export class XiamiCrypto implements CryptoBase {
hasSignature(): boolean {
return true;
cryptoName = 'Xiami';
checkByDecryptHeader = false;
decryptTargetAudio = true;
async checkBySignature(buffer: ArrayBuffer): Promise<boolean> {
const header = new DataView(buffer);
return header.getUint32(0x00, true) === XIAMI_FILE_MAGIC && header.getUint32(0x08, true) === XIAMI_EXPECTED_PADDING;
}
async isSupported(blob: Blob): Promise<boolean> {
const headerBuffer = await blob.slice(0, 0x10).arrayBuffer();
const header = new Uint8Array(headerBuffer);
return (
header.slice(0x00, 0x04).every((b, i) => b === XIAMI_FILE_MAGIC[i]) &&
header.slice(0x08, 0x0c).every((b, i) => b === XIAMI_EXPECTED_PADDING[i])
);
}
async decrypt(blob: Blob): Promise<Blob> {
const headerBuffer = await blob.slice(0, 0x10).arrayBuffer();
async decrypt(src: ArrayBuffer): Promise<ArrayBuffer> {
const headerBuffer = src.slice(0, 0x10);
const header = new Uint8Array(headerBuffer);
const key = u8Sub(header[0x0f], 1);
const plainTextSize = header[0x0c] | (header[0x0d] << 8) | (header[0x0e] << 16);
const decrypted = new Uint8Array(await blob.slice(0x10).arrayBuffer());
const decrypted = new Uint8Array(src.slice(0x10));
for (let i = decrypted.byteLength - 1; i >= plainTextSize; i--) {
decrypted[i] = u8Sub(key, decrypted[i]);
}
return new Blob([decrypted]);
return decrypted;
}
}