A robust Python-based tool for verifying the integrity of code directories by comparing file hashes and detecting differences between reference and target code structures. Features comprehensive help system, multiple hash algorithms, and automated service integration.
- Hash-based integrity verification: Calculate and compare MD5, SHA256, or SHA1 hashes of files and directories
- Batch folder analysis: Analyze multiple code directories against a reference
- File difference detection: Identify specific file differences between directories
- Pattern-based file ignoring: Skip files matching specified patterns during analysis
- Service integration: Automatically start/stop system services based on integrity results
- Colored output: Visual feedback with color-coded results (PASS/FAIL)
- Multiple hash algorithms: Support for MD5 (default), SHA256, and SHA1
- Comprehensive help system: Professional help documentation with examples and usage guidance
- Standalone executable: Build portable executables with PyInstaller
- Cross-platform compatibility: Works on Linux, macOS, and Windows
check_code_integrity/
├── analise.py # Main integrity analysis tool
├── diff.py # File comparison utility
├── build.sh # Build script for creating executable
├── test.sh # Test script with example commands
├── test_diff.sh # Test script for difference checking
├── requirements.txt # Python dependencies (optional)
├── README.md # Project documentation
├── .ignore # File patterns to ignore during analysis
├── analise.spec # PyInstaller specification (auto-generated)
├── dist/ # Built executables directory
├── build/ # Build artifacts directory
├── ref_code/ # Reference code directory
├── valid_codes/ # Valid code samples for testing
└── invalid_codes/ # Invalid code samples for testing
- Python 3.x (3.6 or higher recommended)
- No external dependencies required (uses Python standard library only)
Optional dependencies:
# For building standalone executables
pip install pyinstaller
# For development (optional)
pip install pytest flake8 blackNote: The core functionality uses only Python standard library modules: hashlib, os, re, getopt, sys, and difflib.
To create a standalone executable:
# Install PyInstaller if not already installed
pip install pyinstaller
# Make build script executable and run
chmod +x build.sh
./build.shThis creates a single ~14MB executable in dist/analise that can be distributed without Python dependencies.
# Clone and test the tool
git clone https://github.com/dolalima/check_code_integrity.git
cd check_code_integrity
# Run built-in tests
chmod +x test.sh
./test.sh
# Or build and test executable
./build.sh
./dist/analise --helpThe tool includes comprehensive built-in help:
# Display detailed help with examples
python3 analise.py --help
# or
./dist/analise --helpThe help system includes:
- Complete option descriptions
- Real-world usage examples
- Hash algorithm recommendations
- Service integration guide
- Exit code documentation
Generate a hash from a reference directory:
python3 analise.py -a ./ref_codeCheck directories against a specific hash:
python3 analise.py -r <HASH_VALUE> -f ./target_folderAnalyze folders using a reference directory:
python3 analise.py -a ./ref_code -f ./target_folders| Option | Long Form | Description |
|---|---|---|
-a |
Calculate hash from reference file/directory | |
-r |
--reference |
Input hash code for verification |
-f |
--folder |
Analyze group of folders with same code |
-i |
--ignore |
Specify ignore pattern file |
-s |
--service |
Service name for start/stop operations |
-o |
--output |
Enable special output mode |
-h |
--help |
Display help information |
--hash= |
Choose hash algorithm (md5, sha256, sha1) | |
--shutdown |
Enable service shutdown on integrity failure |
# Generate reference hash and verify valid codes
python3 analise.py -a ./ref_code -f ./valid_codes
# Check invalid codes against reference
python3 analise.py -a ./ref_code -f ./invalid_codespython3 analise.py -a ./ref_code -f ./valid_codes --hash=sha256# Stop nginx service if integrity check fails
python3 analise.py -r ca44ff5eccbd5b05b6ade6314784595b -f ./target_codes -s nginx --shutdown# Compare specific directories and show differences
python3 diff.py ./ref_code ./target_code# All commands work with the executable
./dist/analise -a ./ref_code -f ./valid_codes
./dist/analise --help
./dist/analise -r <hash> -f ./folders --hash=sha256Create a .ignore file to specify patterns for files that should be excluded from analysis:
application/config/database.php
uploads
.jpg
.png
.env
The tool supports wildcard patterns where * matches any characters.
The tool provides colored output indicating the status of each check:
- GREEN "PASSED": Directory matches reference hash
- RED "FAIL": Directory differs from reference hash
Example output:
./valid_codes/c1 ca44ff5eccbd5b05b6ade6314784595b PASSED
./invalid_codes/c1 df9b8e4c12a3b5d6e7f8a9b0c1d2e3f4 FAIL
The tool can integrate with system services:
- Automatically stop services when integrity checks fail
- Start services when all checks pass
- Useful for maintaining system security and preventing execution of compromised code
Run the included test scripts:
# Test integrity checking
chmod +x test.sh
./test.sh
# Test file differences
chmod +x test_diff.sh
./test_diff.sh
# Test standalone executable
./build.sh
./dist/analise -a ./ref_code -f ./valid_codesThe tool has been thoroughly tested with:
- ✅ Hash calculation and verification
- ✅ Multiple hash algorithms (MD5, SHA256, SHA1)
- ✅ File pattern ignoring
- ✅ Service integration simulation
- ✅ Standalone executable functionality
- ✅ Comprehensive help system
The tool handles various error conditions:
- Missing directories or files
- Invalid hash algorithms
- File permission issues
- Service control errors
- PyInstaller build issues (automatically resolved)
- ✅ Comprehensive help system with examples
- ✅ Fixed PyInstaller compatibility issues
- ✅ Professional documentation
- ✅ Enhanced error handling
- ✅ Cross-platform executable builds
Build Issues:
# If PyInstaller is not found
pip install pyinstaller
# If executable fails with exit() error (fixed in v1.0)
# Ensure you're using the latest versionPermission Issues:
# Make scripts executable
chmod +x build.sh test.sh test_diff.sh- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is open source. Please check the repository for license details.
For issues, questions, or contributions, please refer to the project repository or contact the maintainers.