PRACTICAL NO:8
HMAC-SHA1
Aim
To write a Python program to calculate the HMAC-SHA1 (Hash-based Message Authentication Code using SHA-1) signature for a given message and secret key.
Introduction
HMAC stands for Hash-based Message Authentication Code. It is a mechanism used to verify the integrity and authenticity of a message. HMAC uses a cryptographic hash function together with a secret key. Both the sender and receiver must know the same secret key.
HMAC-SHA1 uses the SHA-1 hash algorithm as its underlying hash function. The result of HMAC-SHA1 is a 160-bit (20-byte) message authentication code, normally displayed as 40 hexadecimal characters.
Need for HMAC
A normal hash function can detect accidental changes to data, but by itself it does not prove who generated the hash. An attacker who changes a message could also calculate a new ordinary hash. HMAC solves this problem by combining the message with a secret key that is known only to the communicating parties.
HMAC-SHA1
In HMAC-SHA1, SHA-1 is used as the hash function. The secret key is processed according to the HMAC construction and combined with the message using two fixed padding values called ipad and opad.

8.1 CODE:
import hmac
import hashlib

msg = input("Enter the message:")
key = input("Enter the secret key:")

msg = msg.encode()
key = key.encode()

hmac_result = hmac.new(key, msg, hashlib.sha1)
print("HMAC-SHA1 Signature:", hmac_result.hexdigest())

8.1 OUTPUT:

8.2 CODE:
import hmac
import hashlib

file_path = input("Enter the path of the text file: ")
secret_key = input("Enter the secret key: ")

with open(file_path, 'r') as file:
    message = file.read()

key_bytes = secret_key.encode()
message_bytes = message.encode()

hmac_result = hmac.new(key_bytes, message_bytes, hashlib.sha1)
print("HMAC-SHA1:", hmac_result.hexdigest())

8.2 OUTPUT:
