开发者

file size is dramatically increased after pickle

开发者 https://www.devze.com 2022-12-10 14:35 出处:网络
I\'m reading in a file and sending the data (once encrypted) to a dictionary, with a hash of the data before and after encryption. I th开发者_运维技巧en pickle the dictionary but find the file size is

I'm reading in a file and sending the data (once encrypted) to a dictionary, with a hash of the data before and after encryption. I th开发者_运维技巧en pickle the dictionary but find the file size is massive compared to the source file size. If I write the encrypted data straight to a file the size is identical to the source. Any idea why my pickled file is so large?

#Encrypt data and get hashes        
def encryptAndExportFile(self, key, inFile, outFile):

    openInFile = open(inFile,"rb")
    inFileSize = os.path.getsize(inFile)
    inFileData = openInFile.readlines()
    openInFile.close()

    """ initialise cipher """

    cipher = AES.new(key, AES.MODE_CFB)

    """ initialise MD5 """

    m = hashlib.md5() #hash
    h = hashlib.md5() #hash of encrypted dataq

    encryptedData = []

    for data in inFileData:

        m.update(data) 
        encData = cipher.encrypt(data)
        h.update(encData)
        encryptedData.append(encData)


    hashResult = m.digest()
    encHashResult = h.digest()

    return hashResult, encryptedData, encHashResult

def storeEncryptedObject(self, obj, path):

    outFile = open(path, 'wb')
    pickle.dump(obj, outFile)
    outFile.close()


Try using a binary pickle by specifying protocol=2 as a keyword argument to pickle.dump. It should be much more efficient.

0

精彩评论

暂无评论...
验证码 换一张
取 消