What is the best way to compare a public key extracted from a server and a locally stored key in java?
Here is a code to get the public key
public static PublicKey getPublicKey(){
String hostname = "www.google.com";
SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory();
SSLSocket socket = null;
Certificate[] certs = null;
try {
socket = (SSLSocket) factory.createSocket(hostname, 443);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
socket.startHandshake();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
certs = socket.getSession().getPeerCertificates();
} catch (SSLPeerUnverifiedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Certificate cert = certs[0];
PublicKey key = cert.getPublicKey();
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
System.out.println("Public key \n" + key);
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
return key;
}
and i want to compare the public key from the response with a locally stored key for example in a text file.
Thanks