From 12162de22f66e5905dfafbcdec1f6d32c38b3575 Mon Sep 17 00:00:00 2001 From: Gravit Date: Fri, 12 Jul 2019 20:11:55 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20CertificateManager=20=D1=84=D1=83?= =?UTF-8?q?=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20=D1=81=20Reader/Writer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manangers/CertificateManager.java | 42 ++++++++++++++----- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/CertificateManager.java b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/CertificateManager.java index f9c9bbd1..e5b05545 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/CertificateManager.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/CertificateManager.java @@ -25,6 +25,8 @@ import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.Reader; +import java.io.Writer; import java.math.BigInteger; import java.nio.file.Path; import java.security.*; @@ -105,29 +107,45 @@ public KeyPair generateKeyPair() throws InvalidAlgorithmParameterException, NoSu } public void writePrivateKey(Path file, PrivateKey privateKey) throws IOException { - try (PemWriter writer = new PemWriter(IOHelper.newWriter(file))) { - writer.writeObject(new PemObject("PRIVATE KEY", privateKey.getEncoded())); + writePrivateKey(IOHelper.newWriter(file), privateKey); + } + + 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 { + writePrivateKey(IOHelper.newWriter(file), key); + } + + public void writePrivateKey(Writer writer, AsymmetricKeyParameter key) throws IOException { PrivateKeyInfo info = PrivateKeyInfoFactory.createPrivateKeyInfo(key); - try (PemWriter writer = new PemWriter(IOHelper.newWriter(file))) { - writer.writeObject(new PemObject("PRIVATE KEY", info.getEncoded())); + try (PemWriter writer1 = new PemWriter(writer)) { + writer1.writeObject(new PemObject("PRIVATE KEY", info.getEncoded())); } } public void writeCertificate(Path file, X509CertificateHolder holder) throws IOException { - try (PemWriter writer = new PemWriter(IOHelper.newWriter(file))) { - writer.writeObject(new PemObject("CERTIFICATE", holder.toASN1Structure().getEncoded())); + writeCertificate(IOHelper.newWriter(file), holder); + } + + 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 { + return readPrivateKey(IOHelper.newReader(file)); + } + + public AsymmetricKeyParameter readPrivateKey(Reader reader) throws IOException { 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)) { @@ -138,10 +156,14 @@ public AsymmetricKeyParameter readPrivateKey(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; - 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); } return ret;