style: reformatted codes
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
# Module `devkit-utils`
|
# Common Toolbox
|
||||||
|
|
||||||
## Introduction
|
## 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
|
## Features
|
||||||
|
|
||||||
|
|||||||
@@ -61,9 +61,11 @@ public final class CollectionUtil {
|
|||||||
* {@code maxSize} is less than zero, or
|
* {@code maxSize} is less than zero, or
|
||||||
* {@code collectionFactory} is {@code null}
|
* {@code collectionFactory} is {@code null}
|
||||||
*/
|
*/
|
||||||
public static <T, C extends Collection<T>> List<C> chunk(C originalCollection,
|
public static <T, C extends Collection<T>> List<C> chunk(
|
||||||
|
C originalCollection,
|
||||||
int maxSize,
|
int maxSize,
|
||||||
Supplier<C> collectionFactory) {
|
Supplier<C> collectionFactory
|
||||||
|
) {
|
||||||
// check inputs
|
// check inputs
|
||||||
if (Objects.isNull(originalCollection)) {
|
if (Objects.isNull(originalCollection)) {
|
||||||
throw new IllegalArgumentException("Collection must not be null.");
|
throw new IllegalArgumentException("Collection must not be null.");
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ public final class RangeUtil {
|
|||||||
return IntStream.range(start, end);
|
return IntStream.range(start, end);
|
||||||
} else {
|
} else {
|
||||||
// Descending range (exclusive of end)
|
// 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
## ECDSA-based algorithm
|
||||||
|
|
||||||
|
|||||||
@@ -25,8 +25,8 @@ package com.onixbyte.crypto;
|
|||||||
import java.security.PrivateKey;
|
import java.security.PrivateKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@code PrivateKeyLoader} interface provides utility methods for loading keys pairs from
|
* The {@code PrivateKeyLoader} provides utility methods for loading private keys from
|
||||||
* PEM-formatted key text. This class supports loading both private and public keys.
|
* PEM-formatted key text.
|
||||||
*
|
*
|
||||||
* @author zihluwang
|
* @author zihluwang
|
||||||
* @author siujamo
|
* @author siujamo
|
||||||
|
|||||||
@@ -29,16 +29,19 @@ import java.security.interfaces.ECPublicKey;
|
|||||||
import java.security.interfaces.RSAPublicKey;
|
import java.security.interfaces.RSAPublicKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* The {@code PublicKeyLoader} provides utility methods for loading public keys from PEM-formatted
|
||||||
|
* key text.
|
||||||
*
|
*
|
||||||
|
* @author zihluwang
|
||||||
* @author siujamo
|
* @author siujamo
|
||||||
* @version 3.0.0
|
* @version 3.0.0
|
||||||
*/
|
*/
|
||||||
public interface PublicKeyLoader {
|
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
|
* @return loaded private key
|
||||||
*/
|
*/
|
||||||
PublicKey loadPublicKey(String pemKeyText);
|
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.exception.KeyLoadingException;
|
||||||
import com.onixbyte.crypto.util.CryptoUtil;
|
import com.onixbyte.crypto.util.CryptoUtil;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
|
||||||
import java.security.AlgorithmParameters;
|
|
||||||
import java.security.KeyFactory;
|
import java.security.KeyFactory;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.interfaces.ECPrivateKey;
|
import java.security.interfaces.ECPrivateKey;
|
||||||
import java.security.interfaces.ECPublicKey;
|
|
||||||
import java.security.spec.*;
|
import java.security.spec.*;
|
||||||
import java.util.Base64;
|
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>
|
* <p>
|
||||||
* <b>Example usage:</b>
|
* This class implements the {@link PrivateKeyLoader} interface and provides methods to load private
|
||||||
* <pre>{@code
|
* RSA keys. The keys are expected to be in the standard PEM format, which includes Base64-encoded
|
||||||
* PrivateKeyLoader keyLoader = new ECPrivateKeyLoader();
|
* key content surrounded by header and footer lines. The class handles the decoding of Base64
|
||||||
* String pemPrivateKey = """
|
* content and the generation of keys using the RSA key factory.
|
||||||
* -----BEGIN EC PRIVATE KEY-----
|
* <p>
|
||||||
* ...
|
* Any exceptions encountered during the loading process are encapsulated in a
|
||||||
* -----END EC PRIVATE KEY-----""";
|
* {@link KeyLoadingException}, allowing for flexible error handling.
|
||||||
* ECPrivateKey privateKey = PrivateKeyLoader.loadEcdsaPrivateKey(pemPrivateKey);
|
|
||||||
* }</pre>
|
|
||||||
*
|
*
|
||||||
* @author zihluwang
|
* @author zihluwang
|
||||||
|
* @author siujamo
|
||||||
* @version 3.0.0
|
* @version 3.0.0
|
||||||
|
* @see PrivateKeyLoader
|
||||||
|
* @see KeyLoadingException
|
||||||
*/
|
*/
|
||||||
public class ECPrivateKeyLoader implements PrivateKeyLoader {
|
public class ECPrivateKeyLoader implements PrivateKeyLoader {
|
||||||
|
|
||||||
|
|||||||
+15
@@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
package com.onixbyte.crypto.algorithm.ecdsa;
|
package com.onixbyte.crypto.algorithm.ecdsa;
|
||||||
|
|
||||||
|
import com.onixbyte.crypto.PrivateKeyLoader;
|
||||||
import com.onixbyte.crypto.PublicKeyLoader;
|
import com.onixbyte.crypto.PublicKeyLoader;
|
||||||
import com.onixbyte.crypto.exception.KeyLoadingException;
|
import com.onixbyte.crypto.exception.KeyLoadingException;
|
||||||
import com.onixbyte.crypto.util.CryptoUtil;
|
import com.onixbyte.crypto.util.CryptoUtil;
|
||||||
@@ -38,7 +39,21 @@ import java.util.HashSet;
|
|||||||
import java.util.Set;
|
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 {
|
public class ECPublicKeyLoader implements PublicKeyLoader {
|
||||||
|
|
||||||
|
|||||||
+6
-5
@@ -35,18 +35,19 @@ import java.security.spec.*;
|
|||||||
import java.util.Base64;
|
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>
|
* <p>
|
||||||
* This class implements the {@link PrivateKeyLoader} interface and provides methods to load both
|
* This class implements the {@link PrivateKeyLoader} interface and provides methods to load private
|
||||||
* private and public RSA keys. The keys are expected to be in the standard PEM format, which
|
* RSA keys. The keys are expected to be in the standard PEM format, which includes Base64-encoded
|
||||||
* includes Base64-encoded key content surrounded by header and footer lines. The class handles
|
* key content surrounded by header and footer lines. The class handles the decoding of Base64
|
||||||
* the decoding of Base64 content and the generation of keys using the RSA key factory.
|
* content and the generation of keys using the RSA key factory.
|
||||||
* <p>
|
* <p>
|
||||||
* Any exceptions encountered during the loading process are encapsulated in a
|
* Any exceptions encountered during the loading process are encapsulated in a
|
||||||
* {@link KeyLoadingException}, allowing for flexible error handling.
|
* {@link KeyLoadingException}, allowing for flexible error handling.
|
||||||
*
|
*
|
||||||
* @author zihluwang
|
* @author zihluwang
|
||||||
* @author siujamo
|
* @author siujamo
|
||||||
|
* @version 3.0.0
|
||||||
* @see PrivateKeyLoader
|
* @see PrivateKeyLoader
|
||||||
* @see KeyLoadingException
|
* @see KeyLoadingException
|
||||||
*/
|
*/
|
||||||
|
|||||||
+15
@@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
package com.onixbyte.crypto.algorithm.rsa;
|
package com.onixbyte.crypto.algorithm.rsa;
|
||||||
|
|
||||||
|
import com.onixbyte.crypto.PrivateKeyLoader;
|
||||||
import com.onixbyte.crypto.PublicKeyLoader;
|
import com.onixbyte.crypto.PublicKeyLoader;
|
||||||
import com.onixbyte.crypto.exception.KeyLoadingException;
|
import com.onixbyte.crypto.exception.KeyLoadingException;
|
||||||
import com.onixbyte.crypto.util.CryptoUtil;
|
import com.onixbyte.crypto.util.CryptoUtil;
|
||||||
@@ -37,7 +38,21 @@ import java.security.spec.X509EncodedKeySpec;
|
|||||||
import java.util.Base64;
|
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 {
|
public class RSAPublicKeyLoader implements PublicKeyLoader {
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,11 @@
|
|||||||
package com.onixbyte.crypto.util;
|
package com.onixbyte.crypto.util;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Utility class for cryptographic operations.
|
||||||
*
|
*
|
||||||
|
* @author zihluwang
|
||||||
|
* @author siujamo
|
||||||
|
* @version 3.0.0
|
||||||
*/
|
*/
|
||||||
public final class CryptoUtil {
|
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,
|
* Extracts the raw content from a PEM-formatted key by removing any headers, footers,
|
||||||
* and newline characters.
|
* and newline characters.
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* This method processes the provided PEM key text to return a cleaned string that contains
|
* This method processes the given PEM key text and returns a cleaned string containing only
|
||||||
* only the key content. The method strips away the
|
* the key material. It removes the lines matching the
|
||||||
* {@code "-----BEGIN (EC )?(PRIVATE|PUBLIC) KEY-----"} and
|
* {@code "-----BEGIN (EC )?(RSA )?(PRIVATE|PUBLIC) KEY-----"} and
|
||||||
* {@code "-----END (EC )?(PRIVATE|PUBLIC) KEY-----"} lines, as well as any new line characters,
|
* {@code "-----END (EC )?(RSA )?(PRIVATE|PUBLIC) KEY-----"} patterns,
|
||||||
* resulting in a continuous string representation of the key, which can be used for further
|
* as well as any newline characters,
|
||||||
* cryptographic operations.
|
* 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
|
* @param pemKeyText the PEM-formatted key as a string, which may include headers, footers,
|
||||||
* line breaks
|
* and line breaks
|
||||||
* @return a string containing the raw key content devoid of any unnecessary formatting
|
* @return a string containing the raw key content without any unnecessary formatting or whitespace
|
||||||
* or whitespace
|
|
||||||
*/
|
*/
|
||||||
public static String getRawContent(String pemKeyText) {
|
public static String getRawContent(String pemKeyText) {
|
||||||
// remove all unnecessary parts of the pem key text
|
|
||||||
return pemKeyText
|
return pemKeyText
|
||||||
.replaceAll("-----BEGIN ((EC )|(RSA ))?(PRIVATE|PUBLIC) KEY-----", "")
|
.replaceAll("-----BEGIN ((EC )|(RSA ))?(PRIVATE|PUBLIC) KEY-----", "")
|
||||||
.replaceAll("-----END ((EC )|(RSA ))?(PRIVATE|PUBLIC) KEY-----", "")
|
.replaceAll("-----END ((EC )|(RSA ))?(PRIVATE|PUBLIC) KEY-----", "")
|
||||||
|
|||||||
Reference in New Issue
Block a user