📖 Scenario: You are building a simple server that receives webhook requests from a payment service. To make sure the requests are really from the payment service and not from someone else, you need to check a special code called a signature.This signature is created by the payment service using a secret key and the data they send. Your job is to verify this signature on your server before trusting the data.
🎯 Goal: Build a small program that takes a webhook payload and its signature, then checks if the signature is valid using a secret key.
📋 What You'll Learn
Create a variable with the webhook payload string exactly as given
Create a variable with the signature string exactly as given
Create a variable with the secret key string exactly as given
Use HMAC with SHA256 to create a hash of the payload using the secret key
Compare the generated hash with the given signature to verify authenticity
Print 'Valid signature' if they match, otherwise print 'Invalid signature'
💡 Why This Matters
🌍 Real World
Webhook signature verification is used to ensure that data sent from external services like payment gateways or messaging platforms is authentic and has not been tampered with.
💼 Career
Many jobs in backend development, security, and API integration require understanding how to verify webhook signatures to build secure and reliable systems.
Progress0 / 4 steps
1
Set up the webhook payload
Create a variable called payload and set it to the string '{"order_id": "12345", "amount": "100.00"}' exactly.
Rest API
Hint
Remember to include the quotes exactly as shown, including the curly braces and double quotes inside the string.
2
Set up the signature and secret key
Create a variable called signature and set it to the string '5d41402abc4b2a76b9719d911017c592'. Also create a variable called secret_key and set it to the string 'mysecretkey'.
Rest API
Hint
Make sure the variable names and string values match exactly.
3
Generate the HMAC SHA256 hash
Import the hmac and hashlib modules. Then create a variable called computed_hash that uses hmac.new() with secret_key encoded as bytes, payload encoded as bytes, and hashlib.sha256 as the digest mode. Use .hexdigest() to get the hash string.
Rest API
Hint
Use secret_key.encode() and payload.encode() to convert strings to bytes before hashing.
4
Verify the signature and print the result
Use an if statement to compare computed_hash with signature. If they are equal, print 'Valid signature'. Otherwise, print 'Invalid signature'.
Rest API
Hint
Use if computed_hash == signature: to check equality and print the correct message.
Practice
(1/5)
1. What is the main purpose of webhook signature verification?
easy
A. To confirm the webhook is from a trusted source
B. To speed up the webhook delivery
C. To encrypt the webhook payload
D. To log the webhook request time
Solution
Step 1: Understand webhook security
Webhook signature verification is used to check if the webhook request is really from the expected sender.
Step 2: Identify the purpose of verification
This process confirms the webhook is trusted by comparing the signature with a secret key.
Final Answer:
To confirm the webhook is from a trusted source -> Option A
Quick Check:
Verification = Trusted source confirmation [OK]
Hint: Verification means checking trust, not speed or encryption [OK]
Common Mistakes:
Confusing verification with encryption
Thinking it speeds up webhook delivery
Assuming it logs request time
2. Which of the following is the correct Python code snippet to compute an HMAC SHA256 signature for webhook verification?
easy
A. hmac.new(secret.encode(), payload.encode(), hashlib.sha256).hexdigest()
B. hmac.new(payload.encode(), secret.encode(), hashlib.md5).hexdigest()
C. hashlib.sha256(secret + payload).hexdigest()
D. hmac.sha256(secret, payload).hexdigest()
Solution
Step 1: Understand HMAC function parameters
The correct order is secret key first, then message (payload), and the hashing algorithm.
Step 2: Identify correct syntax and algorithm
hmac.new(secret.encode(), payload.encode(), hashlib.sha256).hexdigest() uses hmac.new with secret encoded, payload encoded, and hashlib.sha256, which is correct.
Final Answer:
hmac.new(secret.encode(), payload.encode(), hashlib.sha256).hexdigest() -> Option A
Quick Check:
Secret first, payload second, SHA256 used [OK]
Hint: Secret key is first argument in hmac.new() [OK]
Common Mistakes:
Swapping secret and payload order
Using wrong hash function like md5
Incorrect function call syntax
3. Given the following Python code snippet, what will be the output if the signature matches the computed HMAC?
C. secret should be encoded before use in hmac.new
D. signature comparison should use != instead of ==
Solution
Step 1: Check data types for hmac.new
hmac.new requires the secret key as bytes, but secret is a string and not encoded.
Step 2: Identify correct fix
Encoding secret with secret.encode() fixes the error; payload encoding is correct.
Final Answer:
secret should be encoded before use in hmac.new -> Option C
Quick Check:
Secret must be bytes, so encode it [OK]
Hint: Encode secret string before hmac.new call [OK]
Common Mistakes:
Not encoding secret key
Changing payload encoding incorrectly
Using wrong hash function
Wrong comparison operator
5. You receive webhook requests with a signature header. To securely verify the webhook, which steps should you perform in order? Select the correct sequence.
hard
A. 1. Retrieve secret key
2. Compare signatures
3. Compute HMAC of payload
4. Accept webhook if signatures do not match
B. 1. Accept webhook
2. Compute HMAC of payload
3. Compare signatures
4. Log the webhook
C. 1. Compute HMAC of payload
2. Retrieve secret key
3. Compare signatures
4. Reject webhook if signatures match
D. 1. Retrieve the secret key
2. Compute HMAC of the payload using the secret
3. Compare computed signature with header signature
4. Accept webhook if signatures match
Solution
Step 1: Retrieve the secret key
You need the secret key first to compute the HMAC signature.
Step 2: Compute HMAC of the payload using the secret
Use the secret key and payload to compute the signature.
Step 3: Compare computed signature with header signature
Check if the computed signature matches the one sent in the webhook header.
Step 4: Accept webhook if signatures match
Only accept the webhook if the signatures are the same to ensure trust.
Final Answer:
1. Retrieve the secret key
2. Compute HMAC of the payload using the secret
3. Compare computed signature with header signature
4. Accept webhook if signatures match -> Option D
Quick Check:
Secret -> Compute -> Compare -> Accept [OK]
Hint: Always compute signature before comparing and accepting [OK]
Common Mistakes:
Accepting webhook before verification
Comparing before computing signature
Rejecting on matching signatures
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.