Skip to content

Commit 6c8ed1c

Browse files
authored
Merge pull request #64 from onixbyte/release/2.2.0
Release 2.2.0
2 parents 4149554 + bfbce31 commit 6c8ed1c

File tree

21 files changed

+1026
-366
lines changed

21 files changed

+1026
-366
lines changed

devkit-utils/src/main/java/com/onixbyte/devkit/utils/AesUtil.java

Lines changed: 41 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,12 @@
1717

1818
package com.onixbyte.devkit.utils;
1919

20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
22-
23-
import javax.crypto.BadPaddingException;
2420
import javax.crypto.Cipher;
25-
import javax.crypto.IllegalBlockSizeException;
26-
import javax.crypto.NoSuchPaddingException;
2721
import javax.crypto.spec.IvParameterSpec;
2822
import javax.crypto.spec.SecretKeySpec;
2923
import java.nio.charset.StandardCharsets;
30-
import java.security.InvalidAlgorithmParameterException;
31-
import java.security.InvalidKeyException;
32-
import java.security.NoSuchAlgorithmException;
24+
import java.security.GeneralSecurityException;
3325
import java.util.Base64;
34-
import java.util.Objects;
3526
import java.util.UUID;
3627

3728
/**
@@ -42,110 +33,88 @@
4233
* The utility methods in this class are useful for scenarios where data needs to be securely
4334
* encrypted and decrypted.
4435
* </p>
45-
*
36+
*
4637
* <p><b>Example usage:</b></p>
47-
* <pre>
48-
* {@code
38+
* <pre>{@code
4939
* // Encrypting and decrypting byte array data
5040
* byte[] secretKey = "43f72073956d4c81".getBytes(StandardCharsets.UTF_8);
5141
* byte[] data = "Hello World".getBytes(StandardCharsets.UTF_8);
5242
* byte[] encryptedData = AesUtil.encrypt(data, secretKey);
5343
* byte[] decryptedData = AesUtil.decrypt(encryptedData, secretKey);
5444
* System.out.println(new String(decryptedData, StandardCharsets.UTF_8)); // Output: Hello World
55-
*
45+
*
5646
* // Encrypting and decrypting string data
5747
* String secret = "43f72073956d4c81";
5848
* String encryptedString = AesUtil.encrypt("Hello World", secret);
5949
* String decryptedString = AesUtil.decrypt(encryptedString, secret);
6050
* System.out.println(decryptedString); // Output: Hello World
61-
*
51+
*
6252
* // Generating a random secret key
6353
* String randomSecret = AesUtil.generateRandomSecret();
64-
* System.out.println(randomSecret); // Output: A ramdomly generated 16-character long secret
65-
* }
66-
* </pre>
54+
* System.out.println(randomSecret); // Output: A randomly generated 16-character long secret
55+
* }</pre>
6756
*
6857
* @author hubin@baomidou
6958
* @version 1.1.0
7059
* @since 1.1.0
7160
*/
7261
public final class AesUtil {
7362

74-
private final static Logger log = LoggerFactory.getLogger(AesUtil.class);
75-
7663
/**
77-
* Encrypts the data using the AES algorithm with the given secret.
64+
* Encrypts the specified data using the AES algorithm with the provided secret key.
7865
*
7966
* @param data the data to be encrypted
80-
* @param secret the secret to encrypt the data
81-
* @return the encryption result or {@code null} if encryption failed
67+
* @param secret the secret key used for encryption
68+
* @return the encrypted data as a byte array
69+
* @throws GeneralSecurityException if any cryptographic error occurs during encryption
8270
*/
83-
public static byte[] encrypt(byte[] data, byte[] secret) {
84-
try {
85-
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES);
86-
var cipher = Cipher.getInstance(AES_CBC_CIPHER);
87-
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(secret)); // set IV to secret
88-
return cipher.doFinal(data);
89-
} catch (NoSuchAlgorithmException | NoSuchPaddingException | UnsupportedOperationException |
90-
InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException |
91-
BadPaddingException exception) {
92-
log.error(exception.getMessage());
93-
for (var stackTraceElement : exception.getStackTrace()) {
94-
log.error("{}", stackTraceElement.toString());
95-
}
96-
}
97-
return null;
71+
public static byte[] encrypt(byte[] data, byte[] secret) throws GeneralSecurityException {
72+
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES);
73+
var cipher = Cipher.getInstance(AES_CBC_CIPHER);
74+
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(secret));
75+
return cipher.doFinal(data);
9876
}
9977

