Skip to content

Commit

Permalink
Merge branch 'main' into test-pgp
Browse files Browse the repository at this point in the history
# Conflicts:
#	pom.xml
  • Loading branch information
reza1057 committed Nov 26, 2023
2 parents 944e0f9 + b9a44c7 commit a5c339f
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 33 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,24 @@ 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
<groupId>com.tosan.tools</groupId>
<groupId>com.tosan.tools</groupId>
<artifactId>tosan-tostring-builder</artifactId>
<version>${version}</version>
```

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();
}
```
Expand Down
37 changes: 18 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<artifactId>tosan-tostring-builder</artifactId>
<version>1.0.5-SNAPSHOT</version>


<name>tosan-tostring-builder</name>
<description>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.
Expand All @@ -16,8 +15,8 @@

<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<name>The Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>

Expand All @@ -26,13 +25,13 @@
<name>Mostafa Abdollahi</name>
<email>[email protected]</email>
<organization>tosan</organization>
<organizationUrl>http://www.tosan.com</organizationUrl>
<organizationUrl>https://www.tosan.com</organizationUrl>
</developer>
<developer>
<name>Reza Mortezaei</name>
<email>[email protected]</email>
<organization>tosan</organization>
<organizationUrl>http://www.tosan.com</organizationUrl>
<organizationUrl>https://www.tosan.com</organizationUrl>
</developer>
</developers>

Expand All @@ -46,7 +45,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>8</java.version>
<jackson.version>2.13.1</jackson.version>
<jackson.version>2.15.3</jackson.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -83,7 +82,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.1</version>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -94,7 +93,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<version>3.0.1</version>
<configuration>
<tagNameFormat>v@{project.version}</tagNameFormat>
<releaseProfiles>release</releaseProfiles>
Expand All @@ -103,7 +102,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<version>3.3.0</version>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -116,9 +115,10 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<version>3.5.0</version>
<configuration>
<detectJavaApiLink>false</detectJavaApiLink>
<doclint>none</doclint>
</configuration>
<executions>
<execution>
Expand All @@ -132,29 +132,28 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<version>3.3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<version>3.11.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<showWarnings>true</showWarnings>
<fork>true</fork>
<optimize>true</optimize>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<version>3.3.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<version>3.1.2</version>
<configuration>
<includes>
<include>**/*UTest.java</include>
Expand All @@ -164,12 +163,12 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<version>3.1.1</version>
</plugin>
</plugins>
</pluginManagement>
Expand Down Expand Up @@ -203,7 +202,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<version>3.1.0</version>
<executions>
<execution>
<id>sign-artifacts</id>
Expand All @@ -223,7 +222,7 @@
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.12</version>
<version>1.6.13</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/tosan/tools/tostring/ToStringBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
35 changes: 30 additions & 5 deletions src/main/java/com/tosan/tools/util/EncryptStringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
}

0 comments on commit a5c339f

Please sign in to comment.