diff --git a/README.md b/README.md index 98d6bb6..42c5d94 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Here's an example: # Hash: INSERT_HASH_HERE import selfhash -hasher = selfhash.SelfHash() +hasher = selfhash.SelfHash(bypass_salt=False) # Always prompt for salt when set to False hasher.hash(__file__) ``` @@ -44,6 +44,26 @@ If you choose not to provide a salt/passphrase, you can simply press Enter when Remember - if you use a salt/passphrase you should use the same salt/passphrase every time you run the script, otherwise the hash will be different and the script will fail the verification. This salt/passphrase should be stored securely for future retrieval. +## Bypass Salt/Passphrase + +In some cases, you might want to bypass the salt/passphrase prompt. You can do this by setting the bypass_salt parameter to True when you create an instance of the SelfHash class. + +Here's an example: + +```python + +# Hash: INSERT_HASH_HERE +import selfhash + +hasher = selfhash.SelfHash(bypass_salt=True) +hasher.hash(__file__) +``` + +When you set `bypass_salt` to `True`, the hash method will not prompt you for a salt/passphrase and will calculate the hash based only on your script's code. This can be useful in automated environments where user input is not possible. + +Note: If you bypass the salt/passphrase, you won't be able to use a salt/passphrase for the hash calculation. If you've previously run the hash method with a salt/passphrase, the hash will be different when you bypass the salt/passphrase, and the script will fail the verification. Be sure to use the same bypass_salt setting every time you run the script. + + ## Verifying The SelfHash Module To ensure the integrity of the SelfHash module itself, we provide a script named `verify_self.py`. This script uses SelfHash to verify the hash of the `selfhash/selfhash.py` file. diff --git a/selfhash/selfhash.py b/selfhash/selfhash.py index 2d4d90a..713c6df 100644 --- a/selfhash/selfhash.py +++ b/selfhash/selfhash.py @@ -1,5 +1,5 @@ #!/usr/bin/python -# Hash: 141560b7ed59f8871c2f2c0e09610031e37a753c32cf5311a8a9700b9813f94b +# Hash: 09063288aa0912b98bba70932e28b7b9f592478d9225317d8d67c281689926c9 # No password is set for this hash as it is used to verify the selfhash module code itself and can be checked against the github repo @@ -11,11 +11,12 @@ class SelfHash: """Class for SelfHash""" - def __init__(self): + def __init__(self, bypass_salt=False): """Init function""" self.file_data_hash = None self.source_code_hash = None self.known_hash = None + self.bypass_salt = bypass_salt def hash(self, file): """Function that hashes the source code""" @@ -31,8 +32,9 @@ def hash(self, file): self.file_data_hash = ''.join([line for i, line in enumerate(file_data) if i != hash_line_index]) - salt = getpass.getpass(prompt='This python script is protected by SelfHash.\nPlease provide a salt for the hash calculation.\nIf you do not want to provide one, just press Enter: ') - self.file_data_hash += salt + if not self.bypass_salt: + salt = getpass.getpass(prompt='This python script is protected by SelfHash.\nPlease provide a salt for the hash calculation.\nIf you do not want to provide one, just press Enter: ') + self.file_data_hash += salt self.source_code_hash = hashlib.sha256(self.file_data_hash.encode()).hexdigest() diff --git a/test.py b/test.py index c665418..54d15f8 100644 --- a/test.py +++ b/test.py @@ -1,12 +1,12 @@ #!/usr/bin/python -# Hash: INSERT_HASH_HERE +# Hash: c7f962b5cde0cc68174ca405e52742f25d5a08ec78e900b642824b02a6b298b8 """Test file to check hash generation and verification""" import selfhash # Load selfhash into a hasher and perform the verification check -hasher = selfhash.SelfHash() +hasher = selfhash.SelfHash(bypass_salt=False) hasher.hash(__file__) # This should only run if the hash matches and the program is 'verified' diff --git a/verify_all.py b/verify_all.py index bdd5713..8e78557 100755 --- a/verify_all.py +++ b/verify_all.py @@ -1,5 +1,5 @@ #!/usr/bin/python -# Hash: ecb0e433ccaddc83f49f247e1e38ef19b10c0d73e5def96a2be573913ce57d80 +# Hash: ff09080b23d744ebcc446a3fdce8dc0da5ae1b127517a4d29c3be46219a012c4 """Script to verify all python files in a directory recursively""" @@ -13,7 +13,7 @@ def hash_and_verify_files(directory): print(f"Processing {filename}...") # Instantiate a new selfhash class for each file - hasher = selfhash.SelfHash() + hasher = selfhash.SelfHash(bypass_salt=True) hasher.hash(filename) if hasher.known_hash == "INSERT_HASH_HERE": diff --git a/verify_self.py b/verify_self.py index 7d59289..1c789b7 100755 --- a/verify_self.py +++ b/verify_self.py @@ -1,12 +1,12 @@ #!/usr/bin/python -# Hash: fa7a281d063b383fbf7e91d8fe8eb91d820b08779d0658035735271bbf11da79 +# Hash: 48717260d6a313c4cc1b3d361a852a34bd24e8ba2786c2a08ecfdaf7f1e556c2 """Script to verify the selfhash/selfhash.py module hash""" import selfhash # Load selfhash into a hasher and perform the verification check -hasher = selfhash.SelfHash() +hasher = selfhash.SelfHash(bypass_salt=True) hasher.hash("selfhash/selfhash.py") # replace with the actual path to the selfhash.py file print(hasher.hash)