0
0
Rest APIprogramming~10 mins

Webhook signature verification in Rest API - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the module needed for HMAC.

Rest API
import [1]
Drag options to blanks, or click blank then click option'
Ahmac
Brequests
Cjson
Dhashlib
Attempts:
3 left
💡 Hint
Common Mistakes
Importing hashlib instead of hmac
Forgetting to import any module
2fill in blank
medium

Complete the code to create an HMAC object using the secret key and message.

Rest API
signature = hmac.new([1], message, hashlib.sha256).hexdigest()
Drag options to blanks, or click blank then click option'
Asecret_key
Bmessage
Chashlib
Dsignature
Attempts:
3 left
💡 Hint
Common Mistakes
Using the message as the key
Using hashlib instead of the key
3fill in blank
hard

Fix the error in the code to correctly compare the computed signature with the received signature.

Rest API
if hmac.compare_digest(signature, [1]):
    print('Valid signature')
Drag options to blanks, or click blank then click option'
Amessage
Bsignature
Csecret_key
Dreceived_signature
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing the signature to itself
Comparing signature to message or secret key
4fill in blank
hard

Fill both blanks to decode the received signature from hex and compare it safely.

Rest API
received_sig_bytes = bytes.fromhex([1])
if hmac.compare_digest(signature_bytes, [2]):
    print('Signature verified')
Drag options to blanks, or click blank then click option'
Areceived_signature
Bsignature_bytes
Creceived_sig_bytes
Dsecret_key
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing hex string directly without decoding
Mixing up variable names
5fill in blank
hard

Fill all three blanks to create a function that verifies a webhook signature.

Rest API
def verify_signature(secret, message, [1]):
    computed_sig = hmac.new(secret, message, [2]).hexdigest()
    return hmac.compare_digest(computed_sig, [3])
Drag options to blanks, or click blank then click option'
Areceived_signature
Bhashlib.sha256
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names
Not using the correct hashing algorithm
Comparing wrong variables