get Test Certificate - Java Security

Java examples for Security:Certificate

Description

get Test Certificate

Demo Code

/*/*ww  w  .  ja  v  a2  s  . c o m*/
 *  Copyright (c) 2012-2015 VMware, Inc.  All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not
 *  use this file except in compliance with the License.  You may obtain a copy
 *  of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, without
 *  warranties or conditions of any kind, EITHER EXPRESS OR IMPLIED.  See the
 *  License for the specific language governing permissions and limitations
 *  under the License.
 */
//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

public class Main {

    private static final String TEST_DATA_LOC = "src/test/resources/test_cert.pem";

    public static X509Certificate getTestCertificate()
            throws CertificateException, IOException {
        CertificateFactory certFactory = CertificateFactory
                .getInstance("X.509");
        InputStream inputStream = new ByteArrayInputStream(getTestPEM()
                .getBytes());
        return (X509Certificate) certFactory
                .generateCertificate(inputStream);
    }

    public static String getTestPEM() throws IOException {
        byte[] encoded = Files.readAllBytes(Paths.get(TEST_DATA_LOC));
        return new String(encoded, Charset.defaultCharset());
    }
}

Related Tutorials