Compare commits

...

4 Commits

Author SHA1 Message Date
d53ee1f860 feat: added support for QMCv2 (#11) 2023-05-14 23:15:59 +01:00
a06f58f27f chore: move key to separated file 2023-05-14 22:34:07 +01:00
175d6d0645 feat: added audio ext detection 2023-05-14 21:57:18 +01:00
c89be532a8 chroe: update sdk 2023-05-14 21:46:32 +01:00
10 changed files with 114 additions and 49 deletions

View File

@ -14,7 +14,7 @@
"@chakra-ui/react": "^2.6.1",
"@emotion/react": "^11.11.0",
"@emotion/styled": "^11.11.0",
"@jixun/libparakeet": "0.0.0-exp.12",
"@jixun/libparakeet": "0.0.0-exp.16",
"@reduxjs/toolkit": "^1.9.5",
"framer-motion": "^10.12.8",
"nanoid": "^4.0.2",

8
pnpm-lock.yaml generated
View File

@ -14,8 +14,8 @@ dependencies:
specifier: ^11.11.0
version: 11.11.0(@emotion/react@11.11.0)(@types/react@18.0.28)(react@18.2.0)
'@jixun/libparakeet':
specifier: 0.0.0-exp.12
version: 0.0.0-exp.12
specifier: 0.0.0-exp.16
version: 0.0.0-exp.16
'@reduxjs/toolkit':
specifier: ^1.9.5
version: 1.9.5(react-redux@8.0.5)(react@18.2.0)
@ -1780,8 +1780,8 @@ packages:
resolution: {integrity: sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==}
dev: true
/@jixun/libparakeet@0.0.0-exp.12:
resolution: {integrity: sha512-vtXUSDJMnirr8bz1oG29K+sfDz7T7V+tC4rnzS5Xon3wrUZq5Eth2icPMDYYC5UWEgfxEy/qr19/Em59v492nA==}
/@jixun/libparakeet@0.0.0-exp.16:
resolution: {integrity: sha512-jDj9kju0tCJyesV+yi4xcOGClrFgdarkcG/aGP3tRfonf1l8y11A8lVwbp3cWXJcrgHAn7683rTEUYitg9TfpA==}
dev: false
/@jridgewell/gen-mapping@0.3.3:

View File

@ -1,4 +1,8 @@
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>;
}

View File

