File Hashing๏ƒ

The file hashing module provides utilities for generating various types of file hashes and checksums for data integrity verification.

Module Overview๏ƒ

Hash Management Functions - Fixed Version Provides standardized hash functions that actually exist and work properly

Functions๏ƒ

siege_utilities.files.hashing.calculate_file_hash(file_path)[source]

Alias for get_file_hash with SHA256 - for backward compatibility

siege_utilities.files.hashing.verify_file_integrity(file_path, expected_hash, algorithm='sha256')[source]

Verify file integrity by comparing with expected hash

SECURITY: This function validates paths to prevent path traversal attacks and access to sensitive system files (through get_file_hash).

Parameters:
  • file_path โ€“ Path to the file

  • expected_hash โ€“ Expected hash value

  • algorithm โ€“ Hash algorithm used

Returns:

True if file matches expected hash, False otherwise

Raises:

PathSecurityError โ€“ If path fails security validation

Example

>>> expected = "abc123..."
>>> if verify_file_integrity("data.txt", expected):
...     print("File integrity verified")
>>>
>>> # This will raise PathSecurityError
>>> verify_file_integrity("/etc/shadow", expected)  # Sensitive file blocked
Security Changes:
  • Now validates paths via get_file_hash() to block path traversal

  • Blocks access to sensitive system files

Usage Examples๏ƒ

Basic file hashing:

import siege_utilities

# Calculate MD5 hash
md5_hash = siege_utilities.calculate_md5('document.pdf')
print(f"MD5: {md5_hash}")

# Calculate SHA256 hash
sha256_hash = siege_utilities.calculate_sha256('document.pdf')
print(f"SHA256: {sha256_hash}")

# Calculate SHA1 hash
sha1_hash = siege_utilities.calculate_sha1('document.pdf')
print(f"SHA1: {sha1_hash}")

Generic hashing with algorithm selection:

# Use generic hash function
hash_value = siege_utilities.calculate_file_hash('data.csv', algorithm='sha256')
print(f"Hash: {hash_value}")

# Available algorithms: md5, sha1, sha256, sha512
algorithms = ['md5', 'sha1', 'sha256']
hashes = {}

for algo in algorithms:
    hashes[algo] = siege_utilities.calculate_file_hash('large_file.dat', algorithm=algo)

for algo, hash_val in hashes.items():
    print(f"{algo.upper()}: {hash_val}")

File integrity verification:

Batch processing:

Unit Tests๏ƒ

The file hashing module has comprehensive test coverage:

Test Results: All file hashing tests pass successfully with comprehensive coverage.