INNER CODE UNIT · Python
validateHMAC
openfaas/workshop · hmac-protected/hmac-protected/handler.py:3
def validateHMAC(message, secret, hash):
# GitHub and the sign flag prefix the hash with "sha1="
receivedHash = getHash(hash)
# Hash message with secret
expectedMAC = hmac.new(secret.encode(), message.encode(), hashlib.sha1)
createdHash = expectedMAC.hexdigest()
return receivedHash == createdHash
def getHash(hash):
if "sha1=" in hash:
hash=hash[5:]
return hash
def handle(req):
# We receive the hashed message in form of a header