1 2 24 25 30 31 package com.sun.appserv.management.util.stringifier; 32 33 34 import java.security.cert.X509Certificate ; 35 import java.security.MessageDigest ; 36 import java.security.NoSuchAlgorithmException ; 37 38 import com.sun.appserv.management.util.misc.StringUtil; 39 40 41 44 45 public final class X509CertificateStringifier implements Stringifier 46 { 47 public final static X509CertificateStringifier DEFAULT = new X509CertificateStringifier(); 48 49 public 50 X509CertificateStringifier() 51 { 52 } 53 54 55 private static byte[] 56 getFingerprint( byte[] signature, String alg ) 57 { 58 byte[] result = null; 59 60 try 61 { 62 final MessageDigest md = MessageDigest.getInstance( alg ); 63 64 result = md.digest( signature ); 65 } 66 catch ( NoSuchAlgorithmException e ) 67 { 68 result = signature; 69 e.printStackTrace(); 70 } 71 72 return( result ); 73 } 74 75 78 public static String 79 stringify( final X509Certificate cert ) 80 { 81 final StringBuffer buf = new StringBuffer (); 82 final String NL = "\n"; 83 84 buf.append( "Issuer: " + cert.getIssuerDN().getName() + NL); 85 buf.append( "Issued to: " + cert.getSubjectDN().getName() + NL); 86 buf.append( "Version: " + cert.getVersion() + NL); 87 buf.append( "Not valid before: " + cert.getNotBefore() + NL); 88 buf.append( "Not valid after: " + cert.getNotAfter() + NL); 89 buf.append( "Serial number: " + cert.getSerialNumber() + NL); 90 buf.append( "Signature algorithm: " + cert.getSigAlgName() + NL); 91 buf.append( "Signature algorithm OID: " + cert.getSigAlgOID() + NL); 92 93 buf.append( "Signature fingerprint (MD5): " ); 94 byte[] fingerprint = getFingerprint( cert.getSignature(), "MD5" ); 95 buf.append( StringUtil.toHexString( fingerprint, ":" ) + NL ); 96 97 buf.append( "Signature fingerprint (SHA1): " ); 98 fingerprint = getFingerprint( cert.getSignature(), "SHA1" ); 99 buf.append( StringUtil.toHexString( fingerprint, ":" ) + NL ); 100 101 return( buf.toString() ); 102 } 103 104 public String 105 stringify( Object object ) 106 { 107 return( stringify( (X509Certificate )object ) ); 108 } 109 } 110 111 | Popular Tags |