diff options
| author | 2024-03-28 22:15:54 +0800 | |
|---|---|---|
| committer | 2024-03-28 22:15:54 +0800 | |
| commit | abbcddf3fababd59f60193eaa7d787f32f5060e7 (patch) | |
| tree | 3584b357e28ffb38ab040bc10d7fbc345d2d5c33 /src | |
| parent | ed771d42dd9cceb14be6ba99b4070b7eb71b82b0 (diff) | |
| download | ipm-abbcddf3fababd59f60193eaa7d787f32f5060e7.tar.gz ipm-abbcddf3fababd59f60193eaa7d787f32f5060e7.zip | |
feat(hash): use hex hash string instead of hash bytes
Diffstat (limited to 'src')
| -rw-r--r-- | src/ipm/utils/freeze.py | 21 | ||||
| -rw-r--r-- | src/ipm/utils/hash.py | 4 |
2 files changed, 8 insertions, 17 deletions
diff --git a/src/ipm/utils/freeze.py b/src/ipm/utils/freeze.py index c9cf9b0..2d30a0f 100644 --- a/src/ipm/utils/freeze.py +++ b/src/ipm/utils/freeze.py @@ -51,13 +51,8 @@ def build_ipk(ipk: InfiniProject, echo: bool = False) -> InfiniFrozenPackage: update("创建 SHA256 验证文件...", echo) hash_bytes = ifp_hash(ifp_path) - info(f"文件 SHA256 值为 [purple]{hash_bytes.hex()}[/purple].", echo) + info(f"文件 SHA256 值为 [purple]{hash_bytes}[/purple].", echo) - # update("正在生成 id.xml 文件...", echo) - # _freeze.create_xml_file(project, dist_path) - # success("xml 索引文件生成完毕.", echo) - - (dist_path / ipk.hash_name).write_bytes(hash_bytes) success( f"包 [bold green]{ipk.name}[/bold green] [yellow]{ipk.version}[/yellow] 构建成功.", echo, @@ -67,23 +62,19 @@ def build_ipk(ipk: InfiniProject, echo: bool = False) -> InfiniFrozenPackage: def extract_ipk( - source_path: StrPath, dist_path: StrPath, echo: bool = False + source_path: StrPath, dist_path: StrPath, hash: Optional[str], echo: bool = False ) -> Optional[InfiniProject]: ifp_path = Path(source_path).resolve() dist_path = Path(dist_path).resolve() - hash_path = ifp_path.parent / (ifp_path.name + ".hash") - - if not hash_path.exists(): - raise VerifyFailed(f"哈希文件 [blue]{hash_path}[/blue] 不存在!") - update("文件校验...") - if not ifp_verify(ifp_path, hash_path.read_bytes()): + update("文件校验...", echo) + if not ifp_verify(ifp_path, hash): raise VerifyFailed("文件完整性验证失败!") - success("文件校验成功.") + success("文件校验成功.", echo) temp_dir = tempfile.TemporaryDirectory() temp_path = Path(temp_dir.name).resolve() / "ifp" - info(f"创建临时目录 [blue]{temp_dir}[/blue].") + info(f"创建临时目录 [blue]{temp_dir}[/blue].", echo) update(f"解压 [blue]{ifp_path}[/blue]...", echo) _freeze.extract_tar_gz(str(ifp_path), str(temp_path)) diff --git a/src/ipm/utils/hash.py b/src/ipm/utils/hash.py index 7409d37..c5cc990 100644 --- a/src/ipm/utils/hash.py +++ b/src/ipm/utils/hash.py @@ -3,12 +3,12 @@ from pathlib import Path import hashlib -def ifp_hash(lfp_path: StrPath, block_size=65536) -> bytes: +def ifp_hash(lfp_path: StrPath, block_size=65536) -> str: sha256 = hashlib.sha256() with Path(lfp_path).resolve().open("rb") as file: for block in iter(lambda: file.read(block_size), b""): sha256.update(block) - return sha256.digest() + return sha256.digest().hex() def ifp_verify(lfp_path, expected_hash) -> bool: |
