• 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()
    

      

  • 相关阅读:
    Java的字符串及格式化输入输出
    Java的数据类型与类型转换
    java基本程序
    svn基础入门
    github基础入门笔记
    git基础入门笔记
    linux基础入门笔记
    二、FreeMarker 模版开发指南 第二章 数值和类型
    【CodeForces】[599B]Spongebob and Joke
    【CodeForces】[612B]HDD is Outdated Technology
  • 原文地址:https://www.cnblogs.com/websec/p/9355229.html
Copyright © 2020-2023  润新知