Java tutorial
//package com.java2s; import java.security.Key; import java.security.interfaces.DSAKey; import java.security.interfaces.ECKey; import java.security.interfaces.RSAKey; public class Main { /** * Creates a signature algorithm string using the specified message digest and the encryption type corresponding * to the supplied signingKey. Useful when generating the signature algorithm to be used to sign server certificates * using the CA root certificate's signingKey. * <p/> * For example, if the root certificate has an RSA private key, and you * wish to use the SHA256 message digest, this method will return the string "SHA256withRSA". See the * "Signature Algorithms" section of http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html * for a list of JSSE-supported signature algorithms. * * @param messageDigest digest to use to sign the certificate, such as SHA512 * @param signingKey private key that will be used to sign the certificate * @return a JCA-compatible signature algorithm */ public static String getSignatureAlgorithm(String messageDigest, Key signingKey) { return messageDigest + "with" + getDigitalSignatureType(signingKey); } /** * Returns the type of digital signature used with the specified signing key. * * @param signingKey private key that will be used to sign a certificate (or something else) * @return a string representing the digital signature type (ECDSA, RSA, etc.) */ public static String getDigitalSignatureType(Key signingKey) { if (signingKey instanceof ECKey) { return "ECDSA"; } else if (signingKey instanceof RSAKey) { return "RSA"; } else if (signingKey instanceof DSAKey) { return "DSA"; } else { throw new IllegalArgumentException( "Cannot determine digital signature encryption type for unknown key type: " + signingKey.getClass().getCanonicalName()); } } }