Last active
February 11, 2020 21:50
-
-
Save jrdmb/d38da10534a7a56af32d to your computer and use it in GitHub Desktop.
Hash a string in Powershell with various encryption algorithms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Here is a simple script to hash a string using your chosen cryptography algorithm. | |
#Usage Examples: | |
#Get-StringHash "My String to hash" "MD5" | |
#Get-StringHash "My String to hash" "RIPEMD160" | |
#Get-StringHash "My String to hash" "SHA1" | |
#Get-StringHash "My String to hash" "SHA256" | |
#Get-StringHash "My String to hash" "SHA384" | |
#Get-StringHash "My String to hash" "SHA512" | |
#http://jongurgul.com/blog/#Get-StringHash-get-filehash/ | |
#https://gallery.technet.microsoft.com/scriptcenter/Get-StringHash-aa843f71 | |
Function Get-StringHash([String] $String,$HashName = "MD5") | |
{ | |
$StringBuilder = New-Object System.Text.StringBuilder | |
[System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))|%{ | |
[Void]$StringBuilder.Append($_.ToString("x2")) | |
} | |
$StringBuilder.ToString() | |
} | |
$myvar = Read-Host –Prompt 'Enter string to hash using SHA-512 algorithm' | |
Get-StringHash $myvar "SHA512" | |
Write-Host "Press any key to continue ..." | |
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | |
#PowerShell #encryption |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$myvar = Read-Host –Prompt 'Enter string to hash using SHA-512 algorithm'$myvar
The dash before "Prompt" is an endash, script doesn't run. I get
Easy enough to fix in the editor, but figured you might want to know that, might be some weird encoding bug that the character was replaced at some point.