mirror of https://github.com/shred/acme4j
Add utility method to write public key
parent
5db82b1ad7
commit
22e8969b89
|
@ -21,6 +21,7 @@ import java.security.KeyPair;
|
||||||
import java.security.KeyPairGenerator;
|
import java.security.KeyPairGenerator;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.NoSuchProviderException;
|
import java.security.NoSuchProviderException;
|
||||||
|
import java.security.PublicKey;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
|
|
||||||
import org.bouncycastle.jce.ECNamedCurveTable;
|
import org.bouncycastle.jce.ECNamedCurveTable;
|
||||||
|
@ -123,4 +124,20 @@ public class KeyPairUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes a {@link PublicKey} as PEM file.
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* {@link PublicKey}
|
||||||
|
* @param w
|
||||||
|
* {@link Writer} to write the PEM file to. The {@link Writer} is closed
|
||||||
|
* after use.
|
||||||
|
* @since 3.0.0
|
||||||
|
*/
|
||||||
|
public static void writePublicKey(PublicKey key, Writer w) throws IOException {
|
||||||
|
try (var jw = new JcaPEMWriter(w)) {
|
||||||
|
jw.writeObject(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -140,6 +140,19 @@ public class KeyPairUtilsTest {
|
||||||
assertThat(pair).isNotSameAs(readPair);
|
assertThat(pair).isNotSameAs(readPair);
|
||||||
assertThat(pair.getPublic().getEncoded()).isEqualTo(readPair.getPublic().getEncoded());
|
assertThat(pair.getPublic().getEncoded()).isEqualTo(readPair.getPublic().getEncoded());
|
||||||
assertThat(pair.getPrivate().getEncoded()).isEqualTo(readPair.getPrivate().getEncoded());
|
assertThat(pair.getPrivate().getEncoded()).isEqualTo(readPair.getPrivate().getEncoded());
|
||||||
|
|
||||||
|
// Write Public Key
|
||||||
|
String publicPem;
|
||||||
|
try (var out = new StringWriter()) {
|
||||||
|
KeyPairUtils.writePublicKey(pair.getPublic(), out);
|
||||||
|
publicPem = out.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure PEM file is properly formatted
|
||||||
|
assertThat(publicPem).matches(
|
||||||
|
"-----BEGIN PUBLIC KEY-----[\\r\\n]+"
|
||||||
|
+ "([a-zA-Z0-9/+=]+[\\r\\n]+)+"
|
||||||
|
+ "-----END PUBLIC KEY-----[\\r\\n]*");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue