INNER CODE UNIT · Python

scoped_regular_hash

i3T4AN/KADATH · kadath/engine.py:105

def scoped_regular_hash(root: Path, item: Path) -> tuple[int, str]:
    """Hash one regular file without following links or loading it into memory."""
    root = root.absolute(); item = item.absolute(); relative = item.relative_to(root)
    cursor = root
    for part in relative.parts:
        cursor = cursor / part
        if stat.S_ISLNK(cursor.lstat().st_mode): raise ValueError(f"symbolic link is not admissible evidence: {relative}")
    descriptor = os.open(item, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0))
    try:
        if not stat.S_ISREG(os.fstat(descriptor).st_mode): raise ValueError(f"non-regular evidence file: {relative}")
        hasher = hashlib.sha256(); size = 0
        with os.fdopen(descriptor, "rb", closefd=False) as stream:
            while True:
                data = stream.read(1024 * 1024)
                if not data: break
                size += len(data); hasher.update(data)
        return size, hasher.hexdigest()
    finally:

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…