有没有办法找出两个文件的哈希值?

Is there any way to find out the hash value of two files?

我有 python 代码生成两个文件的散列值。第一个文件位于 c:\windows\system32\wscript.exe,另一个文件是第一个文件的克隆,位于 d:\clone.exe。

python代码

import os
strcommand ='certutil -hashfile c:\windows\system32\wscript.exe md2'
p=os.popen(strcommand ).read()
print(str(p).split('\n')[1])

strcommand1='certutil -hashfile d:\clone.exe md2'
p=os.popen(strcommand1 ).read()
print(str(p).split('\n')[1])

输出为

D:\pythonprogram>python clonefinder.py
4cef03889db08179b57035e4463a84d5
db1cefe474ce12678ea4d6c61dc42291

但是当我在命令提示符下使用python中使用的命令时,两个文件的哈希值相同

命令提示符

D:\pythonprogram>certutil -hashfile c:\windows\system32\wscript.exe md2
MD2 hash of c:\windows\system32\wscript.exe:
db1cefe474ce12678ea4d6c61dc42291
CertUtil: -hashfile command completed successfully.

D:\pythonprogram>certutil -hashfile d:\clone.exe md2
MD2 hash of d:\clone.exe:
db1cefe474ce12678ea4d6c61dc42291
CertUtil: -hashfile command completed successfully.

如果我正在执行 python 程序,我希望散列值相同

有什么帮助吗?

Windows 可能是一个相当有趣的操作系统,并且由于它的年龄,添加了一些魔法以允许旧的 windows 代码仍然可以与 windows 一起工作 7/8/ 10 在某些情况下,您可以在 C:\windows 等目录中看到不同版本的文件。取决于您的权限/取决于您是否启动 32 位/64 位应用程序。我不是很了解所有这些机制,但已经有一些糟糕的惊喜了。

可以 100% 确定,您不会在两个不同的环境中执行 certutil 命令。我提议如下。

  1. 开一个cmd.exewindow
  2. 键入 window
  3. 中的 certutil 命令
  4. 现在也可以从与 C:\Path_to_your_python\python.exe name_of_your_python_script.py 相同的 window 调用 python 脚本 使用 python 脚本的版本,其中您在正则表达式字符串前加上 r (r"regex")

如果您仍然有不同的结果,请检查您是否安装了 32 位版本或 64 位版本的 python。 C:\Path_to_your_python\python.exe -V

如果你是32位版本,那我建议安装64位版本的python再测试一下