💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# hashlib.md5() `md5值` #### 一、hashlib概述 涉及加密服务:[Cryptographic Services][1] 其中 hashlib是涉及 `安全散列` 和 `消息摘要` ,提供多个不同的加密算法借口,如SHA1、SHA224、SHA256、SHA384、SHA512、MD5等。 ```python >>> 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比较文件内容是否相同 ```python #!/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+'\n' 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 ``` [1]: https://docs.python.org/2.7/library/crypto.html