@SuppressWarnings("JavaUtilDate")
  public static X509Certificate generateCertificate(String dn, KeyPair pair, int days,
    String algorithm) throws CertificateEncodingException, InvalidKeyException,
    IllegalStateException, NoSuchProviderException, NoSuchAlgorithmException, SignatureException {
    Date from = new Date();
    Date to = new Date(from.getTime() + days * 86400000L);
    BigInteger sn = new BigInteger(64, new SecureRandom());
    KeyPair keyPair = pair;
    X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
    X500Principal dnName = new X500Principal(dn);

    certGen.setSerialNumber(sn);
    certGen.setIssuerDN(dnName);
    certGen.setNotBefore(from);
    certGen.setNotAfter(to);
    certGen.setSubjectDN(dnName);
    certGen.setPublicKey(keyPair.getPublic());
    certGen.setSignatureAlgorithm(algorithm);
    X509Certificate cert = certGen.generate(pair.getPrivate());
    return cert;
  }
Create a self-signed X.509 Certificate.
@param dn        the X.509 Distinguished Name, eg "CN=Test, L=London, C=GB"
@param pair      the KeyPair
@param days      how many days from now the Certificate is valid for
@param algorithm the signing algorithm, eg "SHA1withRSA"
@return the self-signed certificate
hbase-http/src/test/java/org/apache/hadoop/hbase/http/ssl/KeyStoreTestUtil.java