From faa6eebe5293a707f5edad59c2c7e015aee5fb85 Mon Sep 17 00:00:00 2001 From: Looly Date: Sun, 17 Jul 2022 21:52:55 +0800 Subject: [PATCH] add methods --- .../java/cn/hutool/crypto/OpensslKeyUtil.java | 78 ++++++++++++++----- 1 file changed, 60 insertions(+), 18 deletions(-) diff --git a/hutool-crypto/src/main/java/cn/hutool/crypto/OpensslKeyUtil.java b/hutool-crypto/src/main/java/cn/hutool/crypto/OpensslKeyUtil.java index 6748a79ea..c3fdc2b73 100644 --- a/hutool-crypto/src/main/java/cn/hutool/crypto/OpensslKeyUtil.java +++ b/hutool-crypto/src/main/java/cn/hutool/crypto/OpensslKeyUtil.java @@ -3,7 +3,6 @@ package cn.hutool.crypto; import cn.hutool.core.io.IORuntimeException; import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; - import org.bouncycastle.cert.X509CertificateHolder; import org.bouncycastle.openssl.PEMDecryptorProvider; import org.bouncycastle.openssl.PEMEncryptedKeyPair; @@ -84,23 +83,6 @@ public class OpensslKeyUtil { } } - /** - * 从pem文件中读取公钥或私钥
- * 根据类型返回 {@link PublicKey} 或者 {@link PrivateKey} - * - * @param keyStream pem 流 - * @param password 私钥密码 - * @return {@link Key},null 表示无法识别的密钥类型 - * @since 5.8.5 - */ - public static Key readPemKey(final InputStream keyStream, final char[] password) { - try (final PEMParser pemParser = new PEMParser(new InputStreamReader(keyStream))) { - return readPemKeyFromKeyObject(pemParser.readObject(), password); - } catch (final IOException e) { - throw new CryptoException(e); - } - } - /** * 解密{@link PKCS8EncryptedPrivateKeyInfo}为{@link PrivateKeyInfo} * @@ -137,6 +119,40 @@ public class OpensslKeyUtil { } } + /** + * 从pem文件中读取公钥或私钥
+ * 根据类型返回 {@link PublicKey} 或者 {@link PrivateKey} + * + * @param keyStream pem 流 + * @param password 私钥密码 + * @return {@link Key},null 表示无法识别的密钥类型 + * @since 5.8.5 + */ + public static Key readPemKey(final InputStream keyStream, final char[] password) { + try (final PEMParser pemParser = new PEMParser(new InputStreamReader(keyStream))) { + return readPemKeyFromKeyObject(pemParser.readObject(), password); + } catch (final IOException e) { + throw new CryptoException(e); + } + } + + /** + * 从pem文件中读取公钥或私钥
+ * 根据类型返回 {@link PublicKey} 或者 {@link PrivateKey} + * + * @param keyStream pem 流 + * @param password 私钥密码 + * @return {@link Key},null 表示无法识别的密钥类型 + * @since 5.8.5 + */ + public static KeyPair readPemKeyPair(final InputStream keyStream, final char[] password) { + try (final PEMParser pemParser = new PEMParser(new InputStreamReader(keyStream))) { + return readPemKeyPairFromKeyObject(pemParser.readObject(), password); + } catch (final IOException e) { + throw new CryptoException(e); + } + } + /** * 读取Pem文件中的密钥,密钥支持包括:
*