01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */package org.apache.geronimo.crypto;
17:
18: import org.apache.geronimo.crypto.encoders.HexEncoder;
19:
20: import java.security.cert.Certificate;
21: import java.security.cert.CertificateEncodingException;
22: import java.security.NoSuchAlgorithmException;
23: import java.security.MessageDigest;
24: import java.io.IOException;
25: import java.io.ByteArrayOutputStream;
26: import java.util.regex.Matcher;
27: import java.util.regex.Pattern;
28:
29: /**
30: * Various utility functions for dealing with X.509 certificates
31: *
32: * @version $Rev: 617737 $ $Date: 2008-02-01 17:39:58 -0800 (Fri, 01 Feb 2008) $
33: */
34: public class CertificateUtil {
35: public static String generateFingerprint(Certificate cert,
36: String digestAlgorithm) throws NoSuchAlgorithmException,
37: CertificateEncodingException, IOException {
38: MessageDigest md = MessageDigest.getInstance(digestAlgorithm);
39: byte[] digest = md.digest(cert.getEncoded());
40: ByteArrayOutputStream out = new ByteArrayOutputStream(
41: digest.length * 2);
42: new HexEncoder().encode(digest, 0, digest.length, out);
43: String all = new String(out.toByteArray(), "US-ASCII")
44: .toUpperCase();
45: Matcher matcher = Pattern.compile("..").matcher(all);
46: StringBuffer buf = new StringBuffer();
47: while (matcher.find()) {
48: if (buf.length() > 0) {
49: buf.append(":");
50: }
51: buf.append(matcher.group());
52: }
53: return buf.toString();
54: }
55: }
|