All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cybervillains.ca.ThumbprintUtil Maven / Gradle / Ivy

There is a newer version: 4.0.0-alpha-2
Show newest version
package cybervillains.ca;

import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;

import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.util.encoders.Base64;

/**
 * Generates a persistent SHA1 thumbprint for long-term identification of a certificate.
 * 
 * ***************************************************************************************
 * Copyright (c) 2007, Information Security Partners, LLC
 * All rights reserved.
 * 
 * In a special exception, Selenium/OpenQA is allowed to use this code under the Apache License 2.0.
 * 
 * 
 * @author Brad Hill
 *
 */
public class ThumbprintUtil {

	/**
	 * Generates a SHA1 thumbprint of a certificate for long-term mapping.
	 * @param cert
	 * @return
	 * @throws CertificateEncodingException
	 */
	public static String getThumbprint(final X509Certificate cert) throws CertificateEncodingException {
		
		if(cert == null)
		{
			return null;
		}
		
		byte[] rawOctets = cert.getEncoded();
		
		SHA1Digest digest = new SHA1Digest();
		
		byte[] digestOctets = new byte[digest.getDigestSize()];
		
		digest.update(rawOctets, 0, rawOctets.length);
		
		digest.doFinal(digestOctets, 0);
		
		return new String(Base64.encode(digestOctets));
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy