From fc945839d240f5805029e42eb06fe827f19315cb Mon Sep 17 00:00:00 2001 From: siujamo Date: Thu, 24 Apr 2025 13:15:46 +0800 Subject: [PATCH] refactor: extract common codes --- .../java/com/onixbyte/security/KeyLoader.java | 25 +++++++++++++++++++ .../onixbyte/security/impl/EcKeyLoader.java | 12 ++------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/key-pair-loader/src/main/java/com/onixbyte/security/KeyLoader.java b/key-pair-loader/src/main/java/com/onixbyte/security/KeyLoader.java index 0fa6a63..097ae8e 100644 --- a/key-pair-loader/src/main/java/com/onixbyte/security/KeyLoader.java +++ b/key-pair-loader/src/main/java/com/onixbyte/security/KeyLoader.java @@ -49,4 +49,29 @@ public interface KeyLoader { */ PublicKey loadPublicKey(String pemKeyText); + /** + * Retrieves the raw content of a PEM formatted key by removing unnecessary headers, footers, + * and new line characters. + * + *

+ * This method processes the provided PEM key text to return a cleaned string that contains + * only the key content. The method strips away the + * {@code "-----BEGIN (EC )?(PRIVATE|PUBLIC) KEY-----"} and + * {@code "-----END (EC )?(PRIVATE|PUBLIC) KEY-----"} lines, as well as any new line characters, + * resulting in a continuous string representation of the key, which can be used for further + * cryptographic operations. + * + * @param pemKeyText the PEM formatted key as a string, which may include headers, footers and + * line breaks + * @return a string containing the raw key content devoid of any unnecessary formatting + * or whitespace + */ + default String getRawContent(String pemKeyText) { + // remove all unnecessary parts of the pem key text + return pemKeyText + .replaceAll("-----BEGIN (EC )?(PRIVATE|PUBLIC) KEY-----", "") + .replaceAll("-----END (EC )?(PRIVATE|PUBLIC) KEY-----", "") + .replaceAll("\n", ""); + } + } diff --git a/key-pair-loader/src/main/java/com/onixbyte/security/impl/EcKeyLoader.java b/key-pair-loader/src/main/java/com/onixbyte/security/impl/EcKeyLoader.java index b94b1f7..96daa58 100644 --- a/key-pair-loader/src/main/java/com/onixbyte/security/impl/EcKeyLoader.java +++ b/key-pair-loader/src/main/java/com/onixbyte/security/impl/EcKeyLoader.java @@ -82,11 +82,7 @@ public class EcKeyLoader implements KeyLoader { @Override public ECPrivateKey loadPrivateKey(String pemKeyText) { try { - // remove all unnecessary parts of the pem key text - pemKeyText = pemKeyText - .replaceAll("-----BEGIN (EC )?PRIVATE KEY-----", "") - .replaceAll("-----END (EC )?PRIVATE KEY-----", "") - .replaceAll("\n", ""); + pemKeyText = getRawContent(pemKeyText); var decodedKeyString = decoder.decode(pemKeyText); var keySpec = new PKCS8EncodedKeySpec(decodedKeyString); @@ -112,11 +108,7 @@ public class EcKeyLoader implements KeyLoader { @Override public ECPublicKey loadPublicKey(String pemKeyText) { try { - // remove all unnecessary parts of the pem key text - pemKeyText = pemKeyText - .replaceAll("-----BEGIN (EC )?PUBLIC KEY-----", "") - .replaceAll("-----END (EC )?PUBLIC KEY-----", "") - .replaceAll("\n", ""); + pemKeyText = getRawContent(pemKeyText); var keyBytes = decoder.decode(pemKeyText); var spec = new X509EncodedKeySpec(keyBytes); var key = keyFactory.generatePublic(spec);