1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.eclipse.org/org/documents/epl-v10.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ide.eclipse.adt.internal.utils; 18 19 import java.util.Locale; 20 import java.security.MessageDigest; 21 import java.security.NoSuchAlgorithmException; 22 import java.security.cert.Certificate; 23 import java.security.cert.CertificateEncodingException; 24 25 public class FingerprintUtils { 26 27 /** 28 * Returns the {@link Certificate} fingerprint as returned by <code>keytool</code>. 29 * 30 * @param certificate 31 * @param hashAlgorithm 32 */ getFingerprint(Certificate cert, String hashAlgorithm)33 public static String getFingerprint(Certificate cert, String hashAlgorithm) { 34 if (cert == null) { 35 return null; 36 } 37 try { 38 MessageDigest digest = MessageDigest.getInstance(hashAlgorithm); 39 return toHexadecimalString(digest.digest(cert.getEncoded())); 40 } catch(NoSuchAlgorithmException e) { 41 // ignore 42 } catch(CertificateEncodingException e) { 43 // ignore 44 } 45 return null; 46 } 47 toHexadecimalString(byte[] value)48 private static String toHexadecimalString(byte[] value) { 49 StringBuffer sb = new StringBuffer(); 50 int len = value.length; 51 for (int i = 0; i < len; i++) { 52 int num = ((int) value[i]) & 0xff; 53 if (num < 0x10) { 54 sb.append('0'); 55 } 56 sb.append(Integer.toHexString(num)); 57 if (i < len - 1) { 58 sb.append(':'); 59 } 60 } 61 return sb.toString().toUpperCase(Locale.US); 62 } 63 }