fix(paytm-integration): use checksum library to generate/verify checksum

This commit is contained in:
Mangesh-Khairnar 2020-07-20 23:08:50 +05:30
parent 311babc6aa
commit 0ae7d40ebe
3 changed files with 2 additions and 82 deletions

View file

@ -1,80 +0,0 @@
import base64
import string
import random
import hashlib
import sys
from Crypto.Cipher import AES
iv = '@@@@&&&&####$$$$'
BLOCK_SIZE = 16
if (sys.version_info > (3, 0)):
__pad__ = lambda s: bytes(s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE), 'utf-8')
else:
__pad__ = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)
__unpad__ = lambda s: s[0:-ord(s[-1])]
def encrypt(input, key):
input = __pad__(input)
c = AES.new(key.encode("utf8"), AES.MODE_CBC, iv.encode("utf8"))
input = c.encrypt(input)
input = base64.b64encode(input)
return input.decode("UTF-8")
def decrypt(encrypted, key):
encrypted = base64.b64decode(encrypted)
c = AES.new(key.encode("utf8"), AES.MODE_CBC, iv.encode("utf8"))
param = c.decrypt(encrypted)
if type(param) == bytes:
param = param.decode()
return __unpad__(param)
def generateSignature(params, key):
if not type(params) is dict and not type(params) is str:
raise Exception("string or dict expected, " + str(type(params)) + " given")
if type(params) is dict:
params = getStringByParams(params)
return generateSignatureByString(params, key)
def verifySignature(params, key, checksum):
if not type(params) is dict and not type(params) is str:
raise Exception("string or dict expected, " + str(type(params)) + " given")
if "CHECKSUMHASH" in params:
del params["CHECKSUMHASH"]
if type(params) is dict:
params = getStringByParams(params)
return verifySignatureByString(params, key, checksum)
def generateSignatureByString(params, key):
salt = generateRandomString(4)
return calculateChecksum(params, key, salt)
def verifySignatureByString(params, key, checksum):
paytm_hash = decrypt(checksum, key)
salt = paytm_hash[-4:]
return paytm_hash == calculateHash(params, salt)
def generateRandomString(length):
chars = string.ascii_uppercase + string.digits + string.ascii_lowercase
return ''.join(random.choice(chars) for _ in range(length))
def getStringByParams(params):
params_string = []
for key in sorted(params.keys()):
value = params[key] if params[key] is not None and params[key].lower() != "null" else ""
params_string.append(str(value))
return '|'.join(params_string)
def calculateHash(params, salt):
finalString = '%s|%s' % (params, salt)
hasher = hashlib.sha256(finalString.encode())
hashString = hasher.hexdigest() + salt
return hashString
def calculateChecksum(params, key, salt):
hashString = calculateHash(params, salt)
return encrypt(hashString, key)

View file

@ -13,7 +13,7 @@ from frappe import _
from frappe.utils import get_url, call_hook_method, cint, flt, cstr
from frappe.integrations.utils import create_request_log, create_payment_gateway
from frappe.utils import get_request_site_address
from frappe.integrations.doctype.paytm_settings.checksum import generateSignature, verifySignature
from paytmchecksum import generateSignature, verifySignature
from frappe.utils.password import get_decrypted_password
class PaytmSettings(Document):

View file

@ -66,5 +66,5 @@ watchdog==0.8.0
Werkzeug==0.16.1
xlrd==1.2.0
zxcvbn-python==4.4.24
pycryptodome==3.9.7
Whoosh==2.7.4
paytmchecksum==1.7.0