Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* ************************************************************************
  #
  #  DivConq
  #
  #  http://divconq.com/
  #
  #  Copyright:
  #    Copyright 2014 eTimeline, LLC. All rights reserved.
  #
 #  License:
 #    See the license.txt file in the project's top-level directory for details.
 #
 #  Authors:
 #    * Andy White
 #
 ************************************************************************ */
 package divconq.util;
 
 import java.util.List;
 
 
 public class KeyUtil {
 	static public class CertSummary {
 		public String alias = null;
 		public String subject = null;
         public String thumbprint = null;  
         public boolean privatekey = false;
 	}
 	
 	static public FuncResult<Collection<CertSummary>> loadKeystoreSummary(Path keystorechar[] password) {
 		
 		KeyStore ks = null;
 		
         try {
         	// load key store
             ks = KeyStore.getInstance("JKS");
             ks.load(Files.newInputStream(keystore), password);
         }
         catch (Exception x) {
         	res.error("Unable to open/read keystore file: " + x);
         	return res;
         }
         
         try {
     		List<CertSummarycerts = new ArrayList<>();
     		res.setResult(certs);
 
             CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
             Enumeration<Stringaliases = ks.aliases();
             
             while (aliases.hasMoreElements()) {
               CertSummary summ = new CertSummary();
               summ.alias =  aliases.nextElement();
               
               byte[] encoded = null;
               
               if (ks.isCertificateEntry(summ.alias)) 
 	              encoded = ((KeyStore.TrustedCertificateEntryks.getEntry(summ.aliasnull)).getTrustedCertificate().getEncoded();
               else if (ks.isKeyEntry(summ.alias)) {
 	              encoded = ((KeyStore.PrivateKeyEntryks.getEntry(summ.aliasnew KeyStore.PasswordProtection(password))).getCertificate().getEncoded();
 	              summ.privatekey = true;
               }
               
               if (encoded == null) {
             	  res.error("Unable to decode: " + summ.alias);
             	  return res;
               }
               
               X509Certificate cert = (X509Certificate)certFactory.generateCertificate(new ByteArrayInputStream(encoded));				              
               
 	          summ.subject = cert.getSubjectDN().toString();
 	          summ.thumbprint = KeyUtil.getCertThumbprint(cert);  
               
               certs.add(summ);
             }
         } 
         catch (Exception x) {
         	res.error("Unable to decode keystore file: " + x);
         }
 	    
 		return res;
 	}
 	
     
 	  /* key export notes
     if ((key instanceof PrivateKey) && "PKCS#8".equals(key.getFormat())) {
       // Most PrivateKeys use this format, but check for safety. 
      try (FileOutputStream os = new FileOutputStream(alias + ".key")) {
        os.write(key.getEncoded());
        os.flush();
      }
    }
	   */
	public static String getCertThumbprint(X509Certificate cert) {
		try {
			return HashUtil.getSha1(new ByteArrayInputStream(cert.getEncoded()));
		}
		return null;
	}
		try {
			return HashUtil.getSha1(new ByteArrayInputStream(cert.getEncoded()));
		}
		return null;
	}
	public static String getKeyThumbprint(java.security.Key key) {
		return HashUtil.getSha1(new ByteArrayInputStream(key.getEncoded()));
	}
		try {
			return HashUtil.getSha1(new ByteArrayInputStream(cert.getEncoded()));
		}
		return null;
	}
New to GrepCode? Check out our FAQ X