java 把 PEM 格式的公鑰證書轉換爲 X.509 格式的證書

 

代碼:spa


@UtilityClass
public final class X509Certs {

  private static final CertificateFactory CERTIFICATE_FACTORY;

  static {
    try {
      CERTIFICATE_FACTORY = CertificateFactory.getInstance(CertificateType.X509);
    } catch (CertificateException e) {
      throw Throwables.propagate(e);
    }
  }

  @SneakyThrows
  static PublicKey getVerifyPublicKey(String signPubKeyCert) {
    Preconditions.checkArgument(StringUtils.isNotBlank(signPubKeyCert), "The response message doesn't contains the [signPubKeyCert]");
    @Cleanup InputStream stream = new ByteArrayInputStream(signPubKeyCert.getBytes(StandardCharsets.UTF_8));
    X509Certificate cert = (X509Certificate) CERTIFICATE_FACTORY.generateCertificate(stream); return cert.getPublicKey();
  }

}

 

public interface CertificateType {

  String X509 = "X.509";

}
相關文章
相關標籤/搜索