Bird
0
0

Identify the error in this Python webhook verification snippet:

medium📝 Debug Q14 of 15
Rest API - Webhooks and Events
Identify the error in this Python webhook verification snippet:
import hmac
import hashlib

secret = "mysecret"
payload = "data"
signature = "abc123"

computed = hmac.new(secret, payload.encode(), hashlib.sha256).hexdigest()
if computed == signature:
    print("Verified")
else:
    print("Not verified")
Apayload should not be encoded
Bhashlib.md5 should be used instead of sha256
Csecret should be encoded before use in hmac.new
Dsignature comparison should use != instead of ==
Step-by-Step Solution
Solution:
  1. Step 1: Check data types for hmac.new

    hmac.new requires the secret key as bytes, but secret is a string and not encoded.
  2. Step 2: Identify correct fix

    Encoding secret with secret.encode() fixes the error; payload encoding is correct.
  3. Final Answer:

    secret should be encoded before use in hmac.new -> Option C
  4. Quick Check:

    Secret must be bytes, so encode it [OK]
Quick Trick: Encode secret string before hmac.new call [OK]
Common Mistakes:
MISTAKES
  • Not encoding secret key
  • Changing payload encoding incorrectly
  • Using wrong hash function
  • Wrong comparison operator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes