SHA1 compare PS script code

Dear expert

Code below is not working as it should, I just want $file and $file2 to compare with each other and if they match, delete $file, now my PS code delete all the $file when run the if statement is not working.

Anyone know why?

Regards

<code>
$file = "C:\2014_alla.potx"
$file2 = "C:2014_orange.potx"
$sha1 = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider
[System.BitConverter]::ToString( $sha1.ComputeHash([System.IO.File]::ReadAllBytes($file)))
$sha2 = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider
[System.BitConverter]::ToString( $sha2.ComputeHash([System.IO.File]::ReadAllBytes($file2)))

if ($sha1 -match $sha2){ Remove-Item $file } else { $filecheck = "OK"}
return $filecheck
</code>
LVL 1
WeTiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
You're comparing the two SHA1CryptoServiceProvider objects, which obviously are the same, instead of the hashes. And you only need one of those objects anyway.
And don't use "-match" when you want "-eq"; "-match" matches against a regular expression, and it would, for example, find two hashes the same if the first hash starts with the second one, but is longer.
Test
"abc" -eq "ab"
"abc" -match "ab"

Open in new window

$file = 'C:\2014_alla.potx'
$file2 = 'C:2014_orange.potx'
$sha1 = New-Object -TypeName System.Security.Cryptography.SHA1CryptoServiceProvider 
$hash1 = [System.BitConverter]::ToString($sha1.ComputeHash([System.IO.File]::ReadAllBytes($file)))
$hash2 = [System.BitConverter]::ToString($sha1.ComputeHash([System.IO.File]::ReadAllBytes($file2)))

If ($hash1 -eq $hash2) {
	Remove-Item -Path $file
	$filecheck = 'SAME'
} Else {
	$filecheck = 'DIFFERENT'
}
return $filecheck

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WeTiAuthor Commented:
correct, working :) thanks
0
Olaf DoschkeSoftware DeveloperCommented:
Surely -eq vs. -match plays a major role, but still will not explain why both files are deleted. The code only deletes one, if it finds some matching. But even if this wrongly assumes all files to match other files, you'd keep half of them.

So I think the deletion of both files can only be explained by how this code is called in an outer iteration comparing all file pairs twice, perhaps. So if file1=file2 and you delete file1, then also check file2=file1 and delete file2 because that's also true, you delete both files.

Only compare file pairs, where the second file name is greater than the first, and you only compare each pair of files once.

I also don't see how that would work, after one file is deleted, it can't occur in a second comparison of the reversed pair, but you might read in lists of files and pair them in advance, also I don't know what you get from ReadAllBytes() of a not yet anymore existing file, perhaps it returns an empty byte array as if the file existed and is empty. So always first check if the files you compare both still exist and were not deleted immediately beforehand. Another reason could be three equal files, of which two are deleted.

Logging what you do and why would help to find out. E.g., log deleted filename because of equal to another file, then such a case of file1=file2=file3 would be logged as:
1. deleted file1 as it's equal to file2
2. deleted file2 as it's equal to file3

And file3 should remain.

Bye, Olaf.
1
oBdACommented:
Olaf,
have another close look at the original code, with the irrelevant parts removed:
$sha1 = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider 
$sha2 = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider 
if ($sha1 -match $sha2) {...

Open in new window

$sha1 and $sha2 will always be the same, namely two objects of type SHA1CryptoServiceProvider.
The issue is that the file hashes were never compared.
0
Olaf DoschkeSoftware DeveloperCommented:
I didn't doubt this, still Remove-Item -Path $file only removes one file and the question is why both files are deleted. That can't be explained only by the wrong matching. More code has to be viewed to see why that happens.

Bye, Olaf.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.