fixed rsa private key -> pem issue

This commit is contained in:
Adrian Cole 2010-09-02 01:32:16 -07:00
parent be23e73207
commit fa8386b357
3 changed files with 98 additions and 68 deletions

View File

@ -58,7 +58,13 @@
<artifactId>oauth</artifactId> <artifactId>oauth</artifactId>
<version>20100527</version> <version>20100527</version>
</dependency> </dependency>
<!-- only required for Pems.java and only writing a private key file -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15</artifactId>
<version>1.44</version>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>aopalliance</groupId> <groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId> <artifactId>aopalliance</artifactId>

View File

@ -31,6 +31,8 @@ import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory; import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.KeySpec; import java.security.spec.KeySpec;
import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPrivateKeySpec; import java.security.spec.RSAPrivateKeySpec;
@ -43,12 +45,16 @@ import net.oauth.signature.pem.PEMReader;
import net.oauth.signature.pem.PKCS1EncodedKeySpec; import net.oauth.signature.pem.PKCS1EncodedKeySpec;
import net.oauth.signature.pem.PKCS1EncodedPublicKeySpec; import net.oauth.signature.pem.PKCS1EncodedPublicKeySpec;
import org.bouncycastle.asn1.ASN1OutputStream;
import org.bouncycastle.asn1.pkcs.RSAPrivateKeyStructure;
import org.jclouds.crypto.Pems.PemProcessor.ResultParser; import org.jclouds.crypto.Pems.PemProcessor.ResultParser;
import org.jclouds.io.InputSuppliers; import org.jclouds.io.InputSuppliers;
import com.google.common.annotations.Beta; import com.google.common.annotations.Beta;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteStreams;
import com.google.common.io.InputSupplier; import com.google.common.io.InputSupplier;
/** /**
@ -89,7 +95,7 @@ public class Pems {
return parsers.get(reader.getBeginMarker()).parseResult(bytes); return parsers.get(reader.getBeginMarker()).parseResult(bytes);
} else { } else {
throw new IOException(String.format("Invalid PEM file: no parsers for marker %s in %s", reader throw new IOException(String.format("Invalid PEM file: no parsers for marker %s in %s", reader
.getBeginMarker(), parsers.keySet())); .getBeginMarker(), parsers.keySet()));
} }
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -98,8 +104,7 @@ public class Pems {
} }
/** /**
* Returns the object of generic type {@code T} that is pem encoded in the * Returns the object of generic type {@code T} that is pem encoded in the supplier.
* supplier.
* *
* @param supplier * @param supplier
* the input stream factory * the input stream factory
@ -107,13 +112,12 @@ public class Pems {
* header that begins the PEM block * header that begins the PEM block
* @param processor * @param processor
* how to parser the object from a byte array * how to parser the object from a byte array
* @return the object of generic type {@code T} which was PEM encoded in the * @return the object of generic type {@code T} which was PEM encoded in the stream
* stream
* @throws IOException * @throws IOException
* if an I/O error occurs * if an I/O error occurs
*/ */
public static <T> T fromPem(InputSupplier<? extends InputStream> supplier, PemProcessor<T> processor) public static <T> T fromPem(InputSupplier<? extends InputStream> supplier, PemProcessor<T> processor)
throws IOException { throws IOException {
try { try {
return com.google.common.io.ByteStreams.readBytes(supplier, processor); return com.google.common.io.ByteStreams.readBytes(supplier, processor);
} catch (RuntimeException e) { } catch (RuntimeException e) {
@ -136,24 +140,24 @@ public class Pems {
*/ */
public static KeySpec privateKeySpec(InputSupplier<? extends InputStream> supplier) throws IOException { public static KeySpec privateKeySpec(InputSupplier<? extends InputStream> supplier) throws IOException {
return fromPem(supplier, new PemProcessor<KeySpec>(ImmutableMap.<String, ResultParser<KeySpec>> of( return fromPem(supplier, new PemProcessor<KeySpec>(ImmutableMap.<String, ResultParser<KeySpec>> of(
PRIVATE_PKCS1_MARKER, new ResultParser<KeySpec>() { PRIVATE_PKCS1_MARKER, new ResultParser<KeySpec>() {
public KeySpec parseResult(byte[] bytes) throws IOException { public KeySpec parseResult(byte[] bytes) throws IOException {
return (new PKCS1EncodedKeySpec(bytes)).getKeySpec(); return (new PKCS1EncodedKeySpec(bytes)).getKeySpec();
} }
}, PRIVATE_PKCS8_MARKER, new ResultParser<KeySpec>() { }, PRIVATE_PKCS8_MARKER, new ResultParser<KeySpec>() {
public KeySpec parseResult(byte[] bytes) throws IOException { public KeySpec parseResult(byte[] bytes) throws IOException {
return new PKCS8EncodedKeySpec(bytes); return new PKCS8EncodedKeySpec(bytes);
} }
}))); })));
} }
/** /**
* Executes {@link Pems#privateKeySpec(InputSupplier)} on the string which * Executes {@link Pems#privateKeySpec(InputSupplier)} on the string which contains an encoded
* contains an encoded private key in PEM format. * private key in PEM format.
* *
* @param pem * @param pem
* private key in pem encoded format. * private key in pem encoded format.
@ -175,24 +179,24 @@ public class Pems {
*/ */
public static KeySpec publicKeySpec(InputSupplier<? extends InputStream> supplier) throws IOException { public static KeySpec publicKeySpec(InputSupplier<? extends InputStream> supplier) throws IOException {
return fromPem(supplier, new PemProcessor<KeySpec>(ImmutableMap.<String, ResultParser<KeySpec>> of( return fromPem(supplier, new PemProcessor<KeySpec>(ImmutableMap.<String, ResultParser<KeySpec>> of(
PUBLIC_PKCS1_MARKER, new ResultParser<KeySpec>() { PUBLIC_PKCS1_MARKER, new ResultParser<KeySpec>() {
public KeySpec parseResult(byte[] bytes) throws IOException { public KeySpec parseResult(byte[] bytes) throws IOException {
return (new PKCS1EncodedPublicKeySpec(bytes)).getKeySpec(); return (new PKCS1EncodedPublicKeySpec(bytes)).getKeySpec();
} }
}, PUBLIC_X509_MARKER, new ResultParser<KeySpec>() { }, PUBLIC_X509_MARKER, new ResultParser<KeySpec>() {
public X509EncodedKeySpec parseResult(byte[] bytes) throws IOException { public X509EncodedKeySpec parseResult(byte[] bytes) throws IOException {
return new X509EncodedKeySpec(bytes); return new X509EncodedKeySpec(bytes);
} }
}))); })));
} }
/** /**
* Executes {@link Pems#publicKeySpec(InputSupplier)} on the string which * Executes {@link Pems#publicKeySpec(InputSupplier)} on the string which contains an encoded
* contains an encoded public key in PEM format. * public key in PEM format.
* *
* @param pem * @param pem
* public key in pem encoded format. * public key in pem encoded format.
@ -203,8 +207,7 @@ public class Pems {
} }
/** /**
* Returns the {@link X509EncodedKeySpec} that is pem encoded in the * Returns the {@link X509EncodedKeySpec} that is pem encoded in the supplier.
* supplier.
* *
* @param supplier * @param supplier
* the input stream factory * the input stream factory
@ -217,24 +220,24 @@ public class Pems {
* @throws CertificateException * @throws CertificateException
*/ */
public static X509Certificate x509Certificate(InputSupplier<? extends InputStream> supplier, public static X509Certificate x509Certificate(InputSupplier<? extends InputStream> supplier,
@Nullable CertificateFactory certFactory) throws IOException, CertificateException { @Nullable CertificateFactory certFactory) throws IOException, CertificateException {
final CertificateFactory finalCertFactory = certFactory != null ? certFactory : CertificateFactory final CertificateFactory finalCertFactory = certFactory != null ? certFactory : CertificateFactory
.getInstance("X.509"); .getInstance("X.509");
try { try {
return fromPem(supplier, new PemProcessor<X509Certificate>(ImmutableMap return fromPem(supplier, new PemProcessor<X509Certificate>(ImmutableMap
.<String, ResultParser<X509Certificate>> of(CERTIFICATE_X509_MARKER, .<String, ResultParser<X509Certificate>> of(CERTIFICATE_X509_MARKER,
new ResultParser<X509Certificate>() { new ResultParser<X509Certificate>() {
public X509Certificate parseResult(byte[] bytes) throws IOException { public X509Certificate parseResult(byte[] bytes) throws IOException {
try { try {
return (X509Certificate) finalCertFactory.generateCertificate(new ByteArrayInputStream( return (X509Certificate) finalCertFactory
bytes)); .generateCertificate(new ByteArrayInputStream(bytes));
} catch (CertificateException e) { } catch (CertificateException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
}))); })));
} catch (RuntimeException e) { } catch (RuntimeException e) {
if (e.getCause() != null && e.getCause() instanceof CertificateException) { if (e.getCause() != null && e.getCause() instanceof CertificateException) {
throw (CertificateException) e.getCause(); throw (CertificateException) e.getCause();
@ -244,8 +247,8 @@ public class Pems {
} }
/** /**
* Executes {@link Pems#x509Certificate(InputSupplier, CertificateFactory)} * Executes {@link Pems#x509Certificate(InputSupplier, CertificateFactory)} on the string which
* on the string which contains an X.509 certificate in PEM format. * contains an X.509 certificate in PEM format.
* *
* @param pem * @param pem
* certificate in pem encoded format. * certificate in pem encoded format.
@ -265,9 +268,8 @@ public class Pems {
* @throws CertificateEncodingException * @throws CertificateEncodingException
*/ */
public static String pem(X509Certificate cert) throws IOException, CertificateEncodingException { public static String pem(X509Certificate cert) throws IOException, CertificateEncodingException {
return new StringBuilder("-----BEGIN CERTIFICATE-----\n").append( String marker = CERTIFICATE_X509_MARKER;
CryptoStreams.base64Encode(ByteStreams.newInputStreamSupplier(cert.getEncoded()))).append( return pem(cert.getEncoded(), marker);
"\n-----END CERTIFICATE-----\n").toString();
} }
/** /**
@ -280,9 +282,8 @@ public class Pems {
* @throws CertificateEncodingException * @throws CertificateEncodingException
*/ */
public static String pem(PublicKey key) throws IOException { public static String pem(PublicKey key) throws IOException {
return new StringBuilder("-----BEGIN PUBLIC KEY-----\n").append( String marker = key instanceof RSAPublicKey ? PUBLIC_PKCS1_MARKER : PUBLIC_X509_MARKER;
CryptoStreams.base64Encode(ByteStreams.newInputStreamSupplier(key.getEncoded()))).append( return pem(key.getEncoded(), marker);
"\n-----END PUBLIC KEY-----\n").toString();
} }
/** /**
@ -295,9 +296,34 @@ public class Pems {
* @throws CertificateEncodingException * @throws CertificateEncodingException
*/ */
public static String pem(PrivateKey key) throws IOException { public static String pem(PrivateKey key) throws IOException {
return new StringBuilder("-----BEGIN PRIVATE KEY-----\n").append( String marker = key instanceof RSAPrivateCrtKey ? PRIVATE_PKCS1_MARKER : PRIVATE_PKCS8_MARKER;
CryptoStreams.base64Encode(ByteStreams.newInputStreamSupplier(key.getEncoded()))).append( return pem(key instanceof RSAPrivateCrtKey ? getEncoded(RSAPrivateCrtKey.class.cast(key)) : key.getEncoded(),
"\n-----END PRIVATE KEY-----\n").toString(); marker);
}
// TODO find a way to do this without using bouncycastle
static byte[] getEncoded(RSAPrivateCrtKey key) {
RSAPrivateKeyStructure keyStruct = new RSAPrivateKeyStructure(key.getModulus(), key.getPublicExponent(), key
.getPrivateExponent(), key.getPrimeP(), key.getPrimeQ(), key.getPrimeExponentP(), key
.getPrimeExponentQ(), key.getCrtCoefficient());
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ASN1OutputStream aOut = new ASN1OutputStream(bOut);
try {
aOut.writeObject(keyStruct);
aOut.close();
} catch (IOException e) {
Throwables.propagate(e);
}
return bOut.toByteArray();
}
private static String pem(byte[] key, String marker) throws IOException {
return new StringBuilder(marker + "\n").append(
Joiner.on('\n').join(Splitter.fixedLength(64).split(CryptoStreams.base64(key)))).append(
"\n" + marker.replace("BEGIN", "END") + "\n").toString().trim();
} }
} }

View File

@ -26,6 +26,7 @@ import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory; import java.security.cert.CertificateFactory;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.spec.InvalidKeySpecException; import java.security.spec.InvalidKeySpecException;
import org.jclouds.io.Payloads; import org.jclouds.io.Payloads;
@ -40,7 +41,7 @@ public class PemsTest {
public static final String PRIVATE_KEY = "-----BEGIN RSA PRIVATE KEY-----\nMIIEpQIBAAKCAQEAyb2ZJJqGm0KKR+8nfQJNsSd+F9tXNMV7CfOcW6jsqs8EZgiV\nR09hD1IYOj4YqM0qJONlgyg4xRWewdSG7QTPj1lJpVAida9sXy2+kzyagZA1Am0O\nZcbqb5hoeIDgcX+eDa79s0u0DomjcfO9EKhvHLBz+zM+3QqPRkPV8nYTbfs+HjVz\nzOU6D1B0XR3+IPZZl2AnWs2d0qhnStHcDUvnRVQ0P482YwN9VgceOZt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iE2FW00i8+rIPRu4Z3Q5nybx7w3PzV9\noHN5R5baE9OyI4KpZWztpYYitZF67NcnAvVULHHOvVJQGnKYfLHJYmrJF7GA1ojM\nAuMdFbjFAoGAPxUhxwFy8gaqBahKUEZn4F81HFP5ihGhkT4QL6AFPO2e+JhIGjuR\n27+85hcFqQ+HHVtFsm81b/a+R7P4UuCRgc8eCjxQMoJ1Xl4n7VbjPbHMnIN0Ryvd\nO4ZpWDWYnCO021JTOUUOJ4J/y0416Bvkw0z59y7sNX7wDBBHHbK/XCc=\n-----END RSA PRIVATE KEY-----\n"; public static final String PRIVATE_KEY = "-----BEGIN RSA PRIVATE KEY-----\nMIIEpQIBAAKCAQEAyb2ZJJqGm0KKR+8nfQJNsSd+F9tXNMV7CfOcW6jsqs8EZgiV\nR09hD1IYOj4YqM0qJONlgyg4xRWewdSG7QTPj1lJpVAida9sXy2+kzyagZA1Am0O\nZcbqb5hoeIDgcX+eDa79s0u0DomjcfO9EKhvHLBz+zM+3QqPRkPV8nYTbfs+HjVz\nzOU6D1B0XR3+IPZZl2AnWs2d0qhnStHcDUvnRVQ0P482YwN9VgceOZtpPz0DCKEJ\n5Tx5STub8k0/zt/VAMHQafLSuQMLd2s4ZLuOZptN//uAsTmxireqd37z+8ZTdBbJ\n8LEpJ+iCXuSfm5aUh7iw6oxvToY2AL53+jK2UQIDAQABAoIBAQDA88B3i/xWn0vX\nBVxFamCYoecuNjGwXXkSyZew616A+EOCu47bh4aTurdFbYL0YFaAtaWvzlaN2eHg\nDb+HDuTefE29+WkcGk6SshPmiz5T0XOCAICWw6wSVDkHmGwS4jZvbAFm7W8nwGk9\nYhxgxFiRngswJZFopOLoF5WXs2td8guIYNslMpo7tu50iFnBHwKO2ZsPAk8t9nnS\nxlDavKruymEmqHCr3+dtio5eaenJcp3fjoXBQOKUk3ipII29XRB8NqeCVV/7Kxwq\nckqOBEbRwBclckyIbD+RiAgKvOelORjEiE9R42vuqvxRA6k9kd9o7utlX0AUtpEn\n3gZc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qM0qJONlgyg4xRWewdSG7QTPj1lJpVAida9sXy2+kzyagZA1Am0OZcbqb5hoeIDgcX+eDa79s0u0DomjcfO9EKhvHLBz+zM+3QqPRkPV8nYTbfs+HjVzzOU6D1B0XR3+IPZZl2AnWs2d0qhnStHcDUvnRVQ0P482YwN9VgceOZtpPz0DCKEJ5Tx5STub8k0/zt/VAMHQafLSuQMLd2s4ZLuOZptN//uAsTmxireqd37z+8ZTdBbJ8LEpJ+iCXuSfm5aUh7iw6oxvToY2AL53+jK2UQIDAQAB\n-----END PUBLIC KEY-----\n"; public static final String PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyb2ZJJqGm0KKR+8nfQJN\nsSd+F9tXNMV7CfOcW6jsqs8EZgiVR09hD1IYOj4YqM0qJONlgyg4xRWewdSG7QTP\nj1lJpVAida9sXy2+kzyagZA1Am0OZcbqb5hoeIDgcX+eDa79s0u0DomjcfO9EKhv\nHLBz+zM+3QqPRkPV8nYTbfs+HjVzzOU6D1B0XR3+IPZZl2AnWs2d0qhnStHcDUvn\nRVQ0P482YwN9VgceOZtpPz0DCKEJ5Tx5STub8k0/zt/VAMHQafLSuQMLd2s4ZLuO\nZptN//uAsTmxireqd37z+8ZTdBbJ8LEpJ+iCXuSfm5aUh7iw6oxvToY2AL53+jK2\nUQIDAQAB\n-----END PUBLIC KEY-----\n";
private static final String CERTIFICATE = "-----BEGIN CERTIFICATE-----\nMIIClzCCAgCgAwIBAgIBATANBgkqhkiG9w0BAQUFADCBnjELMAkGA1UEBhMCVVMx\nEzARBgNVBAgMCldhc2hpbmd0b24xEDAOBgNVBAcMB1NlYXR0bGUxFjAUBgNVBAoM\nDU9wc2NvZGUsIEluYy4xHDAaBgNVBAsME0NlcnRpZmljYXRlIFNlcnZpY2UxMjAw\nBgNVBAMMKW9wc2NvZGUuY29tL2VtYWlsQWRkcmVzcz1hdXRoQG9wc2NvZGUuY29t\nMB4XDTEwMDczMDIwNDEzMFoXDTIwMDcyNzIwNDEzMFowADCCASIwDQYJKoZIhvcN\nAQEBBQADggEPADCCAQoCggEBAMm9mSSahptCikfvJ30CTbEnfhfbVzTFewnznFuo\n7KrPBGYIlUdPYQ9SGDo+GKjNKiTjZYMoOMUVnsHUhu0Ez49ZSaVQInWvbF8tvpM8\nmoGQNQJtDmXG6m+YaHiA4HF/ng2u/bNLtA6Jo3HzvRCobxywc/szPt0Kj0ZD1fJ2\nE237Ph41c8zlOg9QdF0d/iD2WZdgJ1rNndKoZ0rR3A1L50VUND+PNmMDfVYHHjmb\naT89AwihCeU8eUk7m/JNP87f1QDB0Gny0rkDC3drOGS7jmabTf/7gLE5sYq3qnd+\n8/vGU3QWyfCxKSfogl7kn5uWlIe4sOqMb06GNgC+d/oytlECAwEAATANBgkqhkiG\n9w0BAQUFAAOBgQBftzSZxstWw60GqRTDNN/F2GnrdtnKBoXzHww3r6jtGEylYq20\n5KfKpEx+sPX0gyZuYJiXC2CkEjImAluWKcdN9ZF6VD541sheAjbiaU7q7ZsztTxF\nWUH2tCvHeDXYKPKek3QzL7bYpUhLnCN/XxEv6ibeMDwtI7f5qpk2Aspzcw==\n-----END CERTIFICATE-----\n"; private static final String CERTIFICATE = "-----BEGIN CERTIFICATE-----\nMIIClzCCAgCgAwIBAgIBATANBgkqhkiG9w0BAQUFADCBnjELMAkGA1UEBhMCVVMx\nEzARBgNVBAgMCldhc2hpbmd0b24xEDAOBgNVBAcMB1NlYXR0bGUxFjAUBgNVBAoM\nDU9wc2NvZGUsIEluYy4xHDAaBgNVBAsME0NlcnRpZmljYXRlIFNlcnZpY2UxMjAw\nBgNVBAMMKW9wc2NvZGUuY29tL2VtYWlsQWRkcmVzcz1hdXRoQG9wc2NvZGUuY29t\nMB4XDTEwMDczMDIwNDEzMFoXDTIwMDcyNzIwNDEzMFowADCCASIwDQYJKoZIhvcN\nAQEBBQADggEPADCCAQoCggEBAMm9mSSahptCikfvJ30CTbEnfhfbVzTFewnznFuo\n7KrPBGYIlUdPYQ9SGDo+GKjNKiTjZYMoOMUVnsHUhu0Ez49ZSaVQInWvbF8tvpM8\nmoGQNQJtDmXG6m+YaHiA4HF/ng2u/bNLtA6Jo3HzvRCobxywc/szPt0Kj0ZD1fJ2\nE237Ph41c8zlOg9QdF0d/iD2WZdgJ1rNndKoZ0rR3A1L50VUND+PNmMDfVYHHjmb\naT89AwihCeU8eUk7m/JNP87f1QDB0Gny0rkDC3drOGS7jmabTf/7gLE5sYq3qnd+\n8/vGU3QWyfCxKSfogl7kn5uWlIe4sOqMb06GNgC+d/oytlECAwEAATANBgkqhkiG\n9w0BAQUFAAOBgQBftzSZxstWw60GqRTDNN/F2GnrdtnKBoXzHww3r6jtGEylYq20\n5KfKpEx+sPX0gyZuYJiXC2CkEjImAluWKcdN9ZF6VD541sheAjbiaU7q7ZsztTxF\nWUH2tCvHeDXYKPKek3QzL7bYpUhLnCN/XxEv6ibeMDwtI7f5qpk2Aspzcw==\n-----END CERTIFICATE-----\n";
@ -64,29 +65,26 @@ public class PemsTest {
Pems.x509Certificate(Payloads.newStringPayload(CERTIFICATE), CertificateFactory.getInstance("X.509")); Pems.x509Certificate(Payloads.newStringPayload(CERTIFICATE), CertificateFactory.getInstance("X.509"));
} }
@Test(enabled = false) @Test
// TODO figure out how to write back in the same format
public void testPrivateKeySpecPem() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException { public void testPrivateKeySpecPem() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
String encoded = Pems.pem(KeyFactory.getInstance("RSA").generatePrivate( RSAPrivateCrtKey key = (RSAPrivateCrtKey) KeyFactory.getInstance("RSA").generatePrivate(
Pems.privateKeySpec(Payloads.newStringPayload(PRIVATE_KEY)))); Pems.privateKeySpec(Payloads.newStringPayload(PRIVATE_KEY)));
assertEquals(encoded, PRIVATE_KEY.replaceAll("\n", "").replaceAll("Y-----", "Y-----\n").replaceAll("-----E", String encoded = Pems.pem(key);
"\n-----E")); assertEquals(encoded, PRIVATE_KEY.replaceAll("\n", "\n").trim());
} }
@Test @Test
public void testPublicKeySpecPem() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException { public void testRSAPublicKeySpecPem() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
String encoded = Pems.pem(KeyFactory.getInstance("RSA").generatePublic( String encoded = Pems.pem(KeyFactory.getInstance("RSA").generatePublic(
Pems.publicKeySpec(Payloads.newStringPayload(PUBLIC_KEY)))); Pems.publicKeySpec(Payloads.newStringPayload(PUBLIC_KEY))));
assertEquals(encoded, PUBLIC_KEY.replaceAll("\n", "").replaceAll("Y-----", "Y-----\n").replaceAll("-----E", assertEquals(encoded, PUBLIC_KEY.replaceAll("PUBLIC", "RSA PUBLIC").replaceAll("\n", "\n").trim());
"\n-----E"));
} }
@Test @Test
public void testX509CertificatePem() throws IOException, CertificateException { public void testX509CertificatePem() throws IOException, CertificateException {
String encoded = Pems.pem(Pems.x509Certificate(Payloads.newStringPayload(CERTIFICATE), CertificateFactory String encoded = Pems.pem(Pems.x509Certificate(Payloads.newStringPayload(CERTIFICATE), CertificateFactory
.getInstance("X.509"))); .getInstance("X.509")));
assertEquals(encoded, CERTIFICATE.replaceAll("\n", "").replaceAll("E-----", "E-----\n").replaceAll("-----E", assertEquals(encoded, CERTIFICATE.replaceAll("\n", "\n").trim());
"\n-----E"));
} }
} }