使用 Powershell 比较两个哈希值

Dan*_*son 3 powershell

我在同一目录中有两个文件我希望比较哈希值,Release.ziprelease821hash.txt

\n

我必须使用的第一个命令是 get-filehash Release.zip -a md5\n我必须使用的第二个命令是get-content release821hash.txt

\n

然后,我必须使用 -eq 来比较哈希值,按照实验室要求定义:

\n
1. Type "new hash" -eq "known hash" and press Enter to determine whether the\nfile hashes match.\n\xe2\x96\xa0 The new hash is the hash generated by the get-filehash file_name -a\nmd5 command.\n\xe2\x96\xa0 The known hash is the hash generated by the get-content\nfile_name.txt command.\n\xe2\x96\xa0 Include the quotation marks and the file extensions with the file names\nin the commands.\n
Run Code Online (Sandbox Code Playgroud)\n

但是我的 powershell 抛出错误。我尝试使用所有可能的方法来使用 -eq 进行比较,但它不断抛出错误。我究竟做错了什么?我尝试过不使用引号,只在“get”命令周围使用引号,并对整行使用一个引号。

\n

d

\n

Lie*_*ers 5

Get-FileHash返回一个对象,其中包含哈希值等。要获取实际的哈希值,您必须使用Hash对象的属性。

所以你可以做

$fileHash = (Get-FileHash Release.zip -a md5).Hash
Run Code Online (Sandbox Code Playgroud)

Get-Content可能返回单个字符串,但也可能返回字符串数组,具体取决于您正在读取的文件中存在的换行符。假设文件仅包含哈希值,以下内容可能就足够了

$checkHash = Get-Content release821hash.txt
Run Code Online (Sandbox Code Playgroud)

将两者缝合在一起,您的支票可能是

$fileHash -eq $checkHash
Run Code Online (Sandbox Code Playgroud)

或者

((Get-FileHash Release.zip -a md5).Hash) -eq (Get-Content release821hash.txt)
Run Code Online (Sandbox Code Playgroud)

注意:如果您知道哈希值相等但检查返回 false,则release821hash.txt 文件中很可能存在其他字符,您必须从字符串中删除这些字符。