Skip to content

Instantly share code, notes, and snippets.

@natesubra
Created January 24, 2023 06:17
Show Gist options
  • Save natesubra/453a7bc06138991839ec5ba2342bff98 to your computer and use it in GitHub Desktop.
Save natesubra/453a7bc06138991839ec5ba2342bff98 to your computer and use it in GitHub Desktop.
function Get-StringHash {
param (
[Parameter(
Position=0,
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[string] $ClearString,
[Parameter(Mandatory=$false)]
[ValidateSet('md5', 'sha1', 'sha256', 'sha384', 'sha512')]
[string] $HashAlgorithm = 'sha256'
)
$hasher = [System.Security.Cryptography.HashAlgorithm]::Create($HashAlgorithm)
$hash = $hasher.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($ClearString))
$hashString = [System.BitConverter]::ToString($hash)
$hashString.Replace('-', '')
}
function Get-URLHash {
param (
[Parameter(
Position=0,
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[string] $URL,
[Parameter(Mandatory=$false)]
[ValidateSet('md5', 'sha1', 'sha256', 'sha384', 'sha512')]
[string] $HashAlgorithm = 'sha256'
)
$hasher = [System.Security.Cryptography.HashAlgorithm]::Create($HashAlgorithm)
$hash = $hasher.ComputeHash([System.Net.WebRequest]::Create($URL).GetResponse().GetResponseStream())
$hashString = [System.BitConverter]::ToString($hash)
$hashString.Replace('-', '')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment