首页 > 编程语言 > 详细

python hashlib

时间:2019-11-01 22:11:11      阅读:118      评论:0      收藏:0      [点我收藏+]

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 = bHello,
key = bsecret
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\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(\rHas 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(\n%s\n%s\n %(root,dict[md5]))
            else:
                pass
            dict[md5] = root
            n += 1
    print(\nDone.)

if __name__ == __main__:
    path = input(Input path:\n)
    walk(path)

 

python hashlib

原文:https://www.cnblogs.com/wztshine/p/11779600.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!