Skip to content

Commit

Permalink
linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldstoner committed Feb 24, 2025
1 parent 9b107af commit c254637
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
8 changes: 4 additions & 4 deletions selfhash/selfhash.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/python
# Hash: f67323982e945bb945f9fc54ed0b7a8c0d1831b1475e6e89d3e270f9dbffd1dc
# Hash: 7133c3ec57f1dbdd2b35a409abebd34ea0736fde377056706b32bf955ae7d313
# Author: Ron Stoner
# Github: ronaldstoner
# Website: stoner.com
Expand All @@ -15,8 +15,8 @@


class SelfHash:

"""Class to handle the self-hashing and verification of Python source code."""

def __init__(self, bypass_salt=False):
"""
Initializes the SelfHash object.
Expand Down Expand Up @@ -67,7 +67,7 @@ def hash(self, file):

# Calculate the SHA-256 hash of the source code (with salt if provided)
self.source_code_hash = hashlib.sha256(self.file_data_hash.encode()).hexdigest()

# Compare the known hash to the calculated hash
self.known_hash = file_data[hash_line_index].strip().split(' ')[-1]
self.known_hash = self.known_hash.strip() # Clean up extra spaces
Expand All @@ -85,7 +85,7 @@ def hash(self, file):
else:
print("\033[91mFAIL\033[0m: The source code may have been tampered with or the salt/passphrase is incorrect.")
fail = True

# Exit with error if there was any failure
if fail:
sys.exit(1)
10 changes: 5 additions & 5 deletions verify_all.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/python
# Hash: 8a26874511080270877d2b3e5f94191fd9e5fd2dad5a191065906c8e66faa781
# Hash: 4d8077bf0fa508b5211a94521fdf436dc1316d3da6ea8d2570470b25ed2330db
# Author: Ron Stoner
# Github: ronaldstoner
# Website: stoner.com
Expand All @@ -11,21 +11,21 @@

def hash_and_verify_files(directory):
"""Hash and verify all .py files in the given directory recursively."""

# Find all Python files in the directory and its subdirectories
py_files = glob.iglob(directory + '**/*.py', recursive=True)
total_files = sum(1 for _ in py_files) # Count the total number of .py files
print(f"\nTotal Python files found: {total_files}")

# Re-run glob to get the file list again after counting
py_files = glob.iglob(directory + '**/*.py', recursive=True)

for filename in py_files:
print(f"\nProcessing {filename}...")

# Instantiate the SelfHash class for each file
hasher = selfhash.SelfHash(bypass_salt=True)

# Perform the hash verification for each file
hasher.hash(filename)

Expand Down
14 changes: 7 additions & 7 deletions verify_self.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
#!/usr/bin/python3
# Hash: b3bdd013f179bb06e9be80e8282c3d3dcaf34463f33257b88a96769daf51689c
# Hash: 7171f49aa3395c9177ee86c621c5635ad5f62cc52a8564e5adb6b7f2e50b786b
# Author: Ron Stoner
# Github: ronaldstoner
# Website: stoner.com

"""Script to verify the selfhash/selfhash.py module hash"""

import sys
import selfhash

def verify_selfhash():
"""Verifies the hash of the selfhash.py module."""

# Instantiate the SelfHash class with bypass_salt=True to skip salt prompt
hasher = selfhash.SelfHash(bypass_salt=True)

# Perform the hash verification check on the selfhash.py module
print("\nVerifying the hash of the 'selfhash/selfhash.py' module...")
hasher.hash("selfhash/selfhash.py") # Replace with the actual path to selfhash.py if needed

# Output the calculated hash and comparison results
print("Expected Hash: ", hasher.known_hash)
print("Actual Hash: ", hasher.source_code_hash)
if hasher.known_hash != hasher.source_code_hash:
print("\033[91mFAIL\033[0m: The selfhash.py module has been tampered with or the hash does not match.")
print("Expected Hash: ", hasher.known_hash)
print("Actual Hash: ", hasher.source_code_hash)
exit(1)
sys.exit(1)
elif hasher.known_hash in ("INSERT_HASH_HERE", "Hash:"):
print("\033[93mWARNING\033[0m: The hash is not set yet. Please replace 'INSERT_HASH_HERE' with the calculated hash.")
print("Generated Hash: ", hasher.source_code_hash)
exit(1)
sys.exit(1)
print("\nVerification complete.")

# Run the verification function
if __name__ == "__main__":
verify_selfhash()

0 comments on commit c254637

Please sign in to comment.