@ -0,0 +1,16 @@
export default new Uint8Array([
0x77, 0x48, 0x32, 0x73, 0xde, 0xf2, 0xc0, 0xc8, 0x95, 0xec, 0x30, 0xb2, 0x51, 0xc3, 0xe1, 0xa0, 0x9e, 0xe6, 0x9d,
0xcf, 0xfa, 0x7f, 0x14, 0xd1, 0xce, 0xb8, 0xdc, 0xc3, 0x4a, 0x67, 0x93, 0xd6, 0x28, 0xc2, 0x91, 0x70, 0xca, 0x8d,
0xa2, 0xa4, 0xf0, 0x08, 0x61, 0x90, 0x7e, 0x6f, 0xa2, 0xe0, 0xeb, 0xae, 0x3e, 0xb6, 0x67, 0xc7, 0x92, 0xf4, 0x91,
0xb5, 0xf6, 0x6c, 0x5e, 0x84, 0x40, 0xf7, 0xf3, 0x1b, 0x02, 0x7f, 0xd5, 0xab, 0x41, 0x89, 0x28, 0xf4, 0x25, 0xcc,
0x52, 0x11, 0xad, 0x43, 0x68, 0xa6, 0x41, 0x8b, 0x84, 0xb5, 0xff, 0x2c, 0x92, 0x4a, 0x26, 0xd8, 0x47, 0x6a, 0x7c,
0x95, 0x61, 0xcc, 0xe6, 0xcb, 0xbb, 0x3f, 0x47, 0x58, 0x89, 0x75, 0xc3, 0x75, 0xa1, 0xd9, 0xaf, 0xcc, 0x08, 0x73,
0x17, 0xdc, 0xaa, 0x9a, 0xa2, 0x16, 0x41, 0xd8, 0xa2, 0x06, 0xc6, 0x8b, 0xfc, 0x66, 0x34, 0x9f, 0xcf, 0x18, 0x23,
0xa0, 0x0a, 0x74, 0xe7, 0x2b, 0x27, 0x70, 0x92, 0xe9, 0xaf, 0x37, 0xe6, 0x8c, 0xa7, 0xbc, 0x62, 0x65, 0x9c, 0xc2,
0x08, 0xc9, 0x88, 0xb3, 0xf3, 0x43, 0xac, 0x74, 0x2c, 0x0f, 0xd4, 0xaf, 0xa1, 0xc3, 0x01, 0x64, 0x95, 0x4e, 0x48,
0x9f, 0xf4, 0x35, 0x78, 0x95, 0x7a, 0x39, 0xd6, 0x6a, 0xa0, 0x6d, 0x40, 0xe8, 0x4f, 0xa8, 0xef, 0x11, 0x1d, 0xf3,
0x1b, 0x3f, 0x3f, 0x07, 0xdd, 0x6f, 0x5b, 0x19, 0x30, 0x19, 0xfb, 0xef, 0x0e, 0x37, 0xf0, 0x0e, 0xcd, 0x16, 0x49,
0xfe, 0x53, 0x47, 0x13, 0x1a, 0xbd, 0xa4, 0xf1, 0x40, 0x19, 0x60, 0x0e, 0xed, 0x68, 0x09, 0x06, 0x5f, 0x4d, 0xcf,
0x3d, 0x1a, 0xfe, 0x20, 0x77, 0xe4, 0xd9, 0xda, 0xf9, 0xa4, 0x2b, 0x76, 0x1c, 0x71, 0xdb, 0x00, 0xbc, 0xfd, 0x0c,
0x6c, 0xa5, 0x47, 0xf7, 0xf6, 0x00, 0x79, 0x4a, 0x11,
]);

View File

