import hmac import hashlib import secrets # Generate and write shared key shared_secret: bytes = secrets.token_bytes(16) with open('k', 'wb') as file: file.write(shared_secret) # Take in message and compute hmac m = input("Message: ").encode() hmac_object: hmac.HMAC = hmac.new(shared_secret, m, hashlib.sha256) m_hash = hmac_object.digest() print(f'HMAC: {m_hash}') # Write hmac digest and message with open('mactext', 'wb') as file: file.write(m + m_hash)