From 676bd25755f4f617b2d00da8e9994a35a8699295 Mon Sep 17 00:00:00 2001 From: santos22 Date: Wed, 26 Feb 2020 20:38:45 -0800 Subject: [PATCH] Add HMAC signature example in Python --- 2.0/python/webhooks/verifyHMacSignature.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 2.0/python/webhooks/verifyHMacSignature.py diff --git a/2.0/python/webhooks/verifyHMacSignature.py b/2.0/python/webhooks/verifyHMacSignature.py new file mode 100644 index 0000000..1f6d453 --- /dev/null +++ b/2.0/python/webhooks/verifyHMacSignature.py @@ -0,0 +1,22 @@ +import base64 +import hashlib +import hmac + + +def getHmacSignature(data, secret): + + digest = hmac.new( + key=str.encode(secret), + msg=str.encode(data), + digestmod=hashlib.sha1).digest() + + hmacSignature = base64.b64encode(digest) + + return hmacSignature + + +# then check signature in listener code +# data = "{\"name\":\"test webhook\", \"callback\":\"sms:callfire\"}" +# secret = 'mysecrets' +# result = hmac.compare_digest(b'v14pF7d5C1+CHNIEWlg+sw9v0Xg=', getHmacSignature(data, secret)) +# assert result == True