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)