1+ from logging import exception
12import requests
3+ import hmac
4+ import hashlib
5+ import base64
26
37def 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