@ -1,51 +1,17 @@
import { transformBlob } from '~/decrypt-worker/util/transformBlob';
import type { CryptoBase } from '../CryptoBase';
import { loadLibParakeet, factory, BlobSink, createArrayBufferReader, TransformResult } from '@jixun/libparakeet';
const key = new Uint8Array([
0x77, 0x48, 0x32, 0x73, 0xde, 0xf2, 0xc0, 0xc8, 0x95, 0xec, 0x30, 0xb2, 0x51, 0xc3, 0xe1, 0xa0, 0x9e, 0xe6, 0x9d,
0xcf, 0xfa, 0x7f, 0x14, 0xd1, 0xce, 0xb8, 0xdc, 0xc3, 0x4a, 0x67, 0x93, 0xd6, 0x28, 0xc2, 0x91, 0x70, 0xca, 0x8d,
0xa2, 0xa4, 0xf0, 0x08, 0x61, 0x90, 0x7e, 0x6f, 0xa2, 0xe0, 0xeb, 0xae, 0x3e, 0xb6, 0x67, 0xc7, 0x92, 0xf4, 0x91,
0xb5, 0xf6, 0x6c, 0x5e, 0x84, 0x40, 0xf7, 0xf3, 0x1b, 0x02, 0x7f, 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x00, 0x79, 0x4a, 0x11,
]);
import key from './qmc_v1.key.ts';
export class QMC1Crypto implements CryptoBase {
async isSupported(_blob: Blob): Promise<boolean> {
hasSignature(): boolean {
return false;
}
async isSupported(): Promise<boolean> {
return true;
}
async decrypt(blob: Blob): Promise<Blob> {
const cleanup: (() => void)[] = [];
try {
const mod = await loadLibParakeet();
const transformer = factory.CreateQMCv1Transformer(mod, key);
cleanup.push(() => transformer.delete());
const reader = createArrayBufferReader(await blob.arrayBuffer(), mod);
cleanup.push(() => reader.delete());
const sink = new BlobSink(mod);
const writer = sink.getWriter();
cleanup.push(() => writer.delete());
const result = transformer.Transform(writer, reader);
if (result !== TransformResult.OK) {
throw new Error(`transform failed with error: ${TransformResult[result]} (${result})`);
}
return sink.collectBlob();
} finally {
cleanup.forEach((clean) => clean());
}
return transformBlob(blob, (p) => p.make.QMCv1(key));
}
}

View File

@ -0,0 +1,3 @@
export const SEED = 106;
export const ENC_V2_KEY_1 = '386ZJY!@#*$%^&)(';
export const ENC_V2_KEY_2 = '**#!(#$%&^a1cZ,T';

View File

@ -0,0 +1,17 @@
import { transformBlob } from '~/decrypt-worker/util/transformBlob';
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;
}
async isSupported(): Promise<boolean> {
return true;
}
async decrypt(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

@ -23,6 +23,10 @@ const u8Sub = (a: number, b: number) => {
};
export class XiamiCrypto implements CryptoBase {
hasSignature(): boolean {
return true;
}
async isSupported(blob: Blob): Promise<boolean> {
const headerBuffer = await blob.slice(0, 0x10).arrayBuffer();
const header = new Uint8Array(headerBuffer);

View File

@ -0,0 +1,31 @@
import { Transformer, Parakeet, TransformResult, fetchParakeet } from '@jixun/libparakeet';
export async function transformBlob(
blob: Blob,
transformerFactory: (p: Parakeet) => Transformer | Promise<Transformer>,
parakeet?: Parakeet
) {
const cleanup: (() => void)[] = [];
try {
const mod = parakeet ?? (await fetchParakeet());
const transformer = await transformerFactory(mod);
cleanup.push(() => transformer.delete());
const reader = mod.make.Reader(await blob.arrayBuffer());
cleanup.push(() => reader.delete());
const sink = mod.make.WriterSink();
const writer = sink.getWriter();
cleanup.push(() => writer.delete());
const result = transformer.Transform(writer, reader);
if (result !== TransformResult.OK) {
throw new Error(`transform failed with error: ${TransformResult[result]} (${result})`);
}
return sink.collectBlob();
} finally {
cleanup.forEach((clean) => clean());
}
}

View File

@ -2,8 +2,11 @@ import { WorkerServerBus } from '~/util/WorkerEventBus';
import { DECRYPTION_WORKER_ACTION_NAME } from './constants';
import type { CryptoFactory } from './crypto/CryptoBase';
import { fetchParakeet } from '@jixun/libparakeet';
import { XiamiCrypto } from './crypto/xiami/xiami';
import { QMC1Crypto } from './crypto/qmc/qmc_v1';
import { QMC2Crypto } from './crypto/qmc/qmc_v2';
const bus = new WorkerServerBus();
onmessage = bus.onmessage;
@ -14,16 +17,37 @@ const decryptorFactories: CryptoFactory[] = [
// QMCv1 (*.qmcflac)
() => new QMC1Crypto(),
// QMCv2 (*.mflac)
() => new QMC2Crypto(),
];
// Use first 4MiB of the file to perform check.
const TEST_FILE_HEADER_LEN = 1024 * 1024 * 4;
bus.addEventHandler(DECRYPTION_WORKER_ACTION_NAME.DECRYPT, async (blobURI) => {
const blob = await fetch(blobURI).then((r) => r.blob());
const parakeet = await fetchParakeet();
for (const factory of decryptorFactories) {
const decryptor = factory();
if (await decryptor.isSupported(blob)) {
const decrypted = await decryptor.decrypt(blob);
return { decrypted: URL.createObjectURL(decrypted) };
try {
const decryptedBlob = await decryptor.decrypt(blob);
// Check if we had a successful decryption
const header = await decryptedBlob.slice(0, TEST_FILE_HEADER_LEN).arrayBuffer();
const audioExt = parakeet.detectAudioExtension(header);
if (!decryptor.hasSignature() && audioExt === 'bin') {
// skip this decryptor result
continue;
}
return { decrypted: URL.createObjectURL(decryptedBlob), ext: audioExt };
} catch (error) {
console.error('decrypt failed: ', error);
continue;
}
}
}