• python遍历文件并且进行md5计算


    import os
    import sys
    
    dirPath = sys.argv[1]
    
    def usage():
        print "python" + sys.argv[0] + "dirname"
    
    class Encrypt(object):
        def __init__(self,dirName):
            self.dirName = dirName
    
        def getFileFromDir(self):
            dirPath = self.dirName
            allFiles = os.walk(dirPath)
            fileHandler = open('tmp.txt','wb')
    
            for pathName, dirList, fileList in allFiles:
                for fileName in fileList:
                    singleFileName = fileName
                    absFilePath = pathName + os.sep + singleFileName
                    fileHandler.write(absFilePath+'
    ')
    
        def md5Encrypt(self):
            import hashlib
    
            for fileName in file('tmp.txt', 'r').readlines():
                fileName = fileName.strip()
                md5Object = hashlib.md5()
                with open(fileName, 'rb') as f:
                    md5Object.update(f.read())
                    md5Hash = md5Object.hexdigest()
                    print fileName+','+md5Hash
    
    if __name__ == '__main__':
        if len(sys.argv) != 1:
            usage()
            
        en = Encrypt(dirName=dirPath)
        en.getFileFromDir()
        en.md5Encrypt()
    

      又从slqmap源码里面看到一个实现,还挺有意思的:

    def md5File(filename):
        """
        Calculates MD5 digest of a file
        Reference: http://stackoverflow.com/a/3431838
        """
    
        checkFile(filename)
    
        digest = hashlib.md5()
        with open(filename, "rb") as f:
            for chunk in iter(lambda: f.read(4096), ""):
                digest.update(chunk)
    
        return digest.hexdigest()
    

      

  • 相关阅读:
    call()与apply()的作用与区别
    Tomcat8/9的catalina.out中文乱码问题解决
    怎样查看Jenkins的版本
    每日日报2020.8.18
    528. Random Pick with Weight
    875. Koko Eating Bananas
    721. Accounts Merge
    515. Find Largest Value in Each Tree Row
    286. Walls and Gates (Solution 1)
    408. Valid Word Abbreviation
  • 原文地址:https://www.cnblogs.com/websec/p/9355229.html
Copyright © 2020-2023  润新知