2025-04-10 18:00:39 +09:00
|
|
|
import argparse
|
|
|
|
|
import hashlib
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def compute_sha256(file_path: Path, chunk_size: int = 8192) -> str:
|
|
|
|
|
sha256_hash = hashlib.sha256()
|
2025-11-30 20:32:07 +09:00
|
|
|
with file_path.open('rb') as f:
|
|
|
|
|
for chunk in iter(lambda: f.read(chunk_size), b''):
|
2025-04-10 18:00:39 +09:00
|
|
|
sha256_hash.update(chunk)
|
|
|
|
|
return sha256_hash.hexdigest()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2025-11-30 20:32:07 +09:00
|
|
|
parser = argparse.ArgumentParser(description='Compute SHA256 hash of a file')
|
|
|
|
|
parser.add_argument('file', type=Path, help='Path to file')
|
2025-04-10 18:00:39 +09:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
if not args.file.exists() or not args.file.is_file():
|
2025-11-30 20:32:07 +09:00
|
|
|
print(f'File does not exist: {args.file}')
|
2025-04-10 18:00:39 +09:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
sha256 = compute_sha256(args.file)
|
2025-11-25 23:19:45 +09:00
|
|
|
print(f"SHA256 hash for '{args.file.name}':\n{sha256}")
|
2025-04-10 18:00:39 +09:00
|
|
|
|
|
|
|
|
|
2025-11-30 20:32:07 +09:00
|
|
|
if __name__ == '__main__':
|
2025-04-10 18:00:39 +09:00
|
|
|
main()
|