Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python hashlib对相同内容的复制文件使用不同的哈希_Python_Python 2.7 - Fatal编程技术网

python hashlib对相同内容的复制文件使用不同的哈希

python hashlib对相同内容的复制文件使用不同的哈希,python,python-2.7,Python,Python 2.7,运行Python2.7并尝试将两个不同文件的散列计算为变量,以便在布尔循环中进行比较和使用。首先,我在file1中生成内容,然后将file1复制到file2,并对file1和file2运行,我使用python hashlib获得不同的哈希值,但对两个不同的文件名运行powershell get filehash,我得到相同的哈希值(如我所料) file1和file2之间没有内容差异,只需使用内容创建file1并复制到file2即可 import sys import hashlib goldr

运行Python2.7并尝试将两个不同文件的散列计算为变量,以便在布尔循环中进行比较和使用。首先,我在file1中生成内容,然后将file1复制到file2,并对file1和file2运行,我使用python hashlib获得不同的哈希值,但对两个不同的文件名运行powershell get filehash,我得到相同的哈希值(如我所料)

file1和file2之间没有内容差异,只需使用内容创建file1并复制到file2即可

import sys
import hashlib

goldresulthashVar = None
testresulthashVar = None


def sha256hashcheck1():
    with open( 'goldresult.txt' ,"rb") as f:
        # Read and update hash string value in blocks of 4K
        for byte_block in iter(lambda: f.read(4096),b""):
            sha256_hash.update(byte_block)
        goldresulthashVar = sha256_hash.hexdigest()
        print goldresulthashVar

def sha256hashcheck2():
    with open( 'test.txt' ,"rb") as f2:
        # Read and update hash string value in blocks of 4K
        for byte_block in iter(lambda: f2.read(4096),b""):
            sha256_hash.update(byte_block)
        testresulthashVar = sha256_hash.hexdigest()
        print testresulthashVar     

sha256hashcheck1()
sha256hashcheck2()

任何指针或建议?

验证了我的脚本打算散列的文本文件的大小,并切换到一个小的单个文件读取,没有按照Andrej的文档指针进行更新

def sha256hashcheck1():
    with open( 'goldresult.txt' ,"rb") as f:
        bytes = f.read() # read entire file as bytes
        goldresulthashVar = hashlib.sha256(bytes).hexdigest();
        print(goldresulthashVar)

现在正在跨多个文件获得良好的可确认散列。

这是什么
sha256_散列
?如果您连续调用
update(a)
update(b)
您只需执行
update(a+b)
。请尝试
hashlib.sha256(byte_block).hexdigest()
Sorry-粘贴脚本时遗漏了一个变量声明,同时在脚本开始时将:sha256_hash=hashlib.sha256()作为声明的变量执行。。。现在读你的hashlib引文,泰