From fb8d6e34764601ba03e0ebc829118e1e9a5b1a6a Mon Sep 17 00:00:00 2001 From: zihluwang Date: Sat, 15 Feb 2025 11:00:57 +0800 Subject: [PATCH 1/2] feat: extracted Base64.Decoder --- .../main/java/com/onixbyte/security/impl/EcKeyLoader.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/key-pair-loader/src/main/java/com/onixbyte/security/impl/EcKeyLoader.java b/key-pair-loader/src/main/java/com/onixbyte/security/impl/EcKeyLoader.java index 4542c7c..b94b1f7 100644 --- a/key-pair-loader/src/main/java/com/onixbyte/security/impl/EcKeyLoader.java +++ b/key-pair-loader/src/main/java/com/onixbyte/security/impl/EcKeyLoader.java @@ -57,12 +57,15 @@ public class EcKeyLoader implements KeyLoader { private final KeyFactory keyFactory; + private final Base64.Decoder decoder; + /** * Initialise a key loader for EC-based algorithms. */ public EcKeyLoader() { try { this.keyFactory = KeyFactory.getInstance("EC"); + this.decoder = Base64.getDecoder(); } catch (NoSuchAlgorithmException e) { throw new KeyLoadingException(e); } @@ -84,7 +87,7 @@ public class EcKeyLoader implements KeyLoader { .replaceAll("-----BEGIN (EC )?PRIVATE KEY-----", "") .replaceAll("-----END (EC )?PRIVATE KEY-----", "") .replaceAll("\n", ""); - var decodedKeyString = Base64.getDecoder().decode(pemKeyText); + var decodedKeyString = decoder.decode(pemKeyText); var keySpec = new PKCS8EncodedKeySpec(decodedKeyString); var _key = keyFactory.generatePrivate(keySpec); @@ -114,7 +117,7 @@ public class EcKeyLoader implements KeyLoader { .replaceAll("-----BEGIN (EC )?PUBLIC KEY-----", "") .replaceAll("-----END (EC )?PUBLIC KEY-----", "") .replaceAll("\n", ""); - var keyBytes = Base64.getDecoder().decode(pemKeyText); + var keyBytes = decoder.decode(pemKeyText); var spec = new X509EncodedKeySpec(keyBytes); var key = keyFactory.generatePublic(spec); if (key instanceof ECPublicKey publicKey) { From d535bd165f8c8af2fad6428d6865509eae91c63d Mon Sep 17 00:00:00 2001 From: zihluwang Date: Sat, 15 Feb 2025 12:23:22 +0800 Subject: [PATCH 2/2] docs: optimised docs --- README.md | 6 ++ devkit-core/README.md | 10 +- devkit-utils/README.md | 56 ++--------- guid/README.md | 79 +++++---------- map-util-unsafe/README.md | 5 + num4j/README.md | 3 + property-guard-spring-boot-starter/README.md | 100 ++++--------------- 7 files changed, 68 insertions(+), 191 deletions(-) create mode 100644 map-util-unsafe/README.md create mode 100644 num4j/README.md diff --git a/README.md b/README.md index 5ae6695..4bc163e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,11 @@ # JDevKit +![Static Badge](https://img.shields.io/badge/tag-v2.0.0-orange) +![Static Badge](https://img.shields.io/badge/maven_central-v2.0.0-orange) +![Static Badge](https://img.shields.io/badge/licence-Apache_2.0-green) +![Static Badge](https://img.shields.io/badge/JDK-%E2%89%A517-blue) + + JDevKit is a Java Development Kit that offers a set of convenient tools for writing code efficiently. ## Installation and Usage diff --git a/devkit-core/README.md b/devkit-core/README.md index abf15a6..c38ceb3 100644 --- a/devkit-core/README.md +++ b/devkit-core/README.md @@ -2,12 +2,4 @@ ## Introduction -This module is the main part of `JDevKit`, an open-source Java class library that provides a set of convenient tools to streamline code development and enhance productivity. This module serves as the basement of other modules. - -## Prerequisites - -This whole `JDevKit` is developed by **JDK 17**, which means you have to use JDK 17 for better experience. - -## Installation - -You don't have to install this module at all, any module which is from `JDevKit` contains this `devkit-core` module. \ No newline at end of file +This module serves as the basement of `JDevKit`. It provides some base exceptions that `JDevKit` might use. \ No newline at end of file diff --git a/devkit-utils/README.md b/devkit-utils/README.md index c264916..6d2b02f 100644 --- a/devkit-utils/README.md +++ b/devkit-utils/README.md @@ -2,53 +2,15 @@ ## Introduction -This module is part of `JDevKit`, an open-source Java Development Kit that provides a set of -convenient tools to streamline code development and enhance productivity. This module serves as the -useful toolkit for the library, contains a collection of utility classes commonly used in all Java -Application development. +This module provides a set of utilities to streamline Java codes. -## Prerequisites +## Features -This whole `JDevKit` is developed by **JDK 17**, which means you have to use JDK 17 for -better experience. +- AES encryption and decryption; +- Base64 encode and decode; +- Boolean calculation; +- Reduce `if...else...` with **lambdas**; +- Hash calculation for strings; +- Convert Java beans to map and map to Java beans; +- Simplified range generator. -## Installation - -### If you are using `Maven` - -It is quite simple to install this module by `Maven`. The only thing you need to do is find your -`pom.xml` file in the project, then find the `` node in the `` node, and add -the following codes to `` node: - -```xml - - com.onixbyte - devkit-utils - ${devkit-utils.version} - -``` - -And run `mvn dependency:get` in your project root folder(i.e., if your `pom.xml` is located at -`/path/to/your/project/pom.xml`, then your current work folder should be `/path/to/your/project`), -then `Maven` will automatically download the `jar` archive from `Maven Central Repository`. This -could be **MUCH EASIER** if you are using IDE(i.e., IntelliJ IDEA), the only thing you need to do -is click the refresh button of `Maven`. - -If you are restricted using the Internet, and have to make `Maven` offline, you could follow the -following steps. - -1. Download the `jar` file from any place you can get and transfer the `jar` files to your - work computer. -2. Move the `jar` files to your local `Maven` Repository as the path of - `/path/to/maven_local_repo/com/onixbyte/devkit-utils/`. - -### If you are using `Gradle` - -Add this module to your project with `Gradle` is much easier than doing so with `Maven`. - -Find `build.gradle` in the needed project, and add the following code to the `dependencies` closure -in the build script: - -```groovy -implementation 'com.onixbyte:devkit-utils:${devkit-utils.version}' -``` diff --git a/guid/README.md b/guid/README.md index c4b5efd..501a1f5 100644 --- a/guid/README.md +++ b/guid/README.md @@ -2,68 +2,37 @@ ## Introduction -Module `guid` is a library that provides utilities for generating and working with globally unique -identifiers (GUIDs). GUIDs are globally unique across all devices and systems, making them ideal -for various use cases like database record keys, distributed systems, and tracking objects. +Module `guid` serves as a guid creator for other `JDevKit` modules. You can also use this module as a guid creator standards. -The `guid` module offers a reliable and efficient GUID generator, allowing developers to create -unique identifiers with low collision probability. It also provides features to customize the -format of generated GUIDs, making it flexible and suitable for different applications. +We have already implemented `SnowflakeGuidCreator`, you can also implement a custom guid creations by implementing `com.onixbyte.guid.GuidCreator`. -With `guid`, developers can easily integrate globally unique identifiers into their projects, -ensuring data integrity, avoiding duplicates, and simplifying the identification of objects across -various systems. The module is designed to be simple to use, highly performant, and compatible with -different programming languages and frameworks. +## Example usage -## Prerequisites +### A UUID creator -This whole `JDevKit` is developed by **JDK 17**, which means you have to use JDK 17 for -better experience. - -## Installation - -### If you are using `Maven` - -It is quite simple to install this module by `Maven`. The only thing you need to do is find your -`pom.xml` file in the project, then find the `` node in the `` node, and add -the following codes to `` node: - -```xml - - com.onixbyte - devkit-utils - ${devkit-utils.version} - +```java +GuidCreator uuidCreator = (GuidCreator) UUID::randomUUID; ``` -And run `mvn dependency:get` in your project root folder(i.e., if your `pom.xml` is located at -`/path/to/your/project/pom.xml`, then your current work folder should be `/path/to/your/project`), -then `Maven` will automatically download the `jar` archive from `Maven Central Repository`. This -could be **MUCH EASIER** if you are using IDE(i.e., IntelliJ IDEA), the only thing you need to do -is click the refresh button of `Maven`. +### A custom guid creator -If you are restricted using the Internet, and have to make `Maven` offline, you could follow the -following steps. +Assume that you need serial guid creator. -1. Download the `jar` file from any place you can get and transfer the `jar` files to your work - computer. -2. Move the `jar` files to your local `Maven` Repository as the path of - `/path/to/maven_local_repo/com/onixbyte/devkit-utils/`. - -### If you are using `Gradle` - -Add this module to your project with `Gradle` is much easier than doing so with `Maven`. - -Find `build.gradle` in the needed project, and add the following code to the `dependencies` closure -in the build script: - -```groovy -implementation 'com.onixbyte:guid:${guid.version}' +```java +@Component +public class CustomGuidCreator implementes GuidCreator { + + public final RedisTemplate serialRedisTemplate; + + @Autowired + public CustomGuidCreator(RedisTemplate serialRedisTemplate) { + this.serialRedisTemplate = serialRedisTemplate; + } + + @Override public String nextId() { + return "SOME_PREFIX" + serialRedisTemplate.opsForValue().get("some_serial_key"); + } + +} ``` -### If you are not using `Maven` or `Gradle` - -1. Download the `jar` file from the Internet. -2. Create a folder in your project and name it as a name you like(i.e., for me, I prefer `vendor`). -3. Put the `jar` file to the folder you just created in Step 2. -4. Add this folder to your project `classpath`. diff --git a/map-util-unsafe/README.md b/map-util-unsafe/README.md new file mode 100644 index 0000000..e5de9cb --- /dev/null +++ b/map-util-unsafe/README.md @@ -0,0 +1,5 @@ +# Map Util Unsafe + +`map-util-unsafe` provides a set of more convenient utilities for converting Java bean to Map or Map to Java bean, but which are less safe than the `MapUtil` provided in `devkit-utils`. + +This `MapUtil` is implemented with Reflect API, which might be removed in later JDKs. \ No newline at end of file diff --git a/num4j/README.md b/num4j/README.md new file mode 100644 index 0000000..7745ec1 --- /dev/null +++ b/num4j/README.md @@ -0,0 +1,3 @@ +# Num4j + +`num4j` provides some mathematical algorithms and utilities such as chained high-precision mathematical calculator and percentile statistic algorithm. \ No newline at end of file diff --git a/property-guard-spring-boot-starter/README.md b/property-guard-spring-boot-starter/README.md index 51e9e52..32cc31f 100644 --- a/property-guard-spring-boot-starter/README.md +++ b/property-guard-spring-boot-starter/README.md @@ -1,97 +1,37 @@ # Property Guard -## Introduction +`property-guard-spring-boot-starter` is a utility that can help you protect secret values in Spring Boot configurations. -This feature is designed to protect the security of configurations and data, to a certain extent, to control the flow of developers leading to the leakage of sensitive information. +## Example usage -## Prerequisites +### 1. Implementation this module -This whole `JDevKit` is developed by **JDK 17**, which means you have to use JDK 17 for better experience. Except this, this module is designed for Spring Boot framework, so you have to install Spring Boot (v3) in your application. - -## Installation - -### If you are using `Maven` - -It is quite simple to install this module by `Maven`. The only thing you need to do is find your `pom.xml` file in the project, then find the `` node in the `` node, and add the following codes to `` node: - -```xml - - cn.org.codecrafters - property-guard-spring-boot-starter - ${property-guard-spring-boot-starter.version} - +```kotlin +dependencies { + implementation(platform("com.onixbyte:devkit-bom:$devKitVersion")) + implementation("com.onixbyte:devkit-utils") + implementation("com.onixbyte:property-guard-spring-boot-starter") +} ``` -And run `mvn dependency:get` in your project root folder(i.e., if your `pom.xml` is located at `/path/to/your/project/pom.xml`, then your current work folder should be `/path/to/your/project`), then `Maven` will automatically download the `jar` archive from `Maven Central Repository`. This could be **MUCH EASIER** if you are using IDE(i.e., IntelliJ IDEA), the only thing you need to do is click the refresh button of `Maven`. +### 2. Generate a secret -If you are restricted using the Internet, and have to make `Maven` offline, you could follow the following steps. +Use the following codes to get a random secret. -1. Download the `jar` file from any place you can get and transfer the `jar` files to your work computer. -2. Move the `jar` files to your local `Maven` Repository as the path of `/path/to/maven_local_repo/cn/org/codecrafters/property-guard-spring-boot-starter/`. - -### If you are using `Gradle` - -Add this module to your project with `Gradle` is much easier than doing so with `Maven`. - -Find `build.gradle` in the needed project, and add the following code to the `dependencies` closure in the build script: - -```groovy -implementation 'cn.org.codecrafters:property-guard-spring-boot-starter:${property-guard-spring-boot-starter.version}' -``` - -### If you are not using `Maven` or `Gradle` - -1. Download the `jar` file from the Internet. -2. Create a folder in your project and name it as a name you like(i.e., for me, I prefer `vendor`). -3. Put the `jar` file to the folder you just created in Step 2. -4. Add this folder to your project `classpath`. - -## Usage - -First, you need a 16-bit-long secret. If you don't have a good way to get a secret, you could consider using our `utils.com.onixbyte.devkit.AesUtil` or `com.onixbyte.simplejwt.SecretCreator` to create a secret. - -For example: ```java -import utils.com.onixbyte.devkit.AesUtil; -import com.onixbyte.simplejwt.SecretCreator; - -class GenerateRandomKeySample { - public static void main(String[] args) { - var secret1 = AesUtil.generateRandomSecret(); - var secret2 = SecretCreator.createSecret(16, true, true, true); +@SpringBootTest +class SpringBootApplicationTest { + + @Test + void contextLoads() { + System.out.println(AesUtil.generateRandomSecret()); // Output: a 16-char long secret } } ``` -Then, remember this secret and encrypt the configuration properties that are required high security. For example: +Or you can write a 16-char long secret by yourself. -```java -import utils.com.onixbyte.devkit.AesUtil; +### 3. Encrypt your secret properties and place them into your configuration file -class EncryptSample { - public static void main(String[] args) { - var dataNeedEncryption = "Sample Value"; - var key = "3856faef0d2d4f33"; - var encryptedData = AesUtil.encrypt(dataNeedEncryption, key); - } -} -``` +### 4. Run application with parameter `--pg.key=$your_secret` -After that, copy the encrypted data to `application.properties` or `application.yml`. - -For `yml`: -```yaml -app: - sample-configuration: pe:t4YBfv8M9ZmTzWgTi2gJqg== # "pe:" is the prefix that declare that this property is encrypted. -``` - -For `properties`: -```properties -app.sample-configuration=pe:t4YBfv8M9ZmTzWgTi2gJqg== -``` - -## Contact - -If you have any suggestions, ideas, don't hesitate contacting us via [GitHub Issues](https://github.com/CodeCraftersCN/jdevkit/issues/new) or [Discord Community](https://discord.gg/NQK9tjcBB8). - -If you face any bugs while using our library and you are able to fix any bugs in our library, we would be happy to accept pull requests from you on [GitHub](https://github.com/CodeCraftersCN/jdevkit/compare). \ No newline at end of file