Get SSH public key fingerprint with powershell

I was working on a project with SSH keys.  I needed to get the key fingerprint but don't have access to ssh-keygen.  So with some poking around I was able to get the MD5 hash of the base64 keyfile.

Here's the code:

$keyFilePath = "SSHKeyFilePath.pub"
 
#Get the key
$rawText = [System.IO.File]::ReadAllText($keyFilePath)
$rawText = $rawText.Replace("`r","")
$rawText = $rawText.Replace("`n","")
$rawText -match "AA.+?=="
$keyText = $Matches[0]

#Compute the hash
 
$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$bd= [System.Convert]::FromBase64String($keyText);
$hash = [System.BitConverter]::ToString($md5.ComputeHash($bd))
$hash.Replace("-",":").ToLower()

Print | posted on Friday, January 23, 2015 10:46 AM