2021-05-23 15:06:21 +00:00
|
|
|
import {
|
|
|
|
AudioMimeType,
|
|
|
|
BytesHasPrefix,
|
|
|
|
GetArrayBuffer,
|
|
|
|
GetCoverFromFile,
|
|
|
|
GetMetaFromFile,
|
|
|
|
SniffAudioExt
|
|
|
|
} from "@/decrypt/utils.ts";
|
2021-05-23 14:29:34 +00:00
|
|
|
import {Decrypt as RawDecrypt} from "@/decrypt/raw.ts";
|
|
|
|
|
|
|
|
import {parseBlob as metaParseBlob} from "music-metadata-browser";
|
2021-05-23 15:47:01 +00:00
|
|
|
import {DecryptResult} from "@/decrypt/entity";
|
2020-04-23 10:15:07 +00:00
|
|
|
|
|
|
|
const MagicHeader = [
|
|
|
|
0x79, 0x65, 0x65, 0x6C, 0x69, 0x6F, 0x6E, 0x2D,
|
|
|
|
0x6B, 0x75, 0x77, 0x6F, 0x2D, 0x74, 0x6D, 0x65,
|
|
|
|
]
|
|
|
|
const PreDefinedKey = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk"
|
|
|
|
|
2021-05-23 15:47:01 +00:00
|
|
|
export async function Decrypt(file: File, raw_filename: string, _: string): Promise<DecryptResult> {
|
2020-04-23 10:15:07 +00:00
|
|
|
const oriData = new Uint8Array(await GetArrayBuffer(file));
|
2021-05-23 14:29:34 +00:00
|
|
|
if (!BytesHasPrefix(oriData, MagicHeader)) {
|
|
|
|
if (SniffAudioExt(oriData) === "aac") {
|
2021-05-23 15:47:01 +00:00
|
|
|
return await RawDecrypt(file, raw_filename, "aac", false)
|
2021-05-23 14:29:34 +00:00
|
|
|
}
|
2021-05-23 15:47:01 +00:00
|
|
|
throw Error("not a valid kwm file")
|
2021-05-23 14:29:34 +00:00
|
|
|
}
|
2020-04-23 10:15:07 +00:00
|
|
|
|
|
|
|
let fileKey = oriData.slice(0x18, 0x20)
|
|
|
|
let mask = createMaskFromKey(fileKey)
|
|
|
|
let audioData = oriData.slice(0x400);
|
|
|
|
let lenAudioData = audioData.length;
|
|
|
|
for (let cur = 0; cur < lenAudioData; ++cur)
|
|
|
|
audioData[cur] ^= mask[cur % 0x20];
|
|
|
|
|
|
|
|
|
2021-05-23 13:40:43 +00:00
|
|
|
const ext = SniffAudioExt(audioData);
|
2020-04-23 10:15:07 +00:00
|
|
|
const mime = AudioMimeType[ext];
|
|
|
|
let musicBlob = new Blob([audioData], {type: mime});
|
|
|
|
|
2021-05-23 14:29:34 +00:00
|
|
|
const musicMeta = await metaParseBlob(musicBlob);
|
2021-05-23 15:06:21 +00:00
|
|
|
const {title, artist} = GetMetaFromFile(raw_filename, musicMeta.common.title, musicMeta.common.artist)
|
2020-04-23 10:15:07 +00:00
|
|
|
return {
|
|
|
|
album: musicMeta.common.album,
|
2021-05-23 15:06:21 +00:00
|
|
|
picture: GetCoverFromFile(musicMeta),
|
2020-04-23 10:15:07 +00:00
|
|
|
file: URL.createObjectURL(musicBlob),
|
2021-05-24 19:06:28 +00:00
|
|
|
blob: musicBlob,
|
2021-05-23 15:06:21 +00:00
|
|
|
mime,
|
|
|
|
title,
|
|
|
|
artist,
|
|
|
|
ext
|
2020-04-23 10:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-23 14:29:34 +00:00
|
|
|
function createMaskFromKey(keyBytes: Uint8Array): Uint8Array {
|
2020-04-23 10:15:07 +00:00
|
|
|
let keyView = new DataView(keyBytes.buffer)
|
|
|
|
let keyStr = keyView.getBigUint64(0, true).toString()
|
|
|
|
let keyStrTrim = trimKey(keyStr)
|
|
|
|
let key = new Uint8Array(32)
|
|
|
|
for (let i = 0; i < 32; i++) {
|
2021-05-23 14:29:34 +00:00
|
|
|
key[i] = PreDefinedKey.charCodeAt(i) ^ keyStrTrim.charCodeAt(i)
|
2020-04-23 10:15:07 +00:00
|
|
|
}
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-23 14:29:34 +00:00
|
|
|
function trimKey(keyRaw: string): string {
|
2020-04-23 10:15:07 +00:00
|
|
|
let lenRaw = keyRaw.length;
|
|
|
|
let out = keyRaw;
|
|
|
|
if (lenRaw > 32) {
|
|
|
|
out = keyRaw.slice(0, 32)
|
|
|
|
} else if (lenRaw < 32) {
|
|
|
|
out = keyRaw.padEnd(32, keyRaw)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|