Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] add mobile encryption #3

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ public final ToStringBuilder rightEncryptedAppend(String key, Object obj) {
return this;
}

@Override
public ToStringBuilder mobileEncryptedAppend(String key, Object obj) {
this.text.put(key, EncryptStringUtil.mobileEncrypt(obj));
return this;
}

@Override
public final ToStringBuilder encryptedAppend(String key, Object obj) {
this.text.put(key, EncryptStringUtil.encrypt(obj));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,21 @@ public ToStringBuilder panEncryptedAppend(String key, Object obj) {
return this;
}

@Override
public ToStringBuilder mobileEncryptedAppend(String key, Object obj) {
this.text.append(SPACE);
this.text.append(key);
this.text.append(EQUAL);
if (obj != null) {
this.text.append(EncryptStringUtil.mobileEncrypt(obj));
this.text.append(ENTER);
} else {
this.text.append(NULL_TEXT);
}
this.text.append(ENTER);
return this;
}

@Override
public ToStringBuilder semiEncryptedAppend(String key, Object obj) {
return semiEncryptedAppend(key, obj, 5);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/tosan/tools/tostring/ToStringBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public interface ToStringBuilder {
*/
ToStringBuilder rightEncryptedAppend(String name, Object value);

/**
* calling this method leads to showing mobile number and only shows begin and end of mobile number value
*/
ToStringBuilder mobileEncryptedAppend(String name,Object value);

/**
* this method will remove value
* use this for fields such as account balance and password
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public ToStringBuilder rightEncryptedAppend(String key, Object obj) {
return builder.rightEncryptedAppend(key, obj);
}

@Override
public ToStringBuilder mobileEncryptedAppend(String key, Object obj) {
return builder.mobileEncryptedAppend(key, obj);
}

@Override
public ToStringBuilder encryptedAppend(String key, Object obj) {
return builder.encryptedAppend(key, obj);
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/tosan/tools/util/EncryptStringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ public static String panEncrypt(Object obj) {
return str;
}

public static String mobileEncrypt(Object obj) {
String str;
try {
str = objToString(obj);
if (str == null || str.isEmpty()) {
return str;
} else if (str.length() < 7) {
return ENCRYPTED;
} else {
return str.substring(0, str.length() - 7) + "***" +
str.substring(str.length() - 4);
}
} catch (Exception e) {
str = ERROR_TEXT;
}
return str;
}

public static String middleEncrypt(Object obj) {
String str;
try {
Expand Down
Loading