10078
/**
101-
* Decrypts the data using the AES algorithm with the given secret.
79+
* Decrypts the specified data using the AES algorithm with the provided secret key.
10280
*
103-
* @param data the data to be decrypted
104-
* @param secret the secret to encrypt the data
105-
* @return the decryption result or {@code null} if decryption failed
81+
* @param data the data to be decrypted
82+
* @param secret the secret key used for decryption
83+
* @return the decrypted data as a byte array
84+
* @throws GeneralSecurityException if any cryptographic error occurs during decryption
10685
*/
107-
public static byte[] decrypt(byte[] data, byte[] secret) {
108-
try {
109-
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES);
110-
var cipher = Cipher.getInstance(AES_CBC_CIPHER);
111-
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(secret)); // set IV to secret
112-
return cipher.doFinal(data);
113-
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
114-
UnsupportedOperationException | InvalidKeyException |
115-
InvalidAlgorithmParameterException | IllegalBlockSizeException |
116-
BadPaddingException exception) {
117-
log.error(exception.getMessage());
118-
for (var stackTraceElement : exception.getStackTrace()) {
119-
log.error("{}", stackTraceElement.toString());
120-
}
121-
}
122-
return null;
86+
public static byte[] decrypt(byte[] data, byte[] secret) throws GeneralSecurityException {
87+
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES);
88+
var cipher = Cipher.getInstance(AES_CBC_CIPHER);
89+
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(secret));
90+
return cipher.doFinal(data);
12391
}
12492

