KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > stringifier > X509CertificateStringifier


1
2 /*
3  * The contents of this file are subject to the terms
4  * of the Common Development and Distribution License
5  * (the License). You may not use this file except in
6  * compliance with the License.
7  *
8  * You can obtain a copy of the license at
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
10  * glassfish/bootstrap/legal/CDDLv1.0.txt.
11  * See the License for the specific language governing
12  * permissions and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL
15  * Header Notice in each file and include the License file
16  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
17  * If applicable, add the following below the CDDL Header,
18  * with the fields enclosed by brackets [] replaced by
19  * you own identifying information:
20  * "Portions Copyrighted [year] [name of copyright owner]"
21  *
22  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23  */

24  
25 /*
26  * $Header: /cvs/glassfish/admin-core/mbeanapi/src/java/com/sun/appserv/management/util/stringifier/X509CertificateStringifier.java,v 1.3 2005/12/25 03:52:02 tcfujii Exp $
27  * $Revision: 1.3 $
28  * $Date: 2005/12/25 03:52:02 $
29  */

30  
31 package com.sun.appserv.management.util.stringifier;
32
33
34 import java.security.cert.X509Certificate JavaDoc;
35 import java.security.MessageDigest JavaDoc;
36 import java.security.NoSuchAlgorithmException JavaDoc;
37
38 import com.sun.appserv.management.util.misc.StringUtil;
39
40
41 /**
42     Stringifies an X509CertificateStringifier.
43  */

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 JavaDoc alg )
57     {
58         byte[] result = null;
59         
60         try
61         {
62             final MessageDigest JavaDoc md = MessageDigest.getInstance( alg );
63             
64             result = md.digest( signature );
65         }
66         catch ( NoSuchAlgorithmException JavaDoc e )
67         {
68             result = signature;
69             e.printStackTrace();
70         }
71         
72         return( result );
73     }
74     
75     /**
76         Static variant when direct call will suffice.
77      */

78         public static String JavaDoc
79     stringify( final X509Certificate JavaDoc cert )
80     {
81         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
82         final String JavaDoc 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 JavaDoc
105     stringify( Object JavaDoc object )
106     {
107         return( stringify( (X509Certificate JavaDoc)object ) );
108     }
109 }
110
111
Popular Tags