style: reformatted codes
This commit is contained in:
@@ -25,8 +25,8 @@ package com.onixbyte.crypto;
|
||||
import java.security.PrivateKey;
|
||||
|
||||
/**
|
||||
* The {@code PrivateKeyLoader} interface provides utility methods for loading keys pairs from
|
||||
* PEM-formatted key text. This class supports loading both private and public keys.
|
||||
* The {@code PrivateKeyLoader} provides utility methods for loading private keys from
|
||||
* PEM-formatted key text.
|
||||
*
|
||||
* @author zihluwang
|
||||
* @author siujamo
|
||||
|
||||
@@ -29,16 +29,19 @@ import java.security.interfaces.ECPublicKey;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
|
||||
/**
|
||||
* The {@code PublicKeyLoader} provides utility methods for loading public keys from PEM-formatted
|
||||
* key text.
|
||||
*
|
||||
* @author zihluwang
|
||||
* @author siujamo
|
||||
* @version 3.0.0
|
||||
*/
|
||||
public interface PublicKeyLoader {
|
||||
|
||||
/**
|
||||
* Load public key from pem-formatted key text.
|
||||
* Load public key from PEM-formatted key text.
|
||||
*
|
||||
* @param pemKeyText pem-formatted key text
|
||||
* @param pemKeyText PEM-formatted key text
|
||||
* @return loaded private key
|
||||
*/
|
||||
PublicKey loadPublicKey(String pemKeyText);
|
||||
|
||||
+11
-15
@@ -26,32 +26,28 @@ import com.onixbyte.crypto.PrivateKeyLoader;
|
||||
import com.onixbyte.crypto.exception.KeyLoadingException;
|
||||
import com.onixbyte.crypto.util.CryptoUtil;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.AlgorithmParameters;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.interfaces.ECPrivateKey;
|
||||
import java.security.interfaces.ECPublicKey;
|
||||
import java.security.spec.*;
|
||||
import java.util.Base64;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Key pair loader for loading key pairs for ECDSA-based algorithms.
|
||||
* A class responsible for loading private ECDSA keys from PEM formatted text.
|
||||
* <p>
|
||||
* <b>Example usage:</b>
|
||||
* <pre>{@code
|
||||
* PrivateKeyLoader keyLoader = new ECPrivateKeyLoader();
|
||||
* String pemPrivateKey = """
|
||||
* -----BEGIN EC PRIVATE KEY-----
|
||||
* ...
|
||||
* -----END EC PRIVATE KEY-----""";
|
||||
* ECPrivateKey privateKey = PrivateKeyLoader.loadEcdsaPrivateKey(pemPrivateKey);
|
||||
* }</pre>
|
||||
* This class implements the {@link PrivateKeyLoader} interface and provides methods to load private
|
||||
* RSA keys. The keys are expected to be in the standard PEM format, which includes Base64-encoded
|
||||
* key content surrounded by header and footer lines. The class handles the decoding of Base64
|
||||
* content and the generation of keys using the RSA key factory.
|
||||
* <p>
|
||||
* Any exceptions encountered during the loading process are encapsulated in a
|
||||
* {@link KeyLoadingException}, allowing for flexible error handling.
|
||||
*
|
||||
* @author zihluwang
|
||||
* @author siujamo
|
||||
* @version 3.0.0
|
||||
* @see PrivateKeyLoader
|
||||
* @see KeyLoadingException
|
||||
*/
|
||||
public class ECPrivateKeyLoader implements PrivateKeyLoader {
|
||||
|
||||
|
||||
+15
@@ -22,6 +22,7 @@
|
||||
|
||||
package com.onixbyte.crypto.algorithm.ecdsa;
|
||||
|
||||
import com.onixbyte.crypto.PrivateKeyLoader;
|
||||
import com.onixbyte.crypto.PublicKeyLoader;
|
||||
import com.onixbyte.crypto.exception.KeyLoadingException;
|
||||
import com.onixbyte.crypto.util.CryptoUtil;
|
||||
@@ -38,7 +39,21 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A class responsible for loading public ECDSA keys from PEM formatted text.
|
||||
* <p>
|
||||
* This class implements the {@link PublicKeyLoader} interface and provides methods to load private
|
||||
* RSA keys. The keys are expected to be in the standard PEM format, which includes Base64-encoded
|
||||
* key content surrounded by header and footer lines. The class handles the decoding of Base64
|
||||
* content and the generation of keys using the RSA key factory.
|
||||
* <p>
|
||||
* Any exceptions encountered during the loading process are encapsulated in a
|
||||
* {@link KeyLoadingException}, allowing for flexible error handling.
|
||||
*
|
||||
* @author zihluwang
|
||||
* @author siujamo
|
||||
* @version 3.0.0
|
||||
* @see PrivateKeyLoader
|
||||
* @see KeyLoadingException
|
||||
*/
|
||||
public class ECPublicKeyLoader implements PublicKeyLoader {
|
||||
|
||||
|
||||
+6
-5
@@ -35,18 +35,19 @@ import java.security.spec.*;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* A class responsible for loading RSA keys from PEM formatted text.
|
||||
* A class responsible for loading private RSA keys from PEM formatted text.
|
||||
* <p>
|
||||
* This class implements the {@link PrivateKeyLoader} interface and provides methods to load both
|
||||
* private and public RSA keys. The keys are expected to be in the standard PEM format, which
|
||||
* includes Base64-encoded key content surrounded by header and footer lines. The class handles
|
||||
* the decoding of Base64 content and the generation of keys using the RSA key factory.
|
||||
* This class implements the {@link PrivateKeyLoader} interface and provides methods to load private
|
||||
* RSA keys. The keys are expected to be in the standard PEM format, which includes Base64-encoded
|
||||
* key content surrounded by header and footer lines. The class handles the decoding of Base64
|
||||
* content and the generation of keys using the RSA key factory.
|
||||
* <p>
|
||||
* Any exceptions encountered during the loading process are encapsulated in a
|
||||
* {@link KeyLoadingException}, allowing for flexible error handling.
|
||||
*
|
||||
* @author zihluwang
|
||||
* @author siujamo
|
||||
* @version 3.0.0
|
||||
* @see PrivateKeyLoader
|
||||
* @see KeyLoadingException
|
||||
*/
|
||||
|
||||
+15
@@ -22,6 +22,7 @@
|
||||
|
||||
package com.onixbyte.crypto.algorithm.rsa;
|
||||
|
||||
import com.onixbyte.crypto.PrivateKeyLoader;
|
||||
import com.onixbyte.crypto.PublicKeyLoader;
|
||||
import com.onixbyte.crypto.exception.KeyLoadingException;
|
||||
import com.onixbyte.crypto.util.CryptoUtil;
|
||||
@@ -37,7 +38,21 @@ import java.security.spec.X509EncodedKeySpec;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* A class responsible for loading public RSA keys from PEM formatted text.
|
||||
* <p>
|
||||
* This class implements the {@link PublicKeyLoader} interface and provides methods to load public
|
||||
* RSA keys. The keys are expected to be in the standard PEM format, which includes Base64-encoded
|
||||
* key content surrounded by header and footer lines. The class handles the decoding of Base64
|
||||
* content and the generation of keys using the RSA key factory.
|
||||
* <p>
|
||||
* Any exceptions encountered during the loading process are encapsulated in a
|
||||
* {@link KeyLoadingException}, allowing for flexible error handling.
|
||||
*
|
||||
* @author zihluwang
|
||||
* @author siujamo
|
||||
* @version 3.0.0
|
||||
* @see PrivateKeyLoader
|
||||
* @see KeyLoadingException
|
||||
*/
|
||||
public class RSAPublicKeyLoader implements PublicKeyLoader {
|
||||
|
||||
|
||||
@@ -23,7 +23,11 @@
|
||||
package com.onixbyte.crypto.util;
|
||||
|
||||
/**
|
||||
* Utility class for cryptographic operations.
|
||||
*
|
||||
* @author zihluwang
|
||||
* @author siujamo
|
||||
* @version 3.0.0
|
||||
*/
|
||||
public final class CryptoUtil {
|
||||
|
||||
@@ -34,24 +38,22 @@ public final class CryptoUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the raw content of a PEM formatted key by removing unnecessary headers, footers,
|
||||
* and new line characters.
|
||||
* Extracts the raw content from a PEM-formatted key by removing any headers, footers,
|
||||
* and newline characters.
|
||||
*
|
||||
* <p>
|
||||
* 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.
|
||||
* This method processes the given PEM key text and returns a cleaned string containing only
|
||||
* the key material. It removes the lines matching the
|
||||
* {@code "-----BEGIN (EC )?(RSA )?(PRIVATE|PUBLIC) KEY-----"} and
|
||||
* {@code "-----END (EC )?(RSA )?(PRIVATE|PUBLIC) KEY-----"} patterns,
|
||||
* as well as any newline characters,
|
||||
* resulting in a continuous string that can be used directly for 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
|
||||
* @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 without any unnecessary formatting or whitespace
|
||||
*/
|
||||
public static String getRawContent(String pemKeyText) {
|
||||
// remove all unnecessary parts of the pem key text
|
||||
return pemKeyText
|
||||
.replaceAll("-----BEGIN ((EC )|(RSA ))?(PRIVATE|PUBLIC) KEY-----", "")
|
||||
.replaceAll("-----END ((EC )|(RSA ))?(PRIVATE|PUBLIC) KEY-----", "")
|
||||
|
||||
Reference in New Issue
Block a user