Create a stable page_hash of the path_or_stream of a file
(path_or_stream: Union[BytesIO, Path])
| 17 | |
| 18 | |
| 19 | def create_file_hash(path_or_stream: Union[BytesIO, Path]) -> str: |
| 20 | """Create a stable page_hash of the path_or_stream of a file""" |
| 21 | |
| 22 | block_size = 65536 |
| 23 | hasher = hashlib.sha256() |
| 24 | |
| 25 | def _hash_buf(binary_stream): |
| 26 | buf = binary_stream.read(block_size) # read and page_hash in chunks |
| 27 | while len(buf) > 0: |
| 28 | hasher.update(buf) |
| 29 | buf = binary_stream.read(block_size) |
| 30 | |
| 31 | if isinstance(path_or_stream, Path): |
| 32 | with path_or_stream.open("rb") as afile: |
| 33 | _hash_buf(afile) |
| 34 | elif isinstance(path_or_stream, BytesIO): |
| 35 | _hash_buf(path_or_stream) |
| 36 | |
| 37 | return hasher.hexdigest() |
| 38 | |
| 39 | |
| 40 | def create_hash(string: str): |