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

utils.CertificateUtils Maven / Gradle / Ivy

There is a newer version: 1.3.9
Show newest version
package utils;

import java.io.ByteArrayInputStream;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Base64;

public class CertificateUtils {
    private static final String X509_PADDING_REGEX = "[\\r\\n]?-{5}(BEGIN|END) CERTIFICATE-{5}[\\r\\n]?";

    public static X509Certificate parseX509Certificate(String certString) throws CertificateException {
        certString = certString.replaceAll(X509_PADDING_REGEX, "").replaceAll("\n", "");
        CertificateFactory f = CertificateFactory.getInstance("X.509");
        byte[] bytes = Base64.getDecoder().decode(certString);
        ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
        return (X509Certificate) f.generateCertificate(stream);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy