Check an SSL Cert Against Key

In case you ever need to check an SSL Cert against a Key to make sure you’ve got the right pair here’s a super minified bash one liner I wrote:

sck(){ c=`echo -e “$1\n$2″|grep -o ‘.*.crt’`;k=`echo -e “$1\n$2″|grep -o ‘.*.key’`;x=`(o(){ openssl $1 -noout -modulus -in $2|openssl md5;};o x509 $c;o rsa $k)|uniq|wc -l`;(( $x – 1 ))&&echo “Fail”||echo “Pass”;}

And yes that is a function in a subshell in a function. o() was built to save about 30 characters, and whitespace was cut wherever possible in the name of the shortest piece of code to accomplish the task. The only output is Pass/Fail, but if you’re the kind of person that would know where to copy paste something like this you’re probably also the kind of person who doesn’t need 20 different colors and an animated ASCII gif in your command output.

One other cool feature lies in:

c=`echo -e “$1\n$2″|grep -o ‘.*.crt’`;k=`echo -e “$1\n$2″|grep -o ‘.*.key’`

This little gem means that it doesn’t care which position the key and cert are at in the parameters ie:

sck supercooldomain.com.crt supercooldomain.com.key

Is equally valid in comparison with:

sck supercooldomain.com.key supercooldomain.com.crt

There it is, hopefully someone else finds it useful.