import org.apache.commons.lang.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EncryptUtil {
public static final String MOBILE_REG = "^\\d{11}$";
public static final String EMAIL_REG = "@+";
public static String mobileEncrypt(String mobile) {
if (StringUtils.isEmpty(mobile) || (mobile.length() != 11)) {
return mobile;
}
return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
}
public static String emailEncrypt(String email) {
if (StringUtils.isEmpty(email)) {
return email;
}
String encrypt = email.replaceAll("(\\w+)\\w{3}@(\\w+)", "$1***@$2");
if(StringUtils.equalsIgnoreCase(email, encrypt)){
encrypt = email.replaceAll("(\\w*)\\w{1}@(\\w+)", "$1*@$2");
}
return encrypt;
}
public static String fieldEncrypt(String field) {
if (StringUtils.isEmpty(field)) {
return field;
}
String encrypt = field.replaceAll("(\\w+)\\w{3}", "$1***");
if(StringUtils.equalsIgnoreCase(field, encrypt)){
encrypt = field.replaceAll("(\\w*)\\w{1}", "$1*");
}
return encrypt;
}
public static String idEncrypt(String id) {
if (StringUtils.isEmpty(id) || (id.length() < 8)) {
return id;
}
return id.replaceAll("(?<=\\w{3})\\w(?=\\w{4})", "*");
}
public static String idPassport(String id) {
if (StringUtils.isEmpty(id) || (id.length() < 8)) {
return id;
}
return id.substring(0, 2) + new String(new char[id.length() - 5]).replace("\0", "*") + id.substring(id.length() - 3);
}
public static String alipayAccountEncrypt(String account) {
if (StringUtils.isEmpty(account)) {
return account;
}
Pattern pattern = Pattern.compile(MOBILE_REG);
Matcher matcher = pattern.matcher(account);
if(matcher.find()){
account = EncryptUtil.mobileEncrypt(account);
return account;
} else {
pattern = Pattern.compile(EMAIL_REG);
matcher = pattern.matcher(account);
if (matcher.find()) {
account = EncryptUtil.emailEncrypt(account);
return account;
} else {
account = EncryptUtil.fieldEncrypt(account);
return account;
}
}
}
}