package org.primeframework.jwt.rsa;

/**
 * @author Daniel DeGroff
 */
public class PEMUtils {
  // PEM Encoded Certificate  Start Tag
  public static final String CERTIFICATE_PREFIX = "-----BEGIN CERTIFICATE-----";

  // PEM Encoded Certificate End Tag
  public static final String CERTIFICATE_SUFFIX = "-----END CERTIFICATE-----";

  // PEM Encoded RSA Private Key file (PKCS#1)  Start Tag
  public static final String PKCS_1_PRIVATE_KEY_PREFIX = "-----BEGIN RSA PRIVATE KEY-----";

  // PEM Encoded RSA Private Key file (PKCS#1) End Tag
  public static final String PKCS_1_PRIVATE_KEY_SUFFIX = "-----END RSA PRIVATE KEY-----";

  // RSA Public Key file (PKCS#1)  Start Tag
  public static final String PKCS_1_PUBLIC_KEY_PREFIX = "-----BEGIN RSA PUBLIC KEY-----";

  // RSA Public Key file (PKCS#1)  End Tag
  public static final String PKCS_1_PUBLIC_KEY_SUFFIX = "-----END RSA PUBLIC KEY-----";

  // PEM Encoded RSA Private Key file (PKCS#8)  Start Tag
  public static final String PKCS_8_PRIVATE_KEY_PREFIX = "-----BEGIN PRIVATE KEY-----";

  // PEM Encoded RSA Private Key file (PKCS#8)  End Tag
  public static final String PKCS_8_PRIVATE_KEY_SUFFIX = "-----END PRIVATE KEY-----";

  // PEM Encoded RSA Public Key file (X.509)  Start Tag
  public static final String PKCS_8_X509_PUBLIC_KEY_PREFIX = "-----BEGIN PUBLIC KEY-----";

  // PEM Encoded RSA Public Key file (X.509)  End Tag
  public static final String PKCS_8_X509_PUBLIC_KEY_SUFFIX = "-----END PUBLIC KEY-----";
}

@@ -1,38 +1,778 @@
-package org.primeframework.jwt.rsa
+/*%0A * Copyright (c) 2017, Inversoft Inc., All Rights Reserved%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *   http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing,%0A * software distributed under the License is distributed on an%0A * %22AS IS%22 BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,%0A * either express or implied. See the License for the specific%0A * language governing permissions and limitations under the License.%0A */%0A%0Apackage org.primeframework.jwt.rsa;%0A%0Aimport java.security.PrivateKey;%0Aimport java.security.PublicKey;%0Aimport java.util.Base64;%0Aimport java.util.Objects
 ;%0A%0A/
@@ -2212,11 +2212,1314 @@
 Y-----%22;
+%0A%0A  private static final Base64.Encoder PEM_ENCODER = Base64.getMimeEncoder(64, new byte%5B%5D%7B'%5Cn'%7D);%0A%0A  public static String encodePrivateKey(PrivateKey privateKey) %7B%0A    Objects.requireNonNull(privateKey);%0A    if (!privateKey.getAlgorithm().equals(%22RSA%22)) %7B%0A      throw new IllegalStateException(%22Only RSA keys are currently supported.%22);%0A    %7D%0A%0A    if (privateKey.getFormat().equals(%22PKCS#8%22)) %7B%0A      return PKCS_8_PRIVATE_KEY_PREFIX + %22%5Cn%22 +%0A          PEM_ENCODER.encodeToString(privateKey.getEncoded()) + %22%5Cn%22 +%0A          PKCS_8_PRIVATE_KEY_SUFFIX;%0A    %7D%0A%0A    throw new IllegalStateException(%22Only RSA PKCS#8 keys are currently supported. Provided key format %5B%22 + privateKey.getFormat() + %22%5D%22);%0A  %7D%0A%0A  public static String encodePublicKey(PublicKey publicKey) %7B%0A    Objects.requireNonNull(publicKey);%0A    if (!publicKey.getAlgorithm().equals(%22RSA%22)) %7B%0A      throw new IllegalStateException(%22Only RSA keys are currently supported.%22);%0A    %7D%0A%0A    if (publicKey.getFormat().equals(%22X.509%22)) %7B%0A      return PKCS_8_X509_PUBLIC_KEY_PREFIX + %22%5Cn%22 +%0A          PEM_ENCODER.encodeToString(publicKey.getEncoded()) + %22%5Cn%22 +%0A          PKCS_8_X509_PUBLIC_KEY_SUFFIX;%0A    %7D%0A%0A    throw new IllegalStateException(%22Only RSA PKCS#8 keys are currently supported. Provided key format %5B%22 + publicKey.getFormat() + %22%5D%22);%0A  %7D
 %0A%7D%0A

Add helper for PEM encoding