fix: derive Windows image keys from local metadata

This commit is contained in:
Ray
2026-09-03 01:00:37 +08:00
parent 4075e52eec
commit 7ef991aa67
4 changed files with 152 additions and 6 deletions
+35
View File
@@ -0,0 +1,35 @@
import crypto from 'crypto'
export type DerivedV4ImageKeys = { xorKey: number; aesKey: string }
export function normalizeV4AccountId(value: string): string {
const leaf = String(value || '')
.trim()
.split(/[\\/]/)
.filter(Boolean)
.pop()
if (!leaf) return ''
const wxid = leaf.match(/^(wxid_[^_]+)/i)
if (wxid) return wxid[1]
return leaf.replace(/_[a-z0-9]{4}$/i, '')
}
export function extractKvcommCode(fileName: string): number | null {
const match = String(fileName || '').match(/^key_(\d+)_.+\.statistic$/i)
if (!match) return null
const code = Number.parseInt(match[1], 10)
if (!Number.isSafeInteger(code) || code <= 0 || code > 0xffffffff) return null
return code
}
export function deriveV4ImageKeys(code: number, accountId: string): DerivedV4ImageKeys | null {
const normalizedAccountId = normalizeV4AccountId(accountId)
if (!Number.isSafeInteger(code) || code <= 0 || code > 0xffffffff || !normalizedAccountId) {
return null
}
const digest = crypto
.createHash('md5')
.update(`${code}${normalizedAccountId}`, 'utf8')
.digest('hex')
return { xorKey: code & 0xff, aesKey: digest.slice(0, 16) }
}