From 472e2a016eca7c77e6e1acac408bbdf264e3fd66 Mon Sep 17 00:00:00 2001 From: mosidev Date: Sun, 26 Nov 2023 00:10:12 +0330 Subject: [PATCH 1/3] [feature] add middle encrypt capability to ToString methods --- README.md | 12 +++---- pom.xml | 35 +++++++++---------- .../tostring/JsonToStringBuilderImpl.java | 10 ++++-- .../tostring/SimpleToStringBuilderImpl.java | 15 ++++++++ .../tosan/tools/tostring/ToStringBuilder.java | 7 +++- .../tools/tostring/ToStringBuilderImpl.java | 5 +++ .../tosan/tools/util/EncryptStringUtil.java | 35 ++++++++++++++++--- 7 files changed, 87 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 5024ec1..6f07678 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ simple and json formatting. also, provides interface for use in aspect to log se For start, you should add the following dependency to your pom: ```xml - com.tosan.tools +com.tosan.tools tosan-tostring-builder ${version} ``` @@ -52,16 +52,16 @@ For start, you should add the following dependency to your pom: for use in toString method you can code like this: ```java - @Override +@Override public String toString() { final ToStringBuilder sb = new ToStringBuilderImpl(this); sb.append("superClass", super.toString()); - sb.semiEncryptedAppend("code", sMaskedCode); sb.append("name", name); + sb.semiEncryptedAppend("code", code); sb.encryptedAppend("password", address); - sb.append("tel", tel); - sb.append("cityName", cityName); - sb.leftEncryptedAppend("cityCode", LMaskedCityCode); + sb.middleEncryptedAppend("mobile", mobile); + sb.rightEncryptedAppend("address", address); + sb.leftEncryptedAppend("cityCode", cityCode); return sb.toString(); } ``` diff --git a/pom.xml b/pom.xml index 72b1254..b3a1573 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,6 @@ tosan-tostring-builder 1.0.5-SNAPSHOT - tosan-tostring-builder Library provides interface for use in toString method and determine each field how should be shown. also, you can choose from two implementations provided for simple and json formatting. @@ -17,7 +16,7 @@ The Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt + https://www.apache.org/licenses/LICENSE-2.0.txt @@ -26,13 +25,13 @@ Mostafa Abdollahi abdollahi@tosan.com tosan - http://www.tosan.com + https://www.tosan.com Reza Mortezaei mortezaei@tosan.com tosan - http://www.tosan.com + https://www.tosan.com @@ -46,7 +45,7 @@ UTF-8 8 - 2.13.1 + 2.15.3 @@ -83,7 +82,7 @@ org.junit.jupiter junit-jupiter-api - 5.7.1 + 5.9.3 test @@ -94,7 +93,7 @@ org.apache.maven.plugins maven-release-plugin - 2.5.3 + 3.0.1 v@{project.version} release @@ -103,7 +102,7 @@ org.apache.maven.plugins maven-source-plugin - 3.2.1 + 3.3.0 attach-sources @@ -116,9 +115,10 @@ org.apache.maven.plugins maven-javadoc-plugin - 3.2.0 + 3.5.0 false + none @@ -132,29 +132,28 @@ org.apache.maven.plugins maven-resources-plugin - 3.2.0 + 3.3.1 org.apache.maven.plugins maven-compiler-plugin - 3.8.1 + 3.11.0 ${java.version} ${java.version} true true - true org.apache.maven.plugins maven-jar-plugin - 3.2.0 + 3.3.0 org.apache.maven.plugins maven-surefire-plugin - 2.22.2 + 3.1.2 **/*UTest.java @@ -164,12 +163,12 @@ org.apache.maven.plugins maven-install-plugin - 2.5.2 + 3.1.1 org.apache.maven.plugins maven-deploy-plugin - 2.8.2 + 3.1.1 @@ -203,7 +202,7 @@ org.apache.maven.plugins maven-gpg-plugin - 1.6 + 3.1.0 sign-artifacts @@ -223,7 +222,7 @@ org.sonatype.plugins nexus-staging-maven-plugin - 1.6.12 + 1.6.13 true ossrh diff --git a/src/main/java/com/tosan/tools/tostring/JsonToStringBuilderImpl.java b/src/main/java/com/tosan/tools/tostring/JsonToStringBuilderImpl.java index baf50e6..5099e07 100644 --- a/src/main/java/com/tosan/tools/tostring/JsonToStringBuilderImpl.java +++ b/src/main/java/com/tosan/tools/tostring/JsonToStringBuilderImpl.java @@ -9,7 +9,7 @@ import java.util.*; -import static com.tosan.tools.util.ToStringConstant.*; +import static com.tosan.tools.util.ToStringConstant.ERROR_TEXT; /** * This implementation provides json format for tostring @@ -87,7 +87,7 @@ public final ToStringBuilder append(String key, Object obj) { } else if (obj instanceof Map) { Map map = (Map) obj; this.text.put(key, map); - } else { + } else { if (obj.toString().startsWith("{")) this.text.put(key, new RawValue(obj.toString())); else @@ -147,6 +147,12 @@ public ToStringBuilder semiEncryptedAppend(String key, Object obj, int maskLengt return this; } + @Override + public ToStringBuilder middleEncryptedAppend(String key, Object obj) { + this.text.put(key, EncryptStringUtil.middleEncrypt(obj)); + return this; + } + @Override public final String toString() { return ToStringJsonUtil.toJson(this.text); diff --git a/src/main/java/com/tosan/tools/tostring/SimpleToStringBuilderImpl.java b/src/main/java/com/tosan/tools/tostring/SimpleToStringBuilderImpl.java index 9b25975..50bc23a 100644 --- a/src/main/java/com/tosan/tools/tostring/SimpleToStringBuilderImpl.java +++ b/src/main/java/com/tosan/tools/tostring/SimpleToStringBuilderImpl.java @@ -225,6 +225,21 @@ public ToStringBuilder semiEncryptedAppend(String key, Object obj, int maskLengt return this; } + @Override + public ToStringBuilder middleEncryptedAppend(String key, Object obj) { + this.text.append(SPACE); + this.text.append(key); + this.text.append(EQUAL); + if (obj != null) { + this.text.append(EncryptStringUtil.middleEncrypt(obj)); + this.text.append(ENTER); + } else { + this.text.append(NULL_TEXT); + } + this.text.append(ENTER); + return this; + } + @Override public final String toString() { StringBuilder sb = new StringBuilder(L_BRACKET); diff --git a/src/main/java/com/tosan/tools/tostring/ToStringBuilder.java b/src/main/java/com/tosan/tools/tostring/ToStringBuilder.java index 3f54b48..fe3d028 100644 --- a/src/main/java/com/tosan/tools/tostring/ToStringBuilder.java +++ b/src/main/java/com/tosan/tools/tostring/ToStringBuilder.java @@ -10,7 +10,7 @@ public interface ToStringBuilder { /** - * @param name field name + * @param name field name * @param value field value */ ToStringBuilder append(String name, Object value); @@ -47,4 +47,9 @@ public interface ToStringBuilder { * it will mask maskLength chars from right part of value. */ ToStringBuilder semiEncryptedAppend(String name, Object value, int maskLength); + + /** + * this method will mask middle part of any vale. + */ + ToStringBuilder middleEncryptedAppend(String name, Object value); } diff --git a/src/main/java/com/tosan/tools/tostring/ToStringBuilderImpl.java b/src/main/java/com/tosan/tools/tostring/ToStringBuilderImpl.java index 0a9765e..f55ebdc 100644 --- a/src/main/java/com/tosan/tools/tostring/ToStringBuilderImpl.java +++ b/src/main/java/com/tosan/tools/tostring/ToStringBuilderImpl.java @@ -79,6 +79,11 @@ public ToStringBuilder semiEncryptedAppend(String key, Object obj, int maskLengt return builder.semiEncryptedAppend(key, obj, maskLength); } + @Override + public ToStringBuilder middleEncryptedAppend(String key, Object obj) { + return builder.middleEncryptedAppend(key, obj); + } + @Override public String toString() { return builder.toString(); diff --git a/src/main/java/com/tosan/tools/util/EncryptStringUtil.java b/src/main/java/com/tosan/tools/util/EncryptStringUtil.java index 30eb8a3..e63669d 100644 --- a/src/main/java/com/tosan/tools/util/EncryptStringUtil.java +++ b/src/main/java/com/tosan/tools/util/EncryptStringUtil.java @@ -105,18 +105,35 @@ public static String panEncrypt(Object obj) { if (obj != null) { str = objToString(obj); if (str.length() == 16) { - str = SEMI_ENCRYPTED.concat(str.substring(0, 6)).concat("******").concat(str.substring(12, 16)); + str = str.substring(0, 6).concat("******").concat(str.substring(12, 16)); } else if (str.length() == 19) { - str = SEMI_ENCRYPTED.concat(str.substring(0, 6)).concat("*********").concat(str.substring(15, 19)); - } else if (str.length() >= 2) { - str = SEMI_ENCRYPTED.concat(str.substring(0, str.length() / 2)); + str = str.substring(0, 6).concat("*********").concat(str.substring(15, 19)); } else { - str = ENCRYPTED; + str = middleEncrypt(str); } } return str; } + public static String middleEncrypt(Object obj) { + String str; + try { + str = objToString(obj); + if (str == null || str.isEmpty()) { + return str; + } else if (str.length() <= 3) { + return ENCRYPTED; + } else { + int partLength = str.length() / 3; + return str.substring(0, partLength) + generateStar(str.length() - partLength * 2) + + str.substring(str.length() - partLength); + } + } catch (Exception e) { + str = ERROR_TEXT; + } + return str; + } + private static String objToString(Object obj) { if (obj != null) { if (obj instanceof String) { @@ -129,4 +146,12 @@ private static String objToString(Object obj) { } return null; } + + private static String generateStar(int length) { + StringBuilder stars = new StringBuilder(length); + for (int i = 0; i < length; i++) { + stars.append("*"); + } + return stars.toString(); + } } From 3936043c1486699c5b208ee1db73530037fa065e Mon Sep 17 00:00:00 2001 From: mosidev Date: Sun, 26 Nov 2023 00:21:24 +0330 Subject: [PATCH 2/3] [enhancement] build with java 8 in actions --- .github/workflows/build.yml | 43 ++++++++++++++++------------------- .github/workflows/release.yml | 42 +++++++++++++++++----------------- pom.xml | 2 +- 3 files changed, 42 insertions(+), 45 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 54f1f37..50149b0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,6 +1,3 @@ -# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time -# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven - name: Build and deploy per push on: @@ -15,31 +12,31 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Set up JDK 11 - uses: actions/setup-java@v3 - with: - java-version: 11 - distribution: temurin - cache: maven - server-id: ossrh - server-username: MAVEN_USERNAME - server-password: MAVEN_PASSWORD - gpg-private-key: ${{ secrets.OSSRH_GPG_SECRET_KEY }} - gpg-passphrase: MAVEN_GPG_PASSPHRASE - - name: Configure Git user - run: | + - uses: actions/checkout@v3 + - name: Set up JDK 8 + uses: actions/setup-java@v3 + with: + java-version: 8 + distribution: temurin + cache: maven + server-id: ossrh + server-username: MAVEN_USERNAME + server-password: MAVEN_PASSWORD + gpg-private-key: ${{ secrets.OSSRH_GPG_SECRET_KEY }} + gpg-passphrase: MAVEN_GPG_PASSPHRASE + - name: Configure Git user + run: | git config user.email "actions@github.com" git config user.name "GitHub Actions" - - name: Build with Maven - run: mvn -B package --file pom.xml - - name: Publish to the Maven Central Repository - run: | + - name: Build with Maven + run: mvn -B package --file pom.xml + - name: Publish to the Maven Central Repository + run: | mvn \ --no-transfer-progress \ --batch-mode \ deploy -Pbuild - env: + env: MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} - MAVEN_GPG_PASSPHRASE: ${{ secrets.OSSRH_GPG_SECRET_KEY_PASSWORD }} + MAVEN_GPG_PASSPHRASE: ${{ secrets.OSSRH_GPG_SECRET_KEY_PASSWORD }} \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0f15676..a0bffb3 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -21,7 +21,7 @@ jobs: - name: Setup Java JDK uses: actions/setup-java@v3 with: - java-version: 11 + java-version: 8 distribution: temurin cache: maven server-id: github @@ -37,31 +37,31 @@ jobs: needs: release runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - with: - ref: v${{ github.event.inputs.releaseVersion }} - - name: Set up JDK 11 - uses: actions/setup-java@v3 - with: - java-version: 11 - distribution: temurin - cache: maven - server-id: ossrh - server-username: MAVEN_USERNAME - server-password: MAVEN_PASSWORD - gpg-private-key: ${{ secrets.OSSRH_GPG_SECRET_KEY }} - gpg-passphrase: MAVEN_GPG_PASSPHRASE - - name: Configure Git user - run: | + - uses: actions/checkout@v3 + with: + ref: v${{ github.event.inputs.releaseVersion }} + - name: Set up JDK 8 + uses: actions/setup-java@v3 + with: + java-version: 8 + distribution: temurin + cache: maven + server-id: ossrh + server-username: MAVEN_USERNAME + server-password: MAVEN_PASSWORD + gpg-private-key: ${{ secrets.OSSRH_GPG_SECRET_KEY }} + gpg-passphrase: MAVEN_GPG_PASSPHRASE + - name: Configure Git user + run: | git config user.email "actions@github.com" git config user.name "GitHub Actions" - - name: Release and publish to the Maven Central Repository - run: | + - name: Release and publish to the Maven Central Repository + run: | mvn \ --no-transfer-progress \ --batch-mode \ deploy -Pbuild - env: + env: MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} - MAVEN_GPG_PASSPHRASE: ${{ secrets.OSSRH_GPG_SECRET_KEY_PASSWORD }} + MAVEN_GPG_PASSPHRASE: ${{ secrets.OSSRH_GPG_SECRET_KEY_PASSWORD }} \ No newline at end of file diff --git a/pom.xml b/pom.xml index b3a1573..87aa493 100644 --- a/pom.xml +++ b/pom.xml @@ -202,7 +202,7 @@ org.apache.maven.plugins maven-gpg-plugin - 3.1.0 + 1.6 sign-artifacts From b9a44c71c103c9ed1993b8eba4e1fd7ea91340ba Mon Sep 17 00:00:00 2001 From: reza1057 Date: Sun, 26 Nov 2023 15:10:00 +0330 Subject: [PATCH 3/3] Revert "[enhancement] build with java 8 in actions" This reverts commit 3936043c1486699c5b208ee1db73530037fa065e. --- .github/workflows/build.yml | 43 +++++++++++++++++++---------------- .github/workflows/release.yml | 42 +++++++++++++++++----------------- pom.xml | 2 +- 3 files changed, 45 insertions(+), 42 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 50149b0..54f1f37 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,3 +1,6 @@ +# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven + name: Build and deploy per push on: @@ -12,31 +15,31 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Set up JDK 8 - uses: actions/setup-java@v3 - with: - java-version: 8 - distribution: temurin - cache: maven - server-id: ossrh - server-username: MAVEN_USERNAME - server-password: MAVEN_PASSWORD - gpg-private-key: ${{ secrets.OSSRH_GPG_SECRET_KEY }} - gpg-passphrase: MAVEN_GPG_PASSPHRASE - - name: Configure Git user - run: | + - uses: actions/checkout@v3 + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: 11 + distribution: temurin + cache: maven + server-id: ossrh + server-username: MAVEN_USERNAME + server-password: MAVEN_PASSWORD + gpg-private-key: ${{ secrets.OSSRH_GPG_SECRET_KEY }} + gpg-passphrase: MAVEN_GPG_PASSPHRASE + - name: Configure Git user + run: | git config user.email "actions@github.com" git config user.name "GitHub Actions" - - name: Build with Maven - run: mvn -B package --file pom.xml - - name: Publish to the Maven Central Repository - run: | + - name: Build with Maven + run: mvn -B package --file pom.xml + - name: Publish to the Maven Central Repository + run: | mvn \ --no-transfer-progress \ --batch-mode \ deploy -Pbuild - env: + env: MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} - MAVEN_GPG_PASSPHRASE: ${{ secrets.OSSRH_GPG_SECRET_KEY_PASSWORD }} \ No newline at end of file + MAVEN_GPG_PASSPHRASE: ${{ secrets.OSSRH_GPG_SECRET_KEY_PASSWORD }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a0bffb3..0f15676 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -21,7 +21,7 @@ jobs: - name: Setup Java JDK uses: actions/setup-java@v3 with: - java-version: 8 + java-version: 11 distribution: temurin cache: maven server-id: github @@ -37,31 +37,31 @@ jobs: needs: release runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - with: - ref: v${{ github.event.inputs.releaseVersion }} - - name: Set up JDK 8 - uses: actions/setup-java@v3 - with: - java-version: 8 - distribution: temurin - cache: maven - server-id: ossrh - server-username: MAVEN_USERNAME - server-password: MAVEN_PASSWORD - gpg-private-key: ${{ secrets.OSSRH_GPG_SECRET_KEY }} - gpg-passphrase: MAVEN_GPG_PASSPHRASE - - name: Configure Git user - run: | + - uses: actions/checkout@v3 + with: + ref: v${{ github.event.inputs.releaseVersion }} + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: 11 + distribution: temurin + cache: maven + server-id: ossrh + server-username: MAVEN_USERNAME + server-password: MAVEN_PASSWORD + gpg-private-key: ${{ secrets.OSSRH_GPG_SECRET_KEY }} + gpg-passphrase: MAVEN_GPG_PASSPHRASE + - name: Configure Git user + run: | git config user.email "actions@github.com" git config user.name "GitHub Actions" - - name: Release and publish to the Maven Central Repository - run: | + - name: Release and publish to the Maven Central Repository + run: | mvn \ --no-transfer-progress \ --batch-mode \ deploy -Pbuild - env: + env: MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} - MAVEN_GPG_PASSPHRASE: ${{ secrets.OSSRH_GPG_SECRET_KEY_PASSWORD }} \ No newline at end of file + MAVEN_GPG_PASSPHRASE: ${{ secrets.OSSRH_GPG_SECRET_KEY_PASSWORD }} diff --git a/pom.xml b/pom.xml index 87aa493..b3a1573 100644 --- a/pom.xml +++ b/pom.xml @@ -202,7 +202,7 @@ org.apache.maven.plugins maven-gpg-plugin - 1.6 + 3.1.0 sign-artifacts