Get-FileHash
I work inC:\alpha>
, so the following code saves the given "text string" ("cat" or "dog") in the named file.
C:\alpha> "cat" > cat.txt
C:\alpha> "dog" > dog.txt
C:\alpha> "cat" > liar.txt
Get-FileHash [file_name]
returns the SHA256 Hash of the named file.
C:\alpha> Get-FileHash cat.txt
SHA256 9CC5AB4814B62C4AD6A3...
C:\alpha> Get-FileHash dog.txt
SHA256 A399A0812D4ABB07773A...
C:\alpha> Get-FileHash liar.txt
SHA256 9CC5AB4814B62C4AD6A3...
The Hash for cat.txt and liar.txt are the same. So, it is the contents of the file that are Hashed and not any of the meta-data.
If I make a file named npp.txt with Notepad++ containing the text 'cat' and run
Get-FileHash
, I get a different Hash, presumably because the BOM (the four binary header digits, I believe) is set up differently.
C:\alpha> Get-FileHash liar.txt
SHA256 77AF778B51ABD4A3C51C5...
And to some extent, a listing of the directory (with output truncated for ease of viewing) confirms this hypothesis (note the lengths).
C:\alpha> dir Mode Length Name ---- ------ ---- d----- ffmpeg d----- gmic -a---- 12 cat.txt -a---- 12 dog.txt -a---- 12 liar.txt -a---- 3 npp.txt
ls
is an acceptable alias for dir
, which in turn is an alias for Get-ChildItem
. I found this out by running Get-Help dir
. Interestingly enough, gci
is another alias. I was wonder if gci
was from the Solaris Operating System, as dir
is DOS and ls
is Linux. But after a few moments, I realized gci
is just the first letters of Get-ChildItem
.
As an aside, I only became interested in Get-FileHash because I wanted to Hash a string (and not a file). It turns out there is a very easy way to do this by using the gethashcode()
command.
PS C:\alpha> 'cat'.gethashcode()
-1741487357
PS C:\alpha> 'dog'.gethashcode()
-1432180093
PS C:\alpha> $dog_string = 'dog'
PS C:\alpha> $dog_string
dog
PS C:\alpha> $cat_string = 'cat'
PS C:\alpha> cat
cat
PS C:\alpha> $cat_string.gethashcode()
-1741487357
PS C:\alpha> $dog_string.gethashcode()
-1432180093
PS C:\alpha>
Note:
GetHashCode()
is only intended to be used internally to confirm two objects are the same (and not for Security Applications).