[FEATURE] CertificateManager функции с Reader/Writer

This commit is contained in:
Gravit 2019-07-12 20:11:55 +07:00
parent 6f77c5720f
commit 12162de22f

View file

@ -25,6 +25,8 @@
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.file.Path; import java.nio.file.Path;
import java.security.*; import java.security.*;
@ -105,29 +107,45 @@ public KeyPair generateKeyPair() throws InvalidAlgorithmParameterException, NoSu
} }
public void writePrivateKey(Path file, PrivateKey privateKey) throws IOException { public void writePrivateKey(Path file, PrivateKey privateKey) throws IOException {
try (PemWriter writer = new PemWriter(IOHelper.newWriter(file))) { writePrivateKey(IOHelper.newWriter(file), privateKey);
writer.writeObject(new PemObject("PRIVATE KEY", privateKey.getEncoded())); }
public void writePrivateKey(Writer writer, PrivateKey privateKey) throws IOException {
try (PemWriter writer1 = new PemWriter(writer)) {
writer1.writeObject(new PemObject("PRIVATE KEY", privateKey.getEncoded()));
} }
} }
public void writePrivateKey(Path file, AsymmetricKeyParameter key) throws IOException { public void writePrivateKey(Path file, AsymmetricKeyParameter key) throws IOException {
writePrivateKey(IOHelper.newWriter(file), key);
}
public void writePrivateKey(Writer writer, AsymmetricKeyParameter key) throws IOException {
PrivateKeyInfo info = PrivateKeyInfoFactory.createPrivateKeyInfo(key); PrivateKeyInfo info = PrivateKeyInfoFactory.createPrivateKeyInfo(key);
try (PemWriter writer = new PemWriter(IOHelper.newWriter(file))) { try (PemWriter writer1 = new PemWriter(writer)) {
writer.writeObject(new PemObject("PRIVATE KEY", info.getEncoded())); writer1.writeObject(new PemObject("PRIVATE KEY", info.getEncoded()));
} }
} }
public void writeCertificate(Path file, X509CertificateHolder holder) throws IOException { public void writeCertificate(Path file, X509CertificateHolder holder) throws IOException {
try (PemWriter writer = new PemWriter(IOHelper.newWriter(file))) { writeCertificate(IOHelper.newWriter(file), holder);
writer.writeObject(new PemObject("CERTIFICATE", holder.toASN1Structure().getEncoded())); }
public void writeCertificate(Writer writer, X509CertificateHolder holder) throws IOException {
try (PemWriter writer1 = new PemWriter(writer)) {
writer1.writeObject(new PemObject("CERTIFICATE", holder.toASN1Structure().getEncoded()));
} }
} }
public AsymmetricKeyParameter readPrivateKey(Path file) throws IOException { public AsymmetricKeyParameter readPrivateKey(Path file) throws IOException {
return readPrivateKey(IOHelper.newReader(file));
}
public AsymmetricKeyParameter readPrivateKey(Reader reader) throws IOException {
AsymmetricKeyParameter ret; AsymmetricKeyParameter ret;
try(PemReader reader = new PemReader(IOHelper.newReader(file))) try(PemReader reader1 = new PemReader(reader))
{ {
byte[] bytes = reader.readPemObject().getContent(); byte[] bytes = reader1.readPemObject().getContent();
try(ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) try(ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes))
{ {
@ -138,10 +156,14 @@ public AsymmetricKeyParameter readPrivateKey(Path file) throws IOException {
} }
public X509CertificateHolder readCertificate(Path file) throws IOException { public X509CertificateHolder readCertificate(Path file) throws IOException {
return readCertificate(IOHelper.newReader(file));
}
public X509CertificateHolder readCertificate(Reader reader) throws IOException {
X509CertificateHolder ret; X509CertificateHolder ret;
try(PemReader reader = new PemReader(IOHelper.newReader(file))) try(PemReader reader1 = new PemReader(reader))
{ {
byte[] bytes = reader.readPemObject().getContent(); byte[] bytes = reader1.readPemObject().getContent();
ret = new X509CertificateHolder(bytes); ret = new X509CertificateHolder(bytes);
} }
return ret; return ret;