加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
toHash.py 1.75 KB
一键复制 编辑 原始数据 按行查看 历史
EITarget 提交于 2022-03-16 16:09 . New master 0308 (#9)
import hashlib
import os
# 生成字符串的MD5值
def str2md5(content=None):
if not content:
return ''
md5gen = hashlib.md5()
md5gen.update(content.encode())
return md5gen.hexdigest()
# 生成字符串的SHA256值
def str2sha256(content=None):
if not content:
return ''
sha256gen = hashlib.sha256()
sha256gen.update(content.encode())
return sha256gen.hexdigest()
# 生成文件的MD5值
def file2md5(filename):
hash_value = ''
if os.path.exists(filename):
try:
md5obj = hashlib.md5()
with open(filename, 'rb') as f:
md5obj.update(f.read())
hash_value = md5obj.hexdigest()
except Exception as e:
print(e)
return hash_value
def file2sha256(filename):
hash_value = ''
if os.path.exists(filename):
try:
sha256obj = hashlib.sha256()
with open(filename, "rb") as f:
sha256obj.update(f.read())
hash_value = sha256obj.hexdigest()
except Exception as e:
print(e)
return hash_value
def file2sha1(filename):
hash_value = ''
if os.path.exists(filename):
try:
sha1obj = hashlib.sha1()
with open(filename, 'rb') as f:
sha1obj.update(f.read())
hash_value = sha1obj.hexdigest()
except Exception as e:
print(e)
return hash_value
def file2sha3(filename):
hash_value = ''
if os.path.exists(filename):
try:
sha3obj = hashlib.sha3_384()
with open(filename, 'rb') as f:
sha3obj.update(f.read())
hash_value = sha3obj.hexdigest()
except Exception as e:
print(e)
return hash_value
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化