12593
/**
126-
* Encrypts the data using the AES algorithm with the given secret.
94+
* Encrypts the specified string data using the AES algorithm with the provided secret key.
12795
*
128-
* @param data the data to be encrypted
129-
* @param secret the secret to encrypt the data
130-
* @return the encryption result or {@code null} if encryption failed
96+
* @param data the string data to be encrypted
97+
* @param secret the secret key used for encryption
98+
* @return the encrypted data encoded in Base64
99+
* @throws GeneralSecurityException if any cryptographic error occurs during encryption
131100
*/
132-
public static String encrypt(String data, String secret) {
101+
public static String encrypt(String data, String secret) throws GeneralSecurityException {
133102
return Base64.getEncoder().encodeToString(encrypt(data.getBytes(StandardCharsets.UTF_8),
134103
secret.getBytes(StandardCharsets.UTF_8)));
135104
}
136105

137106
/**
138-
* Decrypts the data using the AES algorithm with the given secret.
107+
* Decrypts the specified Base64-encoded string data using the AES algorithm with the provided secret key.
139108
*
140-
* @param data the data to be decrypted
141-
* @param secret the secret to encrypt the data
142-
* @return the decryption result or {@code null} if decryption failed
109+
* @param data the Base64-encoded string data to be decrypted
110+
* @param secret the secret key used for decryption
111+
* @return the decrypted string data
112+
* @throws GeneralSecurityException if any cryptographic error occurs during decryption
143113
*/
144-
public static String decrypt(String data, String secret) {
145-
return new String(Objects.requireNonNull(
146-
decrypt(Base64.getDecoder().decode(data.getBytes()),
147-
secret.getBytes(StandardCharsets.UTF_8)))
148-
);
114+
public static String decrypt(String data, String secret) throws GeneralSecurityException {
115+
var decrypted = decrypt(Base64.getDecoder().decode(data.getBytes(StandardCharsets.UTF_8)),
116+
secret.getBytes(StandardCharsets.UTF_8));
117+
return new String(decrypted, StandardCharsets.UTF_8);
149118
}
150119

151120
/**

devkit-utils/src/main/java/com/onixbyte/devkit/utils/Base64Util.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
package com.onixbyte.devkit.utils;
1919

20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
22-
2320
import java.nio.charset.Charset;
2421
import java.nio.charset.StandardCharsets;
2522
import java.util.Base64;
@@ -58,8 +55,6 @@
5855
*/
5956
public final class Base64Util {
6057

61-
private final static Logger log = LoggerFactory.getLogger(Base64Util.class);
62-
6358
/**
6459
* Ensure that there is only one Base64 Encoder.
6560
*

devkit-utils/src/main/java/com/onixbyte/devkit/utils/BoolUtil.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
package com.onixbyte.devkit.utils;
1919

20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
22-
2320
import java.util.Arrays;
2421
import java.util.Objects;
2522
import java.util.function.BooleanSupplier;
@@ -47,8 +44,6 @@
4744
*/
4845
public final class BoolUtil {
4946

50-
private final static Logger log = LoggerFactory.getLogger(BoolUtil.class);
51-
5247
/**
5348
* Logical and calculation.
5449
*

devkit-utils/src/main/java/com/onixbyte/devkit/utils/BranchUtil.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -206,18 +206,4 @@ public void then(Runnable trueHandler) {
206206
then(trueHandler, null);
207207
}
208208

209-
/**
210-
* Get the boolean result.
211-
* <p>
212-
* <b>Note:</b> {@link BranchUtil} is not responsible for getting a raw boolean result, consider use
213-
* {@link BoolUtil} to replace.
214-
*
215-
* @return the result
216-
* @see BoolUtil
217-
*/
218-
@Deprecated(forRemoval = true)
219-
public boolean getResult() {
220-
return result;
221-
}
222-
223209
}

devkit-utils/src/main/java/com/onixbyte/devkit/utils/CollectionUtil.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
package com.onixbyte.devkit.utils;
1919

20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
22-
2320
import java.util.ArrayList;
2421
import java.util.Collection;
2522
import java.util.List;
@@ -33,8 +30,6 @@
3330
*/
3431
public final class CollectionUtil {
3532

36-
private static final Logger log = LoggerFactory.getLogger(CollectionUtil.class);
37-
3833
/**
3934
* Private constructor to prevent instantiation of this utility class.
4035
*/
@@ -68,7 +63,7 @@ public static <T, C extends Collection<T>> List<C> chunk(C originalCollection,
6863
throw new IllegalArgumentException("Collection must not be null.");
6964
}
7065

71-
if (maxSize < 0) {
66+
if (maxSize <= 0) {
7267
throw new IllegalArgumentException("maxSize must greater than 0.");
7368
}
7469

devkit-utils/src/main/java/com/onixbyte/devkit/utils/HashUtil.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@
1717

1818
package com.onixbyte.devkit.utils;
1919

20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
22-
2320
import java.nio.charset.Charset;
2421
import java.nio.charset.StandardCharsets;
2522
import java.security.MessageDigest;
2623
import java.security.NoSuchAlgorithmException;
27-
import java.util.Objects;
2824
import java.util.Optional;
2925

3026
/**
@@ -70,8 +66,6 @@
7066
*/
7167
public final class HashUtil {
7268

73-
private final static Logger log = LoggerFactory.getLogger(HashUtil.class);
74-
7569
/**
7670
* Calculates the MD2 hash value of the specified string using the given charset.
7771
*

devkit-utils/src/main/java/com/onixbyte/devkit/utils/ListUtil.java

Lines changed: 0 additions & 114 deletions
This file was deleted.

0 commit comments

Comments
 (0)