The only downloader you need. 下载器的集大成者。
TypeScript asyncasynciobittorentbtcross-platformdownload-managerdownloaderfluent-designftphttp3magnetpyqtpyside6pythonqtquicreactsoftwarestreaming
from __future__ import annotations
import asyncio
import hashlib
from collections.abc import Callable
from pathlib import Path
ALGORITHMS = sorted(hashlib.algorithms_available)
COMMON_ALGORITHMS = ("md5", "sha1", "sha256", "sha512")
SHAKE_LENGTHS = {"shake_128": 32, "shake_256": 64}
CHUNK_SIZE = 4 * 1024 * 1024
async def toChecksum(path: Path, algorithm: str, onProgress: Callable[[int], None]) -> str:
hasher = hashlib.new(algorithm)
size = path.stat().st_size
done = 0
with open(path, "rb") as f:
while chunk := await asyncio.to_thread(f.read, CHUNK_SIZE):
await asyncio.to_thread(hasher.update, chunk)
done += len(chunk)
onProgress(done * 100 // size)
length = SHAKE_LENGTHS.get(algorithm)
return hasher.hexdigest(length) if length else hasher.hexdigest()
def parseChecksum(text: str) -> str:
return (text.split() or [""])[0].lower()
def matchChecksum(checksums: dict[str, str], expected: str) -> str | None:
expected = parseChecksum(expected)
return next((algorithm for algorithm, value in checksums.items() if value == expected), None)