Skip to content

Commit eb3b3b1

Browse files
author
Zihlu Wang
authored
Merge pull request #15 from CodeCraftersCN/dev/v1.1.0
Upgrade to v1.1.0
2 parents d2b57d7 + 1c3f6ac commit eb3b3b1

File tree

64 files changed

+1041
-672
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1041
-672
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ A module for generating globally unique IDs. It includes a facade interface and
2727

2828
<span style="font-size: 14px;">_[Learn more](https://github.com/CodeCraftersCN/jdevkit/webcal/README.md)_</span>
2929

30-
The module `webcal` is a Java library that facilitates the generation and resolution of iCalendar content for web-based calendar applications. It provides a flexible and easy-to-use API for creating web calendars with customizable settings and events.
30+
The module `webcal` is a Java library that facilitates the generation and resolution of iCalendar content for web-based calendar applications. It provides a flexible and easy-to-use API for creating web calendars with customisable settings and events.
3131

3232
With the `webcal` module, developers can easily integrate calendar functionality into web applications, enabling users to view, add, and manage events in a structured and standardized format. It is designed to simplify calendar-related tasks and enhance the overall user experience when dealing with calendar data on the web.
3333

devkit-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<parent>
2424
<groupId>cn.org.codecrafters</groupId>
2525
<artifactId>jdevkit</artifactId>
26-
<version>1.0.1</version>
26+
<version>1.1.0</version>
2727
</parent>
2828

2929
<artifactId>devkit-core</artifactId>

devkit-core/src/main/java/cn/org/codecrafters/devkit/core/exceptions/NotImplementedException.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,23 @@
1818
package cn.org.codecrafters.devkit.core.exceptions;
1919

2020
/**
21-
* NotImplementedException - Custom Runtime Exception
22-
* <p>
2321
* The {@code NotImplementedException} class is a custom runtime exception
2422
* that represents a situation where a particular method or functionality is
25-
* not implemented or is currently unavailable in the codebase. It extends the
26-
* standard {@code RuntimeException} class, making it an unchecked exception.
27-
*
23+
* not implemented or is currently unavailable in the codebase.
2824
* <p>
2925
* This exception is typically thrown when developers need to indicate that a
3026
* specific part of the code is incomplete or requires further implementation.
3127
* It serves as a placeholder to highlight unfinished sections of the
3228
* application during development and testing phases.
33-
*
3429
* <p>
3530
* Usage Example:
36-
*
3731
* <pre>
3832
* public void someMethod() {
3933
* // Some code...
4034
* throw new NotImplementedException("""
41-
* This feature will be implemented in a future release.
42-
* """);
35+
* This feature will be implemented in a future release.""");
4336
* }
4437
* </pre>
45-
*
4638
* <b>Contact</b>
4739
* <ul>
4840
* <li>
@@ -55,7 +47,7 @@
5547
* </ul>
5648
*
5749
* @author Zihlu Wang
58-
* @version 1.0.0
50+
* @version 1.1.0
5951
* @see RuntimeException
6052
* @since 1.0.0
6153
*/

devkit-core/src/main/java/cn/org/codecrafters/devkit/core/exceptions/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
/**
19-
* Commonly used exceptions will be used in JDevKit.
19+
* This package contains commonly used exceptions will be used in JDevKit.
2020
*
2121
* @author Zihlu Wang
2222
* @since 1.0.0

devkit-core/src/main/java/cn/org/codecrafters/devkit/core/package-info.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,16 @@
1616
*/
1717

1818
/**
19-
* This package is a part of JDevKit, an open-source Java Development Kit that
20-
* provides a set of convenient tools to streamline code development and
21-
* enhance productivity. This package serves as the core package containing
22-
* common exceptions that are used throughout the entireJDevKit project.
23-
*
19+
* This package is the core part of JDevKit, an open-source Java Development
20+
* Kit that provides a set of convenient tools to streamline code development
21+
* and enhance productivity. This package serves as the core package containing
22+
* common exceptions that are used throughout the entire JDevKit project.
2423
* <p>
2524
* JDevKit is designed to be modular, and other specific feature modules within
2625
* the library may rely on these exceptions from the core package.
27-
*
2826
* <p>
2927
* For more information and the latest version of JDevKit, please visit our
3028
* website <a href="https://codecrafters.org.cn">codecrafters.org.cn</a>.
31-
*
3229
* <p>
3330
* <b>Contact</b>
3431
* <ul>

devkit-core/src/main/java/cn/org/codecrafters/devkit/package-info.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
* This package is the main part of JDevKit, an open-source Java class library
2020
* that provides a set of convenient tools to streamline code development and
2121
* enhance productivity. This package serves as the root package for several
22-
* modules, containing devkit-core, guid and dev-utils module.
23-
*
22+
* modules, containing {@code devkit-core}, {@code guid} and {@code dev-utils}
23+
* module.
2424
* <p>
2525
* For more information and the latest version of JDevKit, please visit our
2626
* website <a href="https://codecrafters.org.cn">codecrafters.org.cn</a>.

devkit-utils/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>cn.org.codecrafters</groupId>
88
<artifactId>jdevkit</artifactId>
9-
<version>1.0.1</version>
9+
<version>1.1.0</version>
1010
</parent>
1111

1212
<artifactId>devkit-utils</artifactId>
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright (C) 2023 CodeCraftersCN.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
*
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package cn.org.codecrafters.devkit.utils;
19+
20+
import lombok.extern.slf4j.Slf4j;
21+
22+
import javax.crypto.BadPaddingException;
23+
import javax.crypto.Cipher;
24+
import javax.crypto.IllegalBlockSizeException;
25+
import javax.crypto.NoSuchPaddingException;
26+
import javax.crypto.spec.IvParameterSpec;
27+
import javax.crypto.spec.SecretKeySpec;
28+
import java.nio.charset.StandardCharsets;
29+
import java.security.InvalidAlgorithmParameterException;
30+
import java.security.InvalidKeyException;
31+
import java.security.NoSuchAlgorithmException;
32+
import java.util.Base64;
33+
import java.util.Objects;
34+
import java.util.UUID;
35+
36+
/**
37+
* {@link AesUtil} can help you encrypt and decrypt data with specified secret
38+
* by AES algorithm.
39+
*
40+
* @author hubin@baomidou
41+
* @version 1.1.0
42+
* @since 1.1.0
43+
*/
44+
@Slf4j
45+
public final class AesUtil {
46+
47+
private AesUtil() {
48+
}
49+
50+
private static final String AES = "AES";
51+
52+
private static final String AES_CBC_CIPHER = "AES/CBC/PKCS5Padding";
53+
54+
/**
55+
* Encrypts the data using the AES algorithm with the given secret.
56+
*
57+
* @param data the data to be encrypted
58+
* @param secret the secret to encrypt the data
59+
* @return the encryption result or {@code null} if encryption failed
60+
*/
61+
public static byte[] encrypt(byte[] data, byte[] secret) {
62+
try {
63+
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES);
64+
var cipher = Cipher.getInstance(AES_CBC_CIPHER);
65+
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(secret));
66+
return cipher.doFinal(data);
67+
} catch (NoSuchAlgorithmException | NoSuchPaddingException | UnsupportedOperationException |
68+
InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException |
69+
BadPaddingException exception) {
70+
log.error(exception.getMessage());
71+
for (var stackTraceElement : exception.getStackTrace()) {
72+
log.error("{}", stackTraceElement.toString());
73+
}
74+
}
75+
return null;
76+
}
77+
78+
/**
79+
* Decrypts the data using the AES algorithm with the given secret.
80+
*
81+
* @param data the data to be decrypted
82+
* @param secret the secret to encrypt the data
83+
* @return the decryption result or {@code null} if decryption failed
84+
*/
85+
public static byte[] decrypt(byte[] data, byte[] secret) {
86+
try {
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);
91+
} catch (NoSuchAlgorithmException | NoSuchPaddingException | UnsupportedOperationException |
92+
InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException |
93+
BadPaddingException exception) {
94+
log.error(exception.getMessage());
95+
for (var stackTraceElement : exception.getStackTrace()) {
96+
log.error("{}", stackTraceElement.toString());
97+
}
98+
}
99+
return null;
100+
}
101+
102+
/**
103+
* Encrypts the data using the AES algorithm with the given secret.
104+
*
105+
* @param data the data to be encrypted
106+
* @param secret the secret to encrypt the data
107+
* @return the encryption result or {@code null} if encryption failed
108+
*/
109+
public static String encrypt(String data, String secret) {
110+
return Base64.getEncoder().encodeToString(encrypt(data.getBytes(StandardCharsets.UTF_8), secret.getBytes(StandardCharsets.UTF_8)));
111+
}
112+
113+
/**
114+
* Decrypts the data using the AES algorithm with the given secret.
115+
*
116+
* @param data the data to be decrypted
117+
* @param secret the secret to encrypt the data
118+
* @return the decryption result or {@code null} if decryption failed
119+
*/
120+
public static String decrypt(String data, String secret) {
121+
return new String(Objects.requireNonNull(
122+
decrypt(Base64.getDecoder().decode(data.getBytes()),
123+
secret.getBytes(StandardCharsets.UTF_8)))
124+
);
125+
}
126+
127+
/**
128+
* Generates 16 characters-long random secret.
129+
*
130+
* @return the generated secure secret
131+
*/
132+
public static String generateRandomSecret() {
133+
return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 16);
134+
}
135+
136+
}

