Skip to content

Commit abe7698

Browse files
committed
add methods for signature verifcation
1 parent eca9f0f commit abe7698

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

convoy/utils/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
from convoy.utils.helpers import responseHelper
1+
from convoy.utils.helpers import responseHelper
2+
from convoy.utils.helpers import verifySignature
3+
from convoy.utils.helpers import hashString

convoy/utils/helpers.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,50 @@
1+
from logging import exception
12
import requests
3+
import hmac
4+
import hashlib
5+
import base64
26

37
def responseHelper(e):
48
if e == requests.exceptions.HTTPError:
5-
print ("Http Error:", e)
9+
raise Exception("Http Error: %s" % e)
610
if e == requests.exceptions.ConnectionError:
7-
print ("Error Connecting:", e)
11+
raise Exception("Error Connecting: %s" % e)
812
if e == requests.exceptions.Timeout:
9-
print ("Timeout Error:", e)
13+
raise Exception("Timeout Error: %s" % e)
1014
if e == requests.exceptions.RequestException:
11-
print(e)
15+
raise Exception(e)
16+
else:
17+
raise Exception(e)
18+
19+
def verifySignature(algorithm, hash, payload, secret):
20+
dec = hashString(algorithm, payload, secret)
21+
return dec == hash
22+
23+
24+
def hashString(algorithm, msg, secret):
25+
alg = getHashFunction(str.lower(algorithm))
26+
dig = hmac.new(bytes(secret, 'utf-8'), msg=bytes(msg, 'utf-8'), digestmod=alg).digest()
27+
dec = base64.b64encode(dig).decode()
28+
return dec
29+
30+
def getHashFunction(hash):
31+
if str.lower(hash) == 'sha256':
32+
return hashlib.sha256
33+
if str.lower(hash) == 'md5':
34+
return hashlib.md5
35+
if str.lower(hash) == 'sha384':
36+
return hashlib.sha384
37+
if str.lower(hash) == 'sha224':
38+
return hashlib.sha224
39+
if str.lower(hash) == 'sha512':
40+
return hashlib.sha512
41+
if str.lower(hash) == 'sha1':
42+
return hashlib.sha1
43+
if str.lower(hash) == 'sha3_256':
44+
return hashlib.sha3_256
45+
if str.lower(hash) == 'sha3_224':
46+
return hashlib.sha3_224
47+
if str.lower(hash) == 'sha3_512':
48+
return hashlib.sha3_512
49+
else:
50+
raise Exception("algorithm not available.")

0 commit comments

Comments
 (0)