KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > CertificateUtil


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.util;
18
19 import org.apache.geronimo.util.encoders.HexEncoder;
20
21 import java.security.cert.Certificate JavaDoc;
22 import java.security.cert.CertificateEncodingException JavaDoc;
23 import java.security.NoSuchAlgorithmException JavaDoc;
24 import java.security.MessageDigest JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.util.regex.Matcher JavaDoc;
28 import java.util.regex.Pattern JavaDoc;
29
30 /**
31  * Various utility functions for dealing with X.509 certificates
32  *
33  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
34  */

35 public class CertificateUtil {
36     public static String JavaDoc generateFingerprint(Certificate JavaDoc cert, String JavaDoc digestAlgorithm) throws NoSuchAlgorithmException JavaDoc, CertificateEncodingException JavaDoc, IOException JavaDoc {
37         MessageDigest JavaDoc md = MessageDigest.getInstance(digestAlgorithm);
38         byte[] digest = md.digest(cert.getEncoded());
39         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc(digest.length*2);
40         new HexEncoder().encode(digest, 0, digest.length, out);
41         String JavaDoc all = new String JavaDoc(out.toByteArray(), "US-ASCII").toUpperCase();
42         Matcher JavaDoc matcher = Pattern.compile("..").matcher(all);
43         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
44         while(matcher.find()) {
45             if(buf.length() > 0) {
46                 buf.append(":");
47             }
48             buf.append(matcher.group());
49         }
50         return buf.toString();
51     }
52 }
53
Popular Tags