使用 Powershell 比较两个哈希值

问题描述 投票:0回答:3

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

我必须使用的第一个命令是

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

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

1. Type "new hash" -eq "known hash" and press Enter to determine whether the
file hashes match.
■ The new hash is the hash generated by the get-filehash file_name -a
md5 command.
■ The known hash is the hash generated by the get-content
file_name.txt command.
■ Include the quotation marks and the file extensions with the file names
in the commands.

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

powershell
3个回答
5
投票

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

所以你可以做

$fileHash = (Get-FileHash Release.zip -a md5).Hash

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

$checkHash = Get-Content release821hash.txt

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

$fileHash -eq $checkHash

((Get-FileHash Release.zip -a md5).Hash) -eq (Get-Content release821hash.txt)

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


0
投票

您必须将实际的哈希值放入命令行中。示例:“4A84C7958C246E39439C784349F4ZDB4”-eq“9C784349F4ZDB44A84C7958C246E3943”


0
投票

要检查是否进行比较,只需输入以下内容

“4A84C7958C246E39439C784349F4ZDB4”-eq“9C784349F4ZDB44A84C7958C246E3943”

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.