35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
"""
|
|
backend/app/services/voucher_check/fingerprint.py
|
|
|
|
用途:根据文件名与元数据生成稳定 SHA-256 指纹。
|
|
依赖:标准库 hashlib / json。
|
|
逻辑:DMS 目录扫描与 HXJM 主体 Adapter 调用,供增量跳过比对。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
from typing import Iterable, Sequence
|
|
|
|
|
|
def build_material_fingerprint(
|
|
entries: Sequence[tuple[str, int, int]] | Iterable[tuple[str, int, int]],
|
|
) -> str:
|
|
"""
|
|
对直接子文件元数据生成 SHA-256。
|
|
核心逻辑:按文件名排序后序列化为 UTF-8 JSON,再哈希;不读取文件内容。
|
|
|
|
Args:
|
|
entries: (filename, size, mtime_ns) 列表。
|
|
|
|
Returns:
|
|
64 位十六进制 SHA-256;无文件时对空数组哈希。
|
|
"""
|
|
normalized = sorted(
|
|
[(str(name), int(size), int(mtime_ns)) for name, size, mtime_ns in entries],
|
|
key=lambda item: item[0],
|
|
)
|
|
payload = json.dumps(normalized, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
|
|
return hashlib.sha256(payload).hexdigest()
|