Change language

hmac — hashing by key for message authentication

HMAC — it is a message authentication mechanism using cryptographic hash functions. HMAC can be used with any iterative cryptographic hash function such as MD5, SHA-1, combined with a secret shared key. 
This module implements the HMAC algorithm. The main idea is to create a cryptographic hash of real data combined with a shared secret. The resulting hash can then be used to verify the transmitted or stored message to determine the level of trust, without transmitting the secret key.

  hmac.new (key, msg = None, digestmod = None)  

Return a new hmac object.  key — it is bytes or a bytearray object giving the secret key. If msg is present, the method call (msg) is updated.  digestmod — digest name, digest constructor, or module for an HMAC object. It supports any name that matches hashlib.new ().

The HMAC object has the following methods:

  1. HMAC.update (msg): this method updates the hmac object with msg. Repeated calls are equivalent to one call with all arguments concatenated: m.update (a); m.update (b) is equivalent to m.update (a + b).
  2. HMAC.digest (): This method returns a digest of bytes passed to the update () method. This byte object will be the same length as the digest_size of the digest passed to the constructor.
  3. HMAC.hexdigest (): This method is similar to the digest () method, except that the digest is returned as a string twice the length , containing only hexadecimal digits.
  4. HMAC.copy (): this method returns a copy or clone hmac object. This can be used to efficiently compute string digests that have a common leading substring.

The hash object has the following attributes:

  • HMAC.digest_size: the size of the resulting HMAC digest in bytes.
  • HMAC.block_size: the size of the internal block of the hashing algorithm in bytes.
  • HMAC.name : the canonical name of this HMAC. for example, hmac-sha1.

Example :

# Python code 3 to demonstrate how the hmac module works.

 

import hmac

import hashlib

 
# create a new hmac object using sha1 hashing algorithm

digest_maker = hmac.new (b ’secret-key’ , b ’ msg’ , hashlib. sha1)

 
# print the Hexdigest bytes transferred to update

print ( "Hexdigest: " + digest_maker.hexdigest ())

  
# call update to update the message

digest_maker.update (b ’another msg’ )

  
# print Hexdigest bytes submitted for update

print ( " Hexdigest after update: " + digest_maker.hexdigest ())

 

print ( "Digest size:" + str (digest_maker.digest_size) + "bytes" )

print ( "Block size:" + str (digest_maker.block_size) + "bytes" )

print ( "Canonical name:" + digest_maker.name)

 
# print a digest of bytes sent for update

print ( "Digest:" , end = "" )

print (digest_maker.digest ())

  
# create a copy of the hmac object

digest_clone = digest_maker.copy ()

print ( "Hexdigest of clone:" + digest_clone.hexdigest ())

Output:

 Hexdigest : df2ae7cdb5c849001e33ee29eb1c51ba2cafbaa7 Hexdigest after update: 3923273eb3aa9328478eb5aabf2d96e185256b4b Digest size: 20 bytes Block size: 64 bytes Canonical name: hmac-sha1 Digest : b " 9 #’" xb3xaax93 (Gx8exb5xaaxbf -x96xe1x85% kK "Hexdigest of clone: ​​3923273eb3aa9 328478eb5aabf2d96e185256b4b 

This article courtesy of Mayank Agrawal . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you’d like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method