6

I have been provided a byte array and an X509 certificate. I can generate X509 certificate from the byte array using below code.

CertificateFactory certificatefactory = CertificateFactory.getInstance("X.509");
InputStream in = new ByteArrayInputStream(bytes);
X509Certificate cert = (X509Certificate)certificatefactory.generateCertificate(in);

Please guide me how to verify that the generated X509 with the one which was provided to me.

1
  • If you look up X.509 on Wikipedia they walk you through how to validate one.
    – azurefrog
    Commented Apr 13, 2015 at 18:36

2 Answers 2

8

Simply use certificate.equals(otherCertificate) : the equals() method will compare them correctly, with a specific implementation for each certificate subclass.

See the javadoc.

4

Do it the other way around. Turn the certificate into bytes (X509Certificate.getEncoded()) and just compare the bytes.

But the question is pointless. If the byte array is the input to the certificate factory that produces the certificate, they can't possibly be different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.