Skip to content

Instantly share code, notes, and snippets.

@dade80vr
Created November 1, 2018 21:12
Show Gist options
  • Save dade80vr/ddefa7ea4481d88deac2561914e4f52a to your computer and use it in GitHub Desktop.
Save dade80vr/ddefa7ea4481d88deac2561914e4f52a to your computer and use it in GitHub Desktop.
Bash script to check if a certificate and a private key match
#!/bin/bash
cert=$1
key=$2
if [[ $# -eq 0 ]]
then
echo "Arguments not given. Usage: ./checkcert.sh CERTIFICATE.crt PRIVKEY.key"
else
crthash=$(openssl x509 -noout -modulus -in "$cert" | openssl md5)
echo $cert $crthash
keyhash=$(openssl rsa -noout -modulus -in "$key" | openssl md5)
if [ "$keyhash" = "$crthash" ]
then
keytest=$(openssl rsa -in "$key" -check -noout)
echo $key $keyhash
echo "---- "$keytest" ----"
else
echo "!!!! Invalid key for given cert !!!!"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment