HMAC is a mechanism for message authentication using cryptographic hash functions. HMAC can be used with any iterative cryptographic hash function, e.g., MD5, SHA-1, in combination with a secret shared key.
This module implements the HMAC algorithm. The basic idea is to generate a cryptographic hash of the actual data combined with a shared secret key. The resulting hash can then be used to check the transmitted or stored message to determine a level of trust, without transmitting the secret key.
hmac.new(key, msg=None, digestmod=None)
Return a new hmac object. key is a bytes or bytearray object giving the secret key. If msg is present, the method call update(msg) is made. digestmod is the digest name, digest constructor or module for the HMAC object to use. It supports any name suitable to hashlib.new().
A HMAC object has following methods:
- HMAC.update(msg): This method updates the hmac object with msg. Repeated calls are equivalent to a single call with the concatenation of all the arguments: m.update(a); m.update(b) is equivalent to m.update(a + b).
- HMAC.digest(): This method returns the digest of the bytes passed to the update() method so far. This bytes object will be the same length as the digest_size of the digest given to the constructor.
- HMAC.hexdigest(): This method is like the digest() method except the digest is returned as a string twice the length containing only hexadecimal digits.
- HMAC.copy(): This method returns a copy or a clone of the hmac object. This can be used to efficiently compute the digests of strings that share a common initial substring.
A hash object has following attributes:
- HMAC.digest_size: The size of the resulting HMAC digest in bytes.
- HMAC.block_size: The internal block size of the hash algorithm in bytes.
- HMAC.name: The canonical name of this HMAC. e.g. hmac-sha1.
Hexdigest: df2ae7cdb5c849001e33ee29eb1c51ba2cafbaa7 Hexdigest after update: 3923273eb3aa9328478eb5aabf2d96e185256b4b Digest size: 20 bytes Block size: 64 bytes Canonical name: hmac-sha1 Digest: b"9#'>\xb3\xaa\x93(G\x8e\xb5\xaa\xbf-\x96\xe1\x85%kK" Hexdigest of clone: 3923273eb3aa9328478eb5aabf2d96e185256b4b
This article is contributed by Mayank Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Message Authentication Codes
- Python Django | Google authentication and Fetching mails from scratch
- Challenge Response Authentication Mechanism (CRAM)
- Implement Token Authentication using Django REST Framework
- 3D passwords-Advanced Authentication Systems
- Authentication using Python requests
- JWT Authentication with Django REST Framework
- Using JWT for user authentication in Flask
- Securing Django Admin login with OTP (2 Factor Authentication)
- Python | Message Encode-Decode using Tkinter
- Python | Issue Warning Message
- Python | Prompt for Password at Runtime and Termination with Error Message
- Message Digest in Information security
- Python Program that Sends And Recieves Message from Client
- Create a Yes/No Message Box in Python using tkinter
- Python Tkinter - Message
- PyQt5 – Set status bar message in window
- Python PyQt5 – How to change font and size of Status Bar Message
- Message Boxes using PyAutoGUI
- Send message to Telegram user using Python