Calculate MD5 and base64 hash of a text string

I was trying to convert some text to MD5 or base64 encoding. Here are a couple of powershell functions that can help with that task

Function to convert to Base64

[sourcecode]

# Function to Convert to Account name to Base64
function ConvertTo-Base64($string) {
$bytes = [System.Text.Encoding]::UTF8.GetBytes($string);
$encoded = [System.Convert]::ToBase64String($bytes);
return $encoded;
}
[/sourcecode]

Function to convert to MD5

[sourcecode]

Function ConvertTo-MD5($string) {
$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider;
$utf8 = new-object -TypeName System.Text.UTF8Encoding;
$hash = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($String))).Replace("-", "");
return $hash;
}
[/sourcecode]

Here is the rest of the script to put it all together

[sourcecode]
# Get Strings to convert

$base64string = Read-host "Type the base64 string here"
$md5string = Read-host "Type the MD5 string here" -AsSecureString
$BSTR = `
[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecretKey)
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
$aktemp = ConvertTo-Base64($base64string)
$sktemp = ConvertTo-MD5($PlainPassword)
Write-host "The base64 string is: " $aktemp
Write-host "The MD5 stirng is: " $sktemp

[/sourcecode]

Subscribe For Latest Updates

Sign up for best of digital marketing, livestream and technology opinions on what matters to you.

Invalid email address
We promise not to spam you. You can unsubscribe at any time.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: