style: reformatted codes

This commit is contained in:
zihluwang
2025-06-18 13:57:32 +08:00
parent 022c1c241a
commit 0e87c0f336
11 changed files with 81 additions and 46 deletions
+3 -2
View File
@@ -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
@@ -61,9 +61,11 @@ public final class CollectionUtil {
* {@code maxSize} is less than zero, or
* {@code collectionFactory} is {@code null}
*/
public static <T, C extends Collection<T>> List<C> chunk(C originalCollection,
int maxSize,
Supplier<C> collectionFactory) {
public static <T, C extends Collection<T>> List<C> chunk(
C originalCollection,
int maxSize,
Supplier<C> collectionFactory
) {
// check inputs
if (Objects.isNull(originalCollection)) {
throw new IllegalArgumentException("Collection must not be null.");
@@ -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);
}
}
+3 -3
View File
@@ -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-----
```
```
@@ -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);
@@ -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 {
@@ -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 {
@@ -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
*/
@@ -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-----", "")