Search code examples
pythonpython-3.xflaskhashlibccavenue

Correctly migrate from Python 2 md5 library to Python 3 hashlib in Flask web application for CCAvenue integration


I'm trying to integrate a 3rd party payment gateway (CCAvenue) in Flask 0.12.2, Python 3.6.1.

The reference code provided by the 3rd party uses the deprecated library md5 to encrypt texts.

I have got the solution for migration in Existing Solution in Django. However, I need Flask version code for the same.


Solution

  • I have got the solution and here is the code

    from Crypto.Cipher import AES
    import hashlib
    from binascii import hexlify, unhexlify
    
    def pad(data):
        length = 16 - (len(data) % 16)
        data += chr(length)*length
        return data
    
    def unpad(data):
        return data[0:-ord(data[-1])] 
    
    def encrypt(plainText, workingKey):
        iv = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'
        plainText = pad(plainText)
        bytearrayWorkingKey = bytearray()
        bytearrayWorkingKey.extend(map(ord, workingKey))
        enc_cipher = AES.new(hashlib.md5(bytearrayWorkingKey).digest(), AES.MODE_CBC, iv)
        return hexlify(enc_cipher.encrypt(plainText)).decode('utf-8')
    
    def decrypt(cipherText, workingKey):
        iv = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'
        encryptedText = unhexlify(cipherText)
        bytearrayWorkingKey = bytearray()
        bytearrayWorkingKey.extend(map(ord, workingKey))
        decCipher = AES.new(hashlib.md5(bytearrayWorkingKey).digest(), AES.MODE_CBC, iv)
        return unpad(decCipher.decrypt(encryptedText).decode('utf-8'))