不同的 SHA1 但相同的 SHA256
Different SHA1 but same SHA256
我正在遍历包含二进制文件的文件夹,并尝试计算每个文件的哈希值,特别是 sha1 和 sha256。在我的运行中,我奇怪地得到了所有文件的相同 sha256 值,但是 sha1 值不同(因此是正确的)。
下面是输出文件的屏幕截图,显示 sha1 散列已正确完成。但是 sha256 不是。 (对不起,每个二进制文件的文件名也是它的 sha1)
我的流程有问题吗?这是Python中的相关代码。我没有看到任何东西。对不起。
out.write("FILENAME,SHA1,SHA256\n")
for root, dirs, files in os.walk(input_path):
for ffile in files:
myfile = os.path.join(root, ffile)
nice = os.path.join(os.getcwd(), myfile)
fo = open(nice, "rb")
a = hashlib.sha1(fo.read())
b = hashlib.sha256(fo.read())
paylname = os.path.basename(myfile)
mysha1 = str(a.hexdigest())
mysha256 = str(b.hexdigest())
fo.close()
out.write("{0},{1},{2}\n".format(paylname, mysha1, mysha256))
正如我在上面的评论中所说,您正在读取整个文件的第一个哈希值,但您需要返回文件的开头以第二次读取它以获得第二个哈希值。或者你可以将它存储在一个变量中,并将其传递给每个散列。
out.write("FILENAME,SHA1,SHA256\n")
for root, dirs, files in os.walk(input_path):
for ffile in files:
myfile = os.path.join(root, ffile)
nice = os.path.join(os.getcwd(), myfile)
fo = open(nice, "rb")
a = hashlib.sha1(fo.read())
fo.seek(0,0) # seek back to start of file
b = hashlib.sha256(fo.read())
paylname = os.path.basename(myfile)
mysha1 = str(a.hexdigest())
mysha256 = str(b.hexdigest())
fo.close()
out.write("{0},{1},{2}\n".format(paylname, mysha1, mysha256))
我正在遍历包含二进制文件的文件夹,并尝试计算每个文件的哈希值,特别是 sha1 和 sha256。在我的运行中,我奇怪地得到了所有文件的相同 sha256 值,但是 sha1 值不同(因此是正确的)。
下面是输出文件的屏幕截图,显示 sha1 散列已正确完成。但是 sha256 不是。 (对不起,每个二进制文件的文件名也是它的 sha1)
我的流程有问题吗?这是Python中的相关代码。我没有看到任何东西。对不起。
out.write("FILENAME,SHA1,SHA256\n")
for root, dirs, files in os.walk(input_path):
for ffile in files:
myfile = os.path.join(root, ffile)
nice = os.path.join(os.getcwd(), myfile)
fo = open(nice, "rb")
a = hashlib.sha1(fo.read())
b = hashlib.sha256(fo.read())
paylname = os.path.basename(myfile)
mysha1 = str(a.hexdigest())
mysha256 = str(b.hexdigest())
fo.close()
out.write("{0},{1},{2}\n".format(paylname, mysha1, mysha256))
正如我在上面的评论中所说,您正在读取整个文件的第一个哈希值,但您需要返回文件的开头以第二次读取它以获得第二个哈希值。或者你可以将它存储在一个变量中,并将其传递给每个散列。
out.write("FILENAME,SHA1,SHA256\n")
for root, dirs, files in os.walk(input_path):
for ffile in files:
myfile = os.path.join(root, ffile)
nice = os.path.join(os.getcwd(), myfile)
fo = open(nice, "rb")
a = hashlib.sha1(fo.read())
fo.seek(0,0) # seek back to start of file
b = hashlib.sha256(fo.read())
paylname = os.path.basename(myfile)
mysha1 = str(a.hexdigest())
mysha256 = str(b.hexdigest())
fo.close()
out.write("{0},{1},{2}\n".format(paylname, mysha1, mysha256))