python--md5

import hashlib
import os


#字符串md5
def string_md5(str_value):
if str_value:
hashmd5 = hashlib.md5()
hashmd5.update(str_value.encode('utf-8'))
str_md5 = hashmd5.hexdigest()
return str_md5
else:
return None


# 字符串md5
def bytes_string_md5(str_value):
if str_value:
hashmd5 = hashlib.md5()
hashmd5.update(str_value)
str_md5 = hashmd5.hexdigest()
return str_md5
else:
return None


#文件md5
def file_md5(pathandname):
if os.path.isfile(pathandname):
hashmd5 = hashlib.md5()
file = open(pathandname, 'rb')
while True:
b = file.read(1024)
if not b:
break
hashmd5.update(b)
file.close()
return hashmd5.hexdigest()
else:
return None

posted on 2018-02-04 16:27  我要的明天  阅读(270)  评论(0编辑  收藏  举报

导航