75 lines
1.9 KiB
Python
75 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Quick hash calculator for malware samples.
|
|
Calculates MD5, SHA1, and SHA256 hashes simultaneously.
|
|
"""
|
|
|
|
import hashlib
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def calculate_hashes(file_path):
|
|
"""
|
|
Calculate MD5, SHA1, and SHA256 hashes for a file.
|
|
|
|
Args:
|
|
file_path: Path to the file
|
|
|
|
Returns:
|
|
dict: Dictionary containing all three hashes
|
|
"""
|
|
md5 = hashlib.md5()
|
|
sha1 = hashlib.sha1()
|
|
sha256 = hashlib.sha256()
|
|
|
|
try:
|
|
with open(file_path, 'rb') as f:
|
|
# Read in chunks for memory efficiency
|
|
while chunk := f.read(8192):
|
|
md5.update(chunk)
|
|
sha1.update(chunk)
|
|
sha256.update(chunk)
|
|
|
|
return {
|
|
'md5': md5.hexdigest(),
|
|
'sha1': sha1.hexdigest(),
|
|
'sha256': sha256.hexdigest()
|
|
}
|
|
except (FileNotFoundError, PermissionError, IsADirectoryError, OSError) as e:
|
|
return {'error': str(e)}
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
print("Usage: python hash_calculator.py <file_path>")
|
|
sys.exit(1)
|
|
|
|
file_path = Path(sys.argv[1])
|
|
|
|
if not file_path.exists():
|
|
print(f"Error: File '{file_path}' not found")
|
|
sys.exit(1)
|
|
|
|
if not file_path.is_file():
|
|
print(f"Error: '{file_path}' is not a file")
|
|
sys.exit(1)
|
|
|
|
print(f"Calculating hashes for: {file_path}")
|
|
print(f"File size: {file_path.stat().st_size} bytes")
|
|
print("-" * 70)
|
|
|
|
hashes = calculate_hashes(file_path)
|
|
|
|
if 'error' in hashes:
|
|
print(f"Error calculating hashes: {hashes['error']}")
|
|
sys.exit(1)
|
|
|
|
print(f"MD5: {hashes['md5']}")
|
|
print(f"SHA1: {hashes['sha1']}")
|
|
print(f"SHA256: {hashes['sha256']}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|