Calculating SHA1 hash algorithm in PowerShell V2.0
Asked Answered
B

3

11

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.

Bernicebernie answered 1/12, 2014 at 15:55 Comment(1)
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.Ethelstan
T
26

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.

Tomtit answered 1/12, 2014 at 16:10 Comment(2)
Just a note to people trying this on large files like IOS's : Exception calling "ReadAllBytes" with "1" argument(s): "The file is too long. This operation is currently limiited to supporting files less than 2 gigabytes in size."Gobioid
It's probably better to use File.OpenRead instead of trying to load the entire file into memory at once. HashAlgorithm.ComputeHash has an overload for Stream.Vig
I
1

Yes, it is possible as it is part of NET 2.0. In fact, the PowerShell Community Extensions use the .NET hash support to implement the Get-Hash command. Version 2.1.1 installs and runs on PowerShell V2.

Incision answered 2/12, 2014 at 2:57 Comment(1)
Cheers for that! useful to know, but unfortunately i'm unable to upgrade powershell in any way shape or form.Bernicebernie
L
0

To calculate the SHA1 hash of a string (not a file):

$mystring = "Some string and text content here"
$mystream = [IO.MemoryStream]::new([byte[]][char[]]$mystring)
Get-FileHash -InputStream $mystream -Algorithm SHA1

From a Reddit post

Labefaction answered 28/6 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.