31 lines
791 B
Python
31 lines
791 B
Python
#!/usr/bin/env python3
|
|
"""File checksum utility - calculates SHA256 checksum of a file."""
|
|
import sys
|
|
import hashlib
|
|
|
|
|
|
def sha256_checksum(filepath):
|
|
"""Calculate SHA256 checksum of a file, return hex digest."""
|
|
sha256 = hashlib.sha256()
|
|
with open(filepath, 'rb') as f:
|
|
for chunk in iter(lambda: f.read(8192), b''):
|
|
sha256.update(chunk)
|
|
return sha256.hexdigest()
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) != 2:
|
|
print("Usage: checksum.py <filepath>")
|
|
sys.exit(1)
|
|
filepath = sys.argv[1]
|
|
try:
|
|
result = sha256_checksum(filepath)
|
|
print(result)
|
|
return 0
|
|
except FileNotFoundError:
|
|
print(f"Error: File '{filepath}' not found", file=sys.stderr)
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main()) |