Skip to content

Instantly share code, notes, and snippets.

@PieterHeijman
Created August 31, 2017 14:43
Show Gist options
  • Save PieterHeijman/b4df36586075b7498debbe04373af6dc to your computer and use it in GitHub Desktop.
Save PieterHeijman/b4df36586075b7498debbe04373af6dc to your computer and use it in GitHub Desktop.
Check if a certificate file matches a private key file in Powershell

You can verify if a private key file matches a certificate file by comparing their modulus (which you can retrieve using openssl). I've wrapped this in the little script below.

param(
  [String]$cert,
  [string]$key
)

$certMod = openssl x509 -noout -modulus -in $cert
$keyMod = openssl rsa -noout -modulus -in $key

if ($certMod -ceq $keyMod) {
  Write-Host "key matches certificate"
} else {
  Write-Host "key does not match certificate" -ForegroundColor Red
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment