有没有简单的方法来生成(和检查)Python中文件列表的MD5校验和?(我有一个小程序,我正在研究,我想确认文件的校验和).
我有这个错误:
Traceback (most recent call last):
File "python_md5_cracker.py", line 27, in <module>
m.update(line)
TypeError: Unicode-objects must be encoded before hashing
Run Code Online (Sandbox Code Playgroud)
当我尝试在Python 3.2.2中执行此代码时:
import hashlib, sys
m = hashlib.md5()
hash = ""
hash_file = input("What is the file name in which the hash resides? ")
wordlist = input("What is your wordlist? (Enter the file name) ")
try:
hashdocument = open(hash_file, "r")
except IOError:
print("Invalid file.")
raw_input()
sys.exit()
else:
hash = hashdocument.readline()
hash = hash.replace("\n", "")
try:
wordlistfile = open(wordlist, …
Run Code Online (Sandbox Code Playgroud) 我使用了hashlib(它取代了Python 2.6/3.0中的md5),如果我打开一个文件并将其内容放入hashlib.md5()
函数中,它工作正常.
问题在于非常大的文件,它们的大小可能超过RAM大小.
如何在不将整个文件加载到内存的情况下获取文件的MD5哈希值?
我想让python读取到EOF,这样我就可以得到一个合适的哈希值,无论是sha1还是md5.请帮忙.这是我到目前为止:
import hashlib
inputFile = raw_input("Enter the name of the file:")
openedFile = open(inputFile)
readFile = openedFile.read()
md5Hash = hashlib.md5(readFile)
md5Hashed = md5Hash.hexdigest()
sha1Hash = hashlib.sha1(readFile)
sha1Hashed = sha1Hash.hexdigest()
print "File Name: %s" % inputFile
print "MD5: %r" % md5Hashed
print "SHA1: %r" % sha1Hashed
Run Code Online (Sandbox Code Playgroud) 我一直在查看hashlib文档但是在散列数据时没有找到任何关于使用salt的内容.
帮助会很棒.
真的不知道这里发生了什么,我需要在弹性 beantalk 上部署我的烧瓶应用程序,但不知何故改变了路径并且无法再运行 python application.py
[dotnet --info]
.NET Core SDK (reflecting any global.json):
Version: 2.1.701
Commit: 8cf7278aa1
Runtime Environment:
OS Name: Mac OS X
OS Version: 10.14
OS Platform: Darwin
RID: osx.10.14-x64
Base Path: /usr/local/share/dotnet/sdk/2.1.701/
Host (useful for support):
Version: 2.1.12
Commit: ccea2e606d
[brew -v]
Homebrew 2.2.0
Homebrew/homebrew-core (git revision 43ad0; last commit 2019-11-30)
Run Code Online (Sandbox Code Playgroud)
我也已经完成了这些步骤:
LDFLAGS: -L/usr/local/opt/openssl/lib
CPPFLAGS: -I/usr/local/opt/openssl/include
Run Code Online (Sandbox Code Playgroud)
还是出现这个错误:
File "/Users/ipchelsea/Library/Python/2.7/lib/python/site-packages/urllib3/util/ssl_.py", line 8, in <module>
from hashlib import md5, sha1, sha256
ImportError: cannot import name md5
Run Code Online (Sandbox Code Playgroud)
当我做 [brew link …
在多次搜索之后,我无法确定如何避免错误说明:"使用此代码时,必须在散列之前对Unicode对象进行编码":
pwdinput = input("Now enter a password:")
pwd = hashlib.sha1()
pwd.update(pwdinput)
pwd = pwd.hexdigest()
Run Code Online (Sandbox Code Playgroud)
我怎样才能克服这个错误?你如何编码Unicode对象?
我在Solaris 10(x86)上.
到现在为止,我使用的是python2.6.今天,我安装了python2.7,在2.7上导入hashlib时发生了一个奇怪的错误,但在2.6上没有:
Python 2.6:
root@myserver [PROD] # python2.6 -c "import hashlib"
root@myserver [PROD] #
Run Code Online (Sandbox Code Playgroud)
Python 2.7:
root@myserver [PROD] # python2.7 -c "import hashlib"
ERROR:root:code for hash md5 was not found.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/hashlib.py", line 139, in <module>
globals()[__func_name] = __get_hash(__func_name)
File "/usr/local/lib/python2.7/hashlib.py", line 91, in __get_builtin_constructor
raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type md5
ERROR:root:code for hash sha1 was not found.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/hashlib.py", line 139, in …
Run Code Online (Sandbox Code Playgroud) 使用python 2.7,以下代码计算文件内容的mD5 hexdigest.
(编辑:嗯,不是因为答案已经显示,我只是这么认为).
import hashlib
def md5sum(filename):
f = open(filename, mode='rb')
d = hashlib.md5()
for buf in f.read(128):
d.update(buf)
return d.hexdigest()
Run Code Online (Sandbox Code Playgroud)
现在,如果我使用python3运行该代码,则会引发TypeError异常:
d.update(buf)
TypeError: object supporting the buffer API required
Run Code Online (Sandbox Code Playgroud)
我发现我可以使用python2和python3运行代码,将其更改为:
def md5sum(filename):
f = open(filename, mode='r')
d = hashlib.md5()
for buf in f.read(128):
d.update(buf.encode())
return d.hexdigest()
Run Code Online (Sandbox Code Playgroud)
现在我仍然想知道为什么原始代码停止工作.看来,当使用二进制模式修饰符打开文件时,它返回整数而不是编码为字节的字符串(我说因为type(buf)返回int).这种行为是在某处解释的吗?
hashlib ×10
python ×9
md5 ×4
python-3.x ×3
unicode ×2
checksum ×1
hash ×1
homebrew ×1
openssl ×1
python-2.7 ×1
salt ×1
saltedhash ×1
sha ×1
sha1 ×1
solaris ×1
solaris-10 ×1
syntax-error ×1