diff --git a/common-toolbox/README.md b/common-toolbox/README.md index 6d2b02f..46be06b 100644 --- a/common-toolbox/README.md +++ b/common-toolbox/README.md @@ -1,8 +1,9 @@ -# Module `devkit-utils` +# Common Toolbox ## Introduction -This module provides a set of utilities to streamline Java codes. +Common Toolbox is a Java SE utility library, that provides a collection of utility to streamline +your Java coding experience. ## Features diff --git a/common-toolbox/src/main/java/com/onixbyte/common/util/CollectionUtil.java b/common-toolbox/src/main/java/com/onixbyte/common/util/CollectionUtil.java index 9737a64..615f1ee 100644 --- a/common-toolbox/src/main/java/com/onixbyte/common/util/CollectionUtil.java +++ b/common-toolbox/src/main/java/com/onixbyte/common/util/CollectionUtil.java @@ -61,9 +61,11 @@ public final class CollectionUtil { * {@code maxSize} is less than zero, or * {@code collectionFactory} is {@code null} */ - public static > List chunk(C originalCollection, - int maxSize, - Supplier collectionFactory) { + public static > List chunk( + C originalCollection, + int maxSize, + Supplier collectionFactory + ) { // check inputs if (Objects.isNull(originalCollection)) { throw new IllegalArgumentException("Collection must not be null."); diff --git a/common-toolbox/src/main/java/com/onixbyte/common/util/RangeUtil.java b/common-toolbox/src/main/java/com/onixbyte/common/util/RangeUtil.java index 81aa62f..e3a0a4c 100644 --- a/common-toolbox/src/main/java/com/onixbyte/common/util/RangeUtil.java +++ b/common-toolbox/src/main/java/com/onixbyte/common/util/RangeUtil.java @@ -122,7 +122,7 @@ public final class RangeUtil { return IntStream.range(start, end); } else { // Descending range (exclusive of end) - return IntStream.iterate(start, n -> n > end, n -> n - 1); + return IntStream.iterate(start, (n) -> n > end, (n) -> n - 1); } } diff --git a/crypto-toolbox/README.md b/crypto-toolbox/README.md index 3d32826..8e287e8 100644 --- a/crypto-toolbox/README.md +++ b/crypto-toolbox/README.md @@ -1,6 +1,6 @@ -# KeyLoader +# Crypto Toolbox -KeyLoader provides utility methods to load keys from pem-formatted key texts. +Crypto Toolbox provides methods to simplify your codes on key pairs. ## ECDSA-based algorithm @@ -110,4 +110,4 @@ pzDq72a2Rhws+dbrH0gqsg1lsLDfhhui2FomuBpDZUtHq0Jz/IEvd3X45XvegSH8 t8+yL/pFK3+YpDVtj/IzMSwL+izvnXFALvZOO+8CABeyKuSjLh/6LbAzrvoftql5 gQIDAQAB -----END PUBLIC KEY----- -``` \ No newline at end of file +``` diff --git a/crypto-toolbox/src/main/java/com/onixbyte/crypto/PrivateKeyLoader.java b/crypto-toolbox/src/main/java/com/onixbyte/crypto/PrivateKeyLoader.java index e1b7876..9f3f8c7 100644 --- a/crypto-toolbox/src/main/java/com/onixbyte/crypto/PrivateKeyLoader.java +++ b/crypto-toolbox/src/main/java/com/onixbyte/crypto/PrivateKeyLoader.java @@ -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 diff --git a/crypto-toolbox/src/main/java/com/onixbyte/crypto/PublicKeyLoader.java b/crypto-toolbox/src/main/java/com/onixbyte/crypto/PublicKeyLoader.java index e4dff60..943afcc 100644 --- a/crypto-toolbox/src/main/java/com/onixbyte/crypto/PublicKeyLoader.java +++ b/crypto-toolbox/src/main/java/com/onixbyte/crypto/PublicKeyLoader.java @@ -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); diff --git a/crypto-toolbox/src/main/java/com/onixbyte/crypto/algorithm/ecdsa/ECPrivateKeyLoader.java b/crypto-toolbox/src/main/java/com/onixbyte/crypto/algorithm/ecdsa/ECPrivateKeyLoader.java index fa527ec..99adf89 100644 --- a/crypto-toolbox/src/main/java/com/onixbyte/crypto/algorithm/ecdsa/ECPrivateKeyLoader.java +++ b/crypto-toolbox/src/main/java/com/onixbyte/crypto/algorithm/ecdsa/ECPrivateKeyLoader.java @@ -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. *

- * Example usage: - *

{@code
- * PrivateKeyLoader keyLoader = new ECPrivateKeyLoader();
- * String pemPrivateKey = """
- *                        -----BEGIN EC PRIVATE KEY-----
- *                        ...
- *                        -----END EC PRIVATE KEY-----""";
- * ECPrivateKey privateKey = PrivateKeyLoader.loadEcdsaPrivateKey(pemPrivateKey);
- * }
+ * 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. + *

+ * 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 { diff --git a/crypto-toolbox/src/main/java/com/onixbyte/crypto/algorithm/ecdsa/ECPublicKeyLoader.java b/crypto-toolbox/src/main/java/com/onixbyte/crypto/algorithm/ecdsa/ECPublicKeyLoader.java index de14870..e3e0ef3 100644 --- a/crypto-toolbox/src/main/java/com/onixbyte/crypto/algorithm/ecdsa/ECPublicKeyLoader.java +++ b/crypto-toolbox/src/main/java/com/onixbyte/crypto/algorithm/ecdsa/ECPublicKeyLoader.java @@ -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. + *

+ * 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. + *

+ * 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 { diff --git a/crypto-toolbox/src/main/java/com/onixbyte/crypto/algorithm/rsa/RSAPrivateKeyLoader.java b/crypto-toolbox/src/main/java/com/onixbyte/crypto/algorithm/rsa/RSAPrivateKeyLoader.java index 8cd6eb9..0d9f475 100644 --- a/crypto-toolbox/src/main/java/com/onixbyte/crypto/algorithm/rsa/RSAPrivateKeyLoader.java +++ b/crypto-toolbox/src/main/java/com/onixbyte/crypto/algorithm/rsa/RSAPrivateKeyLoader.java @@ -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. *

- * 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. *

* 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 */ diff --git a/crypto-toolbox/src/main/java/com/onixbyte/crypto/algorithm/rsa/RSAPublicKeyLoader.java b/crypto-toolbox/src/main/java/com/onixbyte/crypto/algorithm/rsa/RSAPublicKeyLoader.java index f1a5c7b..c1a8810 100644 --- a/crypto-toolbox/src/main/java/com/onixbyte/crypto/algorithm/rsa/RSAPublicKeyLoader.java +++ b/crypto-toolbox/src/main/java/com/onixbyte/crypto/algorithm/rsa/RSAPublicKeyLoader.java @@ -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. + *

+ * 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. + *

+ * 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 { diff --git a/crypto-toolbox/src/main/java/com/onixbyte/crypto/util/CryptoUtil.java b/crypto-toolbox/src/main/java/com/onixbyte/crypto/util/CryptoUtil.java index 9432a27..9951af5 100644 --- a/crypto-toolbox/src/main/java/com/onixbyte/crypto/util/CryptoUtil.java +++ b/crypto-toolbox/src/main/java/com/onixbyte/crypto/util/CryptoUtil.java @@ -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. * *

- * 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-----", "")