Example usage for java.security.cert X509Certificate getEncoded

List of usage examples for java.security.cert X509Certificate getEncoded

Introduction

In this page you can find the example usage for java.security.cert X509Certificate getEncoded.

Prototype

public abstract byte[] getEncoded() throws CertificateEncodingException;

Source Link

Document

Returns the encoded form of this certificate.

Usage

From source file:Main.java

public static String serializeCert(X509Certificate cert) throws CertificateEncodingException {
    StringBuffer buffer = new StringBuffer();
    buffer.append("-----BEGIN CERTIFICATE-----\n");
    buffer.append(Base64.encodeToString(cert.getEncoded(), Base64.DEFAULT));
    buffer.append("\n-----END CERTIFICATE-----");
    return buffer.toString();
}

From source file:Main.java

private static String getCertificateSHA1(X509Certificate certificate)
        throws NoSuchAlgorithmException, CertificateEncodingException {
    MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
    byte[] der = certificate.getEncoded();
    messageDigest.update(der);/*  w  w  w  .  ja v a  2 s .  c o  m*/
    byte[] digest = messageDigest.digest();
    return hexify(digest);
}

From source file:me.figo.internal.FigoTrustManager.java

private static String getThumbPrint(X509Certificate cert) {
    try {/*w ww.  j  a  v  a 2 s  .c  o m*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        byte[] der = cert.getEncoded();
        md.update(der);
        byte[] digest = md.digest();
        return new String(Hex.encodeHex(digest, false));
    } catch (NoSuchAlgorithmException e) {
        return "";
    } catch (CertificateEncodingException e) {
        return "";
    }
}

From source file:Main.java

private static byte[] getHash(@NonNull X509Certificate certificate)
        throws NoSuchAlgorithmException, CertificateEncodingException {
    MessageDigest digest = java.security.MessageDigest.getInstance("SHA1");
    digest.update(certificate.getEncoded());
    return digest.digest();
}

From source file:org.springframework.security.saml.trust.UntrustedCertificateException.java

private static void appendThumbPrint(X509Certificate cert, String hash, StringBuilder sb) {
    try {/*from w ww. j  a v a2  s  .co  m*/
        MessageDigest md = MessageDigest.getInstance(hash);
        byte[] der = cert.getEncoded();
        md.update(der);
        byte[] digest = md.digest();
        char[] encode = Hex.encodeHex(digest);
        appendHexSpace(encode, sb);
    } catch (NoSuchAlgorithmException e) {
        sb.append("Error calculating thumbprint: " + e.getMessage());
    } catch (CertificateEncodingException e) {
        sb.append("Error calculating thumbprint: " + e.getMessage());
    }
}

From source file:org.apache.ws.security.transform.STRTransformUtil.java

protected static Element createBSTX509(Document doc, X509Certificate cert, Element secRefE)
        throws WSSecurityException {
    byte data[];/*  w  ww  . j  av a 2 s. c  o m*/
    try {
        data = cert.getEncoded();
    } catch (CertificateEncodingException e) {
        throw new WSSecurityException(WSSecurityException.SECURITY_TOKEN_UNAVAILABLE, "encodeError", null, e);
    }
    String prefix = WSSecurityUtil.getPrefixNS(WSConstants.WSSE_NS, secRefE);
    Element elem = doc.createElementNS(WSConstants.WSSE_NS, prefix + ":BinarySecurityToken");
    WSSecurityUtil.setNamespace(elem, WSConstants.WSSE_NS, prefix);
    // elem.setAttributeNS(WSConstants.XMLNS_NS, "xmlns", "");
    elem.setAttributeNS(null, "ValueType", X509Security.X509_V3_TYPE);
    Text certText = doc.createTextNode(Base64.encode(data)); // no line wrap
    elem.appendChild(certText);
    return elem;
}

From source file:com.pieframework.runtime.utils.CertificateUtils.java

public static String getThumbPrint(X509Certificate certificate) {

    String thumbPrint = "";
    try {/*from ww  w.j  a va2 s. com*/

        MessageDigest md = MessageDigest.getInstance("SHA-1");
        byte[] der = certificate.getEncoded();
        md.update(der);
        byte[] digest = md.digest();

        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

        StringBuffer buf = new StringBuffer(digest.length * 2);

        for (int i = 0; i < digest.length; ++i) {
            buf.append(hexDigits[(digest[i] & 0xf0) >> 4]);
            buf.append(hexDigits[digest[i] & 0x0f]);
        }

        thumbPrint = buf.toString();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return thumbPrint;
}

From source file:org.springframework.security.saml.trust.UntrustedCertificateException.java

private static void appendCertificate(X509Certificate x509Certificate, StringBuilder sb) {
    sb.append("-----BEGIN CERTIFICATE-----\n");
    try {//from w w  w.  ja  v a  2s .c  o m
        String certificate = new String(Base64.encodeBase64(x509Certificate.getEncoded()));
        int i = 0;
        while (true) {
            int j = i + 76;
            if (j < certificate.length()) {
                sb.append(certificate.substring(i, j)).append("\n");
                i = j;
            } else {
                sb.append(certificate.substring(i)).append("\n");
                break;
            }
        }
    } catch (CertificateEncodingException e) {
        sb.append("Cannot encode: ").append(e.getMessage());
    }
    sb.append("-----END CERTIFICATE-----\n");
}

From source file:com.gsma.iariauth.validator.util.IARIValidatorMain.java

private static String getFingerprint(X509Certificate x509Cert) throws CertificateEncodingException {
    try {//from w ww.  j  a va 2s.  com
        byte[] digest = MessageDigest.getInstance(FINGERPRINT_DIGEST_ALG).digest(x509Cert.getEncoded());
        StringBuffer result = new StringBuffer();
        int len = digest.length - 1;
        for (int i = 0; i < len; i++) {
            result.append(byte2hex(digest[i]));
            result.append(':');
        }
        result.append(byte2hex(digest[len]));
        return result.toString();
    } catch (NoSuchAlgorithmException e) {
        return null;
        /* should not be possible */ }
}

From source file:org.ourgrid.common.util.SelfSignedCertificateGenerator.java

public static void generateX509Certificate(KeyPair keyPair, String dnData, String certFilePath)
        throws CertificateEncodingException, InvalidKeyException, IllegalStateException,
        NoSuchAlgorithmException, SignatureException, IOException {

    X509V3CertificateGenerator certGenerator = new X509V3CertificateGenerator();

    certGenerator.setSerialNumber(BigInteger.valueOf(1));
    certGenerator.setPublicKey(keyPair.getPublic());
    certGenerator.setSubjectDN(new X500Principal(dnData));
    certGenerator.setIssuerDN(new X500Principal(dnData));
    certGenerator.setNotBefore(new Date(System.currentTimeMillis() - VALIDITY_INTERVAL));
    certGenerator.setNotAfter(new Date(System.currentTimeMillis() + VALIDITY_INTERVAL));
    certGenerator.setSignatureAlgorithm(SignatureConstants.SIGN_ALGORITHM);

    X509Certificate certificate = certGenerator.generate(keyPair.getPrivate());

    File file = new File(certFilePath);
    if (!file.exists()) {
        FileUtils.touch(file);/*from w  ww  .  ja  va 2s  . c om*/
    }

    FileOutputStream fosP = new FileOutputStream(file);
    fosP.write(certificate.getEncoded());

    fosP.close();
}