HMAC
From charlesreid1
import hmac
import hashlib
def hmac_sha256(key, message):
"""Return the HMAC-SHA256 hex digest."""
return hmac.new(key, message, hashlib.sha256).hexdigest()
def hmac_sha256_verify(key, message, signature):
"""Constant-time comparison of HMAC-SHA256 digests."""
expected = hmac.new(key, message, hashlib.sha256).hexdigest()
return hmac.compare_digest(expected, signature)
if __name__ == '__main__':
key = b'shared_secret'
message = b'This is a test'
sig = hmac_sha256(key, message)
print("HMAC-SHA256 was '%s'" % sig)
print("Verified: %s" % hmac_sha256_verify(key, message, sig))
See also
- RSA – asymmetric encryption, decryption, and signing
| Crypto cryptography-related resources on the wiki
Implementing AES Cipher in Python: AES
Category:Crypto · Category:Security · Category:Encryption
|
Red Links
- RSA
- GPG
- SHA
- Diffie-Hellman Key Exchange
- Password Hashing
- TLS
- One Time Pad
- HMAC
- Base64
- Elliptic Curve Cryptography
| Python a powerful programming language
Scientific Python: Data analysis libraries: Scipy · Numpy · Pandas · Statsmodel Machine learning libraries: Sklearn Neural network libraries: Tensorflow · Keras Plotting/viz: Matplotlib · Seaborn · Jupyter Solving partial differential equations and bessel functions: Fipy · Bessel Functions
Web and Networking Python: Web programming: Flask · Webapps · Mechanize · Scrapy · Gunicorn Wifi: Wireless/Python · Scapy IPython and Jupyter: Jupyter
Drawing, Geometry, and Shapes: Shapely (for drawing shapes): Shapely Geography library: Geos
General Useful Python Utilities: Python Remote Objects: Pyro Logging (create multi-channel log messages): Logging Keyboard (control keyboard from Python): Keyboard
Black Hat Python: Network scanning: Python/Scanner
|