Part Two – Computing email address hash value using Windows PowerShell

In part one I discussed computing the hash value using the MD5 hash algorithm for an email address, in this post I am going to discuss using other hash algorithms available in the HashAlgorithm Class to compute the value.

The available hash algorithms are MD5, RIPEMD160, SHA1, SHA256, SHA384 and SHA512 therefore in the advanced function we will set a parameter value which validates the input provided. By default, the hash algorithm of MD5 will be used unless specified.

[CmdletBinding()]
Param (
[Parameter(Mandatory=$True, ValueFromPipeline=$True, HelpMessage="Enter an email address to compute hash")]
[ValidateScript({If ($_ -match "\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b") {$True}
Else {Throw "The email address $_ is not a valid format."}})]
[String[]]$Email,
[ValidateSet('MD5','RIPEMD160','SHA1','SHA256','SHA384','SHA512')]
[String] $HashAlgorithm = "MD5"
)

The script block to compute the hash value does not differ from the example provided in the previous example which uses the StringBuilder Class .NET Framework object, other than we pass the hash algorithm parameter to the HashAlgorithm Class to compute the value.

Begin {} #Begin
Process
{
Write-Verbose ("The following email addresses have been specified " + ($Email -join ',') + ".")
ForEach ($String in $Email)
{
Write-Verbose ("Computing hash for the email address " + $String + " using the " + $HashAlgorithm + " algorithm.")
$StringBuilder = New-Object System.Text.StringBuilder
[System.Security.Cryptography.HashAlgorithm]::Create($HashAlgorithm).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))| ForEach-Object {
[Void]$StringBuilder.Append($_.ToString("x2"))} # ForEach-Object
return $StringBuilder.ToString()} # ForEach
} #Process
End {} #End
} # Function

Now be invoking the function and specifying a hash algorithm to compute the hash value I am able to use the default MD5 in the first instance and then each of the hash algorithms specified in the class.

Create-EmailHashPartTwoOutput

In the final part, I will look at using the  Blowfish-based hashing algorithm to compute the hash value and have an available advanced function and also provide the ability to specify not only an email address but a string value to compute the hash value.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s