发布 0.3.0 并增加 RSA 签名验证
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { MAX_FIRMWARE_FILE_SIZE } from '@/lib/firmware'
|
||||
import {
|
||||
buildFirmwareHashInput,
|
||||
type FirmwareHashByteOrder,
|
||||
} from '@/lib/firmwareHash'
|
||||
import { concatenateBlocks, parseIntelHex } from '@/lib/intelHex'
|
||||
|
||||
export type SignatureFileMode = 'raw-file' | 'intel-hex' | 'intel-hex-block-header'
|
||||
|
||||
export interface ParsedSignatureFile {
|
||||
data: Uint8Array
|
||||
dataBytes: number
|
||||
effectiveDataBytes: number
|
||||
headerBytes: number
|
||||
blockCount?: number
|
||||
warnings: string[]
|
||||
}
|
||||
|
||||
export async function parseSignatureFile(
|
||||
file: File,
|
||||
mode: SignatureFileMode,
|
||||
byteOrder: FirmwareHashByteOrder = 'big-endian',
|
||||
): Promise<ParsedSignatureFile> {
|
||||
if (file.size > MAX_FIRMWARE_FILE_SIZE) throw new Error('文件不能超过 64 MiB')
|
||||
if (mode === 'raw-file') {
|
||||
const data = new Uint8Array(await file.arrayBuffer())
|
||||
return { data, dataBytes: data.length, effectiveDataBytes: data.length, headerBytes: 0, warnings: [] }
|
||||
}
|
||||
if (mode !== 'intel-hex' && mode !== 'intel-hex-block-header') throw new Error('文件数据模式无效')
|
||||
const parsed = parseIntelHex(await file.text())
|
||||
const headerBytes = mode === 'intel-hex-block-header' ? parsed.blocks.length * 8 : 0
|
||||
if (parsed.totalBytes + headerBytes > MAX_FIRMWARE_FILE_SIZE) {
|
||||
throw new Error('加入 Block 校验头后的待验签数据不能超过 64 MiB')
|
||||
}
|
||||
const data = mode === 'intel-hex-block-header'
|
||||
? buildFirmwareHashInput(parsed.blocks, 'block-header', byteOrder)
|
||||
: concatenateBlocks(parsed.blocks)
|
||||
return {
|
||||
data,
|
||||
dataBytes: data.length,
|
||||
effectiveDataBytes: parsed.totalBytes,
|
||||
headerBytes,
|
||||
blockCount: parsed.blocks.length,
|
||||
warnings: parsed.warnings,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user