Is it possible to calculate a SHA-1 hash in PowerShell V2.0?
The only information I can find online is with PowerShell V4.0.
PowerShell does not provide a cmdlet to compute the hash of a string. However, you can write a string to a stream and use the InputStream parameter of Get-FileHash to get the hash value.
In modern PowerShell, you can use Get-FileHash -a SHA1 /file/path/file.exe - see also: Get-Help Get-FileHash for help and a list of algorithms supported. It seems that the current default is SHA256 , which is the most common used for website download checksums.
The PowerShell cmdlet Get-FileHash generates hash values both for files or streams of data. A hash is simply a function that converts one value into another. Sometimes the hash value may be smaller to save on space, or the hash value may be a checksum used to validate a file.
What is a SHA-1 Hash? SHA-1 (Secure Hash Algorithm) is a 160 bit cryptographic hash function created by the NSA in 1995. It creates a 40 byte hash value for the input of the algorithm. SHA-1 is one-way, meaning that the original input cannot be be determined simply by knowing the hash value.
I can't remember back in PowerShell V2 days if .NET 3.5 was typically installed too. I think it's the case.
You can always try the following and see if it works:
$file = 'd:\scripts\sha1.ps1'
$sha1 = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider
[System.BitConverter]::ToString( $sha1.ComputeHash([System.IO.File]::ReadAllBytes($file)))
Replace the value of $file
with the name of a file you have.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With