zoukankan      html  css  js  c++  java
  • hashlib的md5计算

    hashlib的md5计算

    hashlib概述

    涉及加密服务:Cryptographic Services
    其中 hashlib是涉及 安全散列消息摘要 ,提供多个不同的加密算法借口,如SHA1、SHA224、SHA256、SHA384、SHA512、MD5等。

    >>> import hashlib 
    >>> m = hashlib.md5() #创建hash对象,md5:(message-Digest Algorithm 5)消息摘要算法,得出一个128位的密文
    >>> m.update("Nobody inspects") #更新哈希对象以字符串参数
    >>> m.update(" the spammish repetition") #更新哈希对象以字符串参数
    >>> m.digest() #返回摘要,作为二进制数据字符串值
    >>> m.hexdigest() #返回十六进制数字字符串 
    

    基本用法

    hashlib.md5('hiyang').hexdigest()
    '97b21efa4d5de4c1a6a2b332f1e183e5'
    
    >>> m = hashlib.md5()
    >>> m.update('hiyang')
    >>> m.hexdigest()
    '97b21efa4d5de4c1a6a2b332f1e183e5'
    

    使用new指定加密算法 new(name, string='')

    hashlib.new('md5','hiyang').hexdigest()
    '97b21efa4d5de4c1a6a2b332f1e183e5'
    
    h = hashlib.new('md5')
    h.update('hiyang')
    hashlib.md5('hiyang').hexdigest()
    '97b21efa4d5de4c1a6a2b332f1e183e5'
    

    范例

    • md5sum() 计算文件的md5
    • md5sumd() 计算目录下文件的md5
    • md5sumd2() 计算目录下文件的md5,返回一个生成器
    • md5CheckFile() 通过md5比较文件内容是否相同
    #!/usr/bin/env python
    # python 2
    # encoding:utf8
    
    import hashlib
    import sys
    import os
    
    def md5sum(f, readsize=4096):
        """to calculate md5 of a file, readsize default is 4096 bytes
        return result"""
        m = hashlib.md5()
        with open(f, 'rb') as fd:
            data = fd.read(readsize)
            while data:
                m.update(data)
                data = fd.read(readsize)
            else:
                return m.hexdigest()
    
    def md5sumd(filename):
        """to calculate md5 of a directory
        return result"""
        _md5sumd = ''
        for paths, dirs, files in os.walk(filename):
            for f in files:
                fn = os.path.join(paths, f)
                _md5sumd += md5sum(fn)+'  '+fn+'
    '
        return _md5sumd
    
    def md5sumd2(filename):
        """to calculate md5 of a directory
        return generator"""
        for paths, dirs, files in os.walk(filename):
            for f in files:
                fn = os.path.join(paths, f)
                yield md5sum(fn)+'  '+fn
    
    def md5CheckFile(filename='.'):
        """calculate md5 of a directory, to check the same file
        return a directory {md5: [filename,...]}, these file has same md5"""
        dic = {}
        for paths, dirs, files in os.walk(filename):
            for f in files:
                fn = os.path.join(paths, f)
                md5 = md5sum(fn)
                if dic.has_key(md5):
                    dic[md5].append(fn)
                else:
                    dic[md5] = [fn]
            return {k: v for k, v in dic.items() if len(v) >= 2}
    
    if __name__ == '__main__':
        try:
            filename = sys.argv[1]
        except IndexError:
            filename = '.'
    
        if os.path.isfile(filename):
            print md5sum(filename)+'  '+filename
        elif os.path.isdir(filename):
            _md5sumd = md5sumd(filename)
            print _md5sumd,
            print '-' * 20, 'total', '-' * 20
            print hashlib.md5(_md5sumd).hexdigest()+'  '+filename
        else:
            print >> sys.stderr, 'file %s is not exist.' % filename
    
  • 相关阅读:
    python的正负无穷float("inf")的用法
    python中几个常见的黑盒子之“字典dict” 与 “集合set”
    python中几个常见的“黑盒子”之 列表list
    Python 排序---sort与sorted学习
    django中使用Profile扩展User模块(基于django 1.10版本下)
    获取Linux进程运行在哪个CPU内核上面的方法
    【转】Crontab定时任务配置
    【转】sudo命令情景分析
    【转】详解Python的装饰器
    一步步来配置安卓开发环境ADTBundle
  • 原文地址:https://www.cnblogs.com/hiyang/p/12631897.html
Copyright © 2011-2022 走看看