Last active
January 3, 2023 12:58
-
-
Save azhai/5955314 to your computer and use it in GitHub Desktop.
Python计算MD5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import hashlib | |
def md5hex(word): | |
""" MD5加密算法,返回32位小写16进制符号 """ | |
if isinstance(word, unicode): | |
word = word.encode("utf-8") | |
elif not isinstance(word, str): | |
word = str(word) | |
m = hashlib.md5() | |
m.update(word) | |
return m.hexdigest() | |
def md5sum(fname): | |
""" 计算文件的MD5值 """ | |
def read_chunks(fh): | |
fh.seek(0) | |
chunk = fh.read(8096) | |
while chunk: | |
yield chunk | |
chunk = fh.read(8096) | |
else: #最后要将游标放回文件开头 | |
fh.seek(0) | |
m = hashlib.md5() | |
if isinstance(fname, basestring) \ | |
and os.path.exists(fname): | |
with open(fname, "rb") as fh: | |
for chunk in read_chunks(fh): | |
m.update(chunk) | |
#上传的文件缓存 或 已打开的文件流 | |
elif fname.__class__.__name__ in ["StringIO", "StringO"] \ | |
or isinstance(fname, file): | |
for chunk in read_chunks(fname): | |
m.update(chunk) | |
else: | |
return "" | |
return m.hexdigest() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment