Created
August 25, 2022 01:07
-
-
Save devvspaces/469ff5d5efa641f44ffc04fd365a8679 to your computer and use it in GitHub Desktop.
Creating keyed sha1 hashes and comparing them in time constant
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
import hashlib | |
import hmac | |
def compare_digest(a, b): | |
"""Compare hashes in constant time.""" | |
return hmac.compare_digest(a, b) | |
def sha1_hash(message: str, key: str): | |
"""Creates a keyed hash for a message using the SHA1 algorithm.""" | |
return hmac.new( | |
key.encode(), message.encode(), hashlib.sha1 | |
).hexdigest() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For other hashing algorithms just change
hashlib.sha1
tohashlib.ALGORITHM
, other algorithms are sha256, sha512 and more. Check out python hashlib docs for more algorithms you can use.