Android Open Source - Swinedroid Certificate Inspect






From Project

Back to project page Swinedroid.

License

The source code is released under:

GNU General Public License

If you think the Android project Swinedroid 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 com.legind.ssl.CertificateInspect;
//from   ww w.  jav a 2 s .c om
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.util.Log;

public class CertificateInspect{
  private final String LOG_TAG = "com.legind.ssl.CertificateInspect";
  private final byte[] encodingTable = {
    (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
    (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
  };
  private X509Certificate certificate;
  
  public CertificateInspect(X509Certificate certificateLocal){
    certificate = certificateLocal;
  }
  
  private int encode(byte[] data, int off, int length, OutputStream out) throws IOException{
    for (int i = off; i < (off + length); i++){
      int    v = data[i] & 0xff;
      out.write(encodingTable[(v >>> 4)]);
      out.write(encodingTable[v & 0xf]);
    }
    return length * 2;
  }
  
  /*
   * Generate a string with the hash of the certificate.
   * 
   * @param hashingAlgorithm the hashing function
   * @return a string of the hash, or null if an exception has occurred
   */
  public String generateFingerprint(String hashingAlgorithm){
    StringBuffer buf = new StringBuffer();
    try{
      MessageDigest md = null;
      md = MessageDigest.getInstance(hashingAlgorithm);
      byte[] digest = md.digest(certificate.getEncoded());
      ByteArrayOutputStream out = new ByteArrayOutputStream(digest.length*2);
      encode(digest, 0, digest.length, out);
      String all = null;
      all = new String(out.toByteArray(), "US-ASCII").toUpperCase();
      Matcher matcher = Pattern.compile("..").matcher(all);
      while(matcher.find()) {
        if(buf.length() > 0) {
          buf.append(":");
        }
        buf.append(matcher.group());
      }
      return buf.toString();
    } catch (NoSuchAlgorithmException e){
      Log.e(LOG_TAG, e.toString());
    } catch (IOException e){
      Log.e(LOG_TAG, e.toString());
    } catch (CertificateEncodingException e){
      Log.e(LOG_TAG, e.toString());
    }
    return null;
  }
  
}




Java Source Code List

com.legind.Dialogs.ErrorMessageHandler.java
com.legind.Dialogs.MessageHandler.java
com.legind.sqlite.AlertDbAdapter.java
com.legind.sqlite.DbAdapter.java
com.legind.sqlite.ServerDbAdapter.java
com.legind.ssl.CertificateInspect.CertificateInspect.java
com.legind.ssl.SSLHandler.SSLHandler.java
com.legind.ssl.TrustManagerFactory.TrustManagerFactory.java
com.legind.swinedroid.AlertList.java
com.legind.swinedroid.AlertSearch.java
com.legind.swinedroid.AlertView.java
com.legind.swinedroid.ServerEdit.java
com.legind.swinedroid.ServerHashDialog.java
com.legind.swinedroid.ServerView.java
com.legind.swinedroid.Swinedroid.java
com.legind.swinedroid.NetworkRunnable.NetworkRunnableBindRequires.java
com.legind.swinedroid.NetworkRunnable.NetworkRunnableManager.java
com.legind.swinedroid.NetworkRunnable.NetworkRunnableRequires.java
com.legind.swinedroid.NetworkRunnable.NetworkRunnableUniqueRequires.java
com.legind.swinedroid.NetworkRunnable.NetworkRunnable.java
com.legind.swinedroid.RequestService.RequestElement.java
com.legind.swinedroid.RequestService.Request.java
com.legind.swinedroid.xml.AlertListXMLElement.java
com.legind.swinedroid.xml.AlertListXMLHandler.java
com.legind.swinedroid.xml.AlertXMLElement.java
com.legind.swinedroid.xml.AlertXMLHandler.java
com.legind.swinedroid.xml.OverviewXMLElement.java
com.legind.swinedroid.xml.OverviewXMLHandler.java
com.legind.swinedroid.xml.XMLHandlerException.java
com.legind.swinedroid.xml.XMLHandler.java
com.legind.web.WebTransport.WebTransportConnection.java
com.legind.web.WebTransport.WebTransportException.java
com.legind.web.WebTransport.WebTransport.java
org.achartengine.chartlib.AbstractChart.java
org.achartengine.chartlib.AlertChart.java