If you think the Android project popcorn-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package nl.xservices.plugins;
/*www.java2s.com*/import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import javax.net.ssl.HttpsURLConnection;
import javax.security.cert.CertificateException;
import java.io.IOException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
publicclass SSLCertificateChecker extends CordovaPlugin {
privatestaticfinal String ACTION_CHECK_EVENT = "check";
privatestaticchar[] HEX_CHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
@Override
publicboolean execute(final String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
if (ACTION_CHECK_EVENT.equals(action)) {
cordova.getThreadPool().execute(new Runnable() {
publicvoid run() {
try {
final String serverURL = args.getString(0);
final String allowedFingerprint = args.getString(1);
final String allowedFingerprintAlt = args.getString(2);
final String serverCertFingerprint = getFingerprint(serverURL);
if (allowedFingerprint.equalsIgnoreCase(serverCertFingerprint) || allowedFingerprintAlt.equalsIgnoreCase(serverCertFingerprint)) {
callbackContext.success("CONNECTION_SECURE");
} else {
callbackContext.success("CONNECTION_NOT_SECURE");
}
} catch (Exception e) {
callbackContext.error("CONNECTION_FAILED. Details: " + e.getMessage());
}
}
});
return true;
} else {
callbackContext.error("sslCertificateChecker." + action + " is not a supported function. Did you mean '" + ACTION_CHECK_EVENT + "'?");
return false;
}
}
privatestatic String getFingerprint(String httpsURL) throws IOException, NoSuchAlgorithmException, CertificateException, CertificateEncodingException {
final HttpsURLConnection con = (HttpsURLConnection) new URL(httpsURL).openConnection();
con.connect();
final Certificate cert = con.getServerCertificates()[0];
final MessageDigest md = MessageDigest.getInstance("SHA1");
md.update(cert.getEncoded());
return dumpHex(md.digest());
}
privatestatic String dumpHex(byte[] data) {
finalint n = data.length;
final StringBuilder sb = new StringBuilder(n * 3 - 1);
for (int i = 0; i < n; i++) {
if (i > 0) {
sb.append(' ');
}
sb.append(HEX_CHARS[(data[i] >> 4) & 0x0F]);
sb.append(HEX_CHARS[data[i] & 0x0F]);
}
return sb.toString();
}
}