Skip to content

Commit

Permalink
Added bypass_salt boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
Ron Stoner committed Oct 7, 2023
1 parent 0e2ce97 commit 743cc4a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
```

Expand All @@ -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.
Expand Down
10 changes: 6 additions & 4 deletions selfhash/selfhash.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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"""
Expand All @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
4 changes: 2 additions & 2 deletions verify_all.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/python
# Hash: ecb0e433ccaddc83f49f247e1e38ef19b10c0d73e5def96a2be573913ce57d80
# Hash: ff09080b23d744ebcc446a3fdce8dc0da5ae1b127517a4d29c3be46219a012c4

"""Script to verify all python files in a directory recursively"""

Expand All @@ -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":
Expand Down
4 changes: 2 additions & 2 deletions verify_self.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit 743cc4a

Please sign in to comment.