devkit-utils/src/main/java/cn/org/codecrafters/devkit/utils/Base64Util.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,18 @@
2020
import java.nio.charset.Charset;
2121
import java.nio.charset.StandardCharsets;
2222
import java.util.Base64;
23+
import java.util.Objects;
2324

2425
/**
25-
* <p>
26-
* The {@code Base64Util} class provides static methods to encode and decode
27-
* strings using Base64 encoding. It utilizes the {@link Base64} class from the
26+
* The {@link Base64Util} class provides static methods to encode and decode
27+
* strings with Base64 encoding. It utilizes the {@link Base64} class from the
2828
* Java standard library for performing the encoding and decoding operations.
2929
* This utility class offers convenient methods to encode and decode strings
3030
* with different character sets.
31-
*
3231
* <p>
3332
* This class is designed as a final class with a private constructor to
3433
* prevent instantiation. All methods in this class are static, allowing easy
3534
* access to the Base64 encoding and decoding functionality.
36-
*
3735
* <p>
3836
* Example usage:
3937
* <pre>
@@ -47,19 +45,45 @@
4745
* String decoded = Base64Util.decode(encoded);
4846
* System.out.println("Decoded string: " + decoded);
4947
* </pre>
50-
*
5148
* <p>
5249
* <b>Note:</b> This utility class uses the default charset (UTF-8) if no
5350
* specific charset is provided. It is recommended to specify the charset
5451
* explicitly to ensure consistent encoding and decoding.
55-
*
5652
*
5753
* @author Zihlu Wang
58-
* @version 1.0.0
54+
* @version 1.1.0
5955
* @since 1.0.0
6056
*/
6157
public final class Base64Util {
6258

59+
private static Base64.Encoder encoder;
60+
61+
private static Base64.Decoder decoder;
62+
63+
/**
64+
* Ensure that there is only one Base64 Encoder.
65+
*
66+
* @return the {@link Base64.Encoder} instance
67+
*/
68+
private static Base64.Encoder getEncoder() {
69+
if (Objects.isNull(encoder)) {
70+
encoder = Base64.getEncoder();
71+
}
72+
return encoder;
73+
}
74+
75+
/**
76+
* Ensure that there is only one Base64 Encoder.
77+
*
78+
* @return the {@link Base64.Encoder} instance
79+
*/
80+
private static Base64.Decoder getDecoder() {
81+
if (Objects.isNull(decoder)) {
82+
decoder = Base64.getDecoder();
83+
}
84+
return decoder;
85+
}
86+
6387
/**
6488
* Private constructor to prevent instantiation of the class.
6589
*/
@@ -74,8 +98,7 @@ private Base64Util() {
7498
* @return the Base64 encoded string
7599
*/
76100
public static String encode(String value, Charset charset) {
77-
var encoder = Base64.getEncoder();
78-
var encoded = encoder.encode(value.getBytes(charset));
101+
var encoded = getEncoder().encode(value.getBytes(charset));
79102

80103
return new String(encoded);
81104
}
@@ -98,8 +121,7 @@ public static String encode(String value) {
98121
* @return the decoded string
99122
*/
100123
public static String decode(String value, Charset charset) {
101-
var decoder = Base64.getDecoder();
102-
var decoded = decoder.decode(value.getBytes(charset));
124+
var decoded = getDecoder().decode(value.getBytes(charset));
103125

104126
return new String(decoded);
105127
}

0 commit comments

Comments
 (0)