get Principals from Certificate - Java Security

Java examples for Security:Certificate

Description

get Principals from Certificate

Demo Code


import org.apache.log4j.Logger;
import javax.security.auth.x500.X500Principal;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.PublicKey;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class Main{
    public static void main(String[] argv) throws Exception{
        List chain = java.util.Arrays.asList("asdf","java2s.com");
        System.out.println(java.util.Arrays.toString(getPrincipals(chain)));
    }//from w w w. j a va2s .  c  o m
    public static X500Principal[] getPrincipals(List<X509Certificate> chain) {
        if (chain.contains(null))
            throw new IllegalArgumentException(
                    "Certificate chain contains null!");

        X500Principal[] array = new X500Principal[chain.size()];

        for (int i = 0; i < array.length; i++)
            array[i] = chain.get(i).getSubjectX500Principal();

        return array;
    }
}

Related Tutorials