Created
September 4, 2025 13:04
-
-
Save cgoldberg/7dd0f0c7affb2f99bb1af4a4d0eaea65 to your computer and use it in GitHub Desktop.
Python - Generate a SHA3-512 hash with optional salt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Generate a SHA3-512 hash with optional salt | |
| import re | |
| # requires pycryptodome | |
| # - https://pycryptodome.readthedocs.io/en/latest/src/hash/sha3_512.html | |
| from Crypto.Hash import SHA3_512 | |
| def generate_hash(s, salt=None): | |
| """Generate a SHA3-512 hash from a string with optional salt. | |
| Returns a string containing a 128 character hex digest. | |
| """ | |
| if salt: | |
| s += salt | |
| sha3_hash = SHA3_512.new() | |
| sha3_hash.update(s.encode()) | |
| return sha3_hash.hexdigest() | |
| # example usage: | |
| if __name__ == "__main__": | |
| s = "some data" | |
| salt = "some salt" | |
| digest = generate_hash(s, salt) | |
| print(digest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment