• python hashlib


    1. hashlib:MD5,sha256,sha512

    import hashlib
     
    # ######## md5 ########
     
    hash = hashlib.md5()
    hash.update('admin')
    print hash.hexdigest()
     
    # ######## sha1 ########
     
    hash = hashlib.sha1()
    hash.update('admin')
    print hash.hexdigest()
     
    # ######## sha256 ########
     
    hash = hashlib.sha256()
    hash.update('admin')
    print hash.hexdigest()
     
     
    # ######## sha384 ########
     
    hash = hashlib.sha384()
    hash.update('admin')
    print hash.hexdigest()
     
    # ######## sha512 ########
     
    hash = hashlib.sha512()
    hash.update('admin')
    print hash.hexdigest()

    2. hmac:

    带有key的hash,可以自定义key值加密,注意message、key都是bytes类型! str需要先转成bytes。

    import hmac
    message = b'Hello,'
    key = b'secret'
    h = hmac.new(key, message, digestmod='MD5')
    h.update(b' world!')
    # 如果消息很长,可以多次调用h.update(msg)
    print(h.hexdigest())

    文件夹查找相同文件:

    import hashlib
    import os
    import send2trash
    
    '''find the same file in a folder ,
    use md5 and sha512 to make sure they are same files'''
    
    def getMD5(path):
        d5 = hashlib.md5()      #生成一个hash的对象
        with open(path,'rb') as f:
            while True:
                content = f.read(40960)   # 每次读取40960大小,防止打开大文件导致内存溢出
                if not content:
                    break
                d5.update(content)   # 每次读取一部分,多次调用,和一次性调用效果一样
        # print('MD5 : %s' % d5.hexdigest())
        return d5.hexdigest()        # 16进制的hash值
    
    def getSha512(path):
        sh = hashlib.sha512()
        with open(path,'rb') as f:
            while True:
                content = f.read(40960)
                if not content:
                    break
                sh.update(content)
        # print(sh.hexdigest())
        return sh.hexdigest()
    
    def walk(path):
        x = input('Want to delete duplicate file? y/n
    ')
        if x.lower() == 'y':
            delete = True
        else:
            delete = False
        dict = {}
        n = 1
        for folder,subfolder,filenames in os.walk(path):
            for filename in filenames:
                print('
    Has scanned %s files' %n,end='')
                root = os.path.join(folder,filename)
                md5 = getMD5(root)
                if md5 in dict:
                    sha1 = getSha512(root)
                    sha2 = getSha512(dict[md5])
                    if sha1 == sha2:
                        # delete to recycle_bin
                        if delete == True:
                            send2trash.send2trash(dict[md5])
                        print('
    %s
    %s
    ' %(root,dict[md5]))
                else:
                    pass
                dict[md5] = root
                n += 1
        print('
    Done.')
    
    if __name__ == '__main__':
        path = input('Input path:
    ')
        walk(path)
  • 相关阅读:
    CentOS 6.6 升级GCC G++ (当前最新版本为v6.1.0) (完整)
    telnet: Unable to connect to remote host: Connection refused
    bash: telnet: command not found (Linux安装telnet)
    telnet: Unable to connect to remote host: No route to host
    IP地址转换函数
    Linux 网络通信 API详解【转载】
    高效算法求解数独
    Java创建List、Map等集合对象的同时进行赋值操作
    根据先序遍历和中序遍历建立二叉树
    继承内部类时使用外部类对象.super()调用内部类的构造方法
  • 原文地址:https://www.cnblogs.com/wztshine/p/11779600.html
Copyright © 2020-2023  润新知