md5码加密(Python)

import hashlib
import hmac

m = input('输入要加密内容:')
md = hashlib.md5()#生成md5 hash对象
md.update(m.encode('utf-8'))#进行加密更新处理
print(md.hexdigest())#16进制表示,digets()为二进制表示

with open('D:\\test.txt','rb') as f:
    data = f.read()
md5 = hashlib.md5()
md5.update(data)
print(md5.hexdigest())

with open('D:\\test.txt','rb') as f:
    while True:
        data = f.read(1024)
        if not data:
            break
        md5 = hashlib.md5()
        md5.update(data)
print(md5.hexdigest())

sha1 = hashlib.sha1()
sha1.update('Biubiubiu'.encode('utf-8'))
print(sha1.hexdigest())

#带key加密
val=b'biubiubiu'
key=b'diudiu'
h=hmac.new(key, val, digestmod='MD5')
print(h.hexdigest())

 

posted @ 2019-04-08 19:16  Hk_Mayfly  阅读(246)  评论(0编辑  收藏  举报