001/* 002 * Forge Mod Loader 003 * Copyright (c) 2012-2013 cpw. 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser Public License v2.1 006 * which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 008 * 009 * Contributors: 010 * cpw - implementation 011 */ 012 013package cpw.mods.fml.common; 014 015import java.nio.ByteBuffer; 016import java.security.MessageDigest; 017import java.security.cert.Certificate; 018 019public class CertificateHelper { 020 021 private static final String HEXES = "0123456789abcdef"; 022 023 public static String getFingerprint(Certificate certificate) 024 { 025 if (certificate == null) 026 { 027 return "NO VALID CERTIFICATE FOUND"; 028 } 029 try 030 { 031 MessageDigest md = MessageDigest.getInstance("SHA-1"); 032 byte[] der = certificate.getEncoded(); 033 md.update(der); 034 byte[] digest = md.digest(); 035 return hexify(digest); 036 } 037 catch (Exception e) 038 { 039 return null; 040 } 041 } 042 043 public static String getFingerprint(ByteBuffer buffer) 044 { 045 try 046 { 047 MessageDigest digest = MessageDigest.getInstance("SHA-1"); 048 digest.update(buffer); 049 byte[] chksum = digest.digest(); 050 return hexify(chksum); 051 } 052 catch (Exception e) 053 { 054 return null; 055 } 056 } 057 058 private static String hexify(byte[] chksum) 059 { 060 final StringBuilder hex = new StringBuilder( 2 * chksum.length ); 061 for ( final byte b : chksum ) { 062 hex.append(HEXES.charAt((b & 0xF0) >> 4)) 063 .append(HEXES.charAt((b & 0x0F))); 064 } 065 return hex.toString(); 066 } 067 068}