Python Hashlib返回不同的值

时间:2018-07-23 07:36:57

标签: python md5 sha256 hashlib

~~解决方案结束~~

我需要检查通过FTP发送的两个文件的完整性。除了SHA256散列,我已经进行了所有设置。让我从代码中进行解释。

BLOCK_SIZE = 65536
hasher = hashlib.sha256()
with open('/home/pi/' + fname, "rb") as targ:
    buf = targ.read(BLOCK_SIZE)
    while len(buf) > 0:
        hasher.update(buf)
        buf = targ.read(BLOCK_SIZE)

checkfile = open("osha256.txt", "w+")
checkfile.write(hasher.hexdigest())

hasher = hashlib.sha256()
with open("/home/pi/" + xr, "rb") as targ:
    buf = targ.read(BUF_SIZE)
    while len(buf) > 0:
        hasher.update(buf)
        buf = targ.read()

hashfile = open("/home/pi/vercheck/sha256.txt", "w+")
hashfile.write(str("{0}".format(hasher.hexdigest())))

第一个返回哈希值(?):c8a31cb076b21999bd2cdcfa5f446a7a6644de88037087112fa18bd90cc13984

第二个返回哈希值: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

文件名和目录相同。

md5 = hashlib.md5()
with open('/home/pi/cd.jpg', "rb") as targ1:
    buf = targ1.read(bufsize)
    while len(buf) > 0:
        md5.update(buf)
        buf = targ1.read(bufsize)
    checkfile = open("1md5.txt", "w+")
    checkfile.write(md5.hexdigest())
## RETURNS e1d147695131c7682372e26a36c4f6fa

with open('/home/pi/cd.jpg', "rb") as targ1:
    buf = targ1.read(bufsize)
    while len(buf) > 0:
         md5.update(buf)
         buf = targ1.read(bufsize)
    checkfile = open("2md5.txt", "w+")
    checkfile.write(md5.hexdigest()
##RETURNS 3966cdd94eaf06d30c5e6f25eecb6015

编辑

您可以通过添加另一个(以我为例)来解决此问题 md5 = hahslib.md5()

的顶部
with open('/home/pi/cd.jpg', "rb") as targ1:
    buf = targ1.read(bufsize)
    while len(buf) > 0:
         md5.update(buf)
         buf = targ1.read(bufsize)
    checkfile = open("2md5.txt", "w+")
    checkfile.write(md5.hexdigest()

0 个答案:

没有答案