您的位置:首页 > 编程语言 > Java开发

Java高性能掩码类 - Override (隐藏敏感信息)

2017-08-19 13:44 519 查看

Java高性能掩码类 - Override (隐藏敏感信息)

#

为了防止敏感信息直接暴露,常常使用掩码对敏感信息进行隐藏

本文提供一个高性能的掩码类来实现该需求

/**
* Created by 谭健 on 2017/8/19. 10:05.
* © All Rights Reserved.
*/
public class Override {

/**
* 掩码缓存,当所需掩码数量在0-16范围之内的时候
* 直接使用缓存,不需要创建内存对象,效率最高
* <p>
* 如果不理解这里,可以参考JDK类库的 Integer 源码 sizeTable
*/
private static final String[] cache = {
"*", "**", "***", "****", "*****",
"******", "*******", "********", "*********",
"**********", "***********", "************", "*************",
"**************", "***************", "****************"};

private static final char coverDefault = '*';

/**
* 欲掩盖的字符串
*/
private String initialCode;
/**
* 掩码起始位置
*/
private int beginIndex;
/**
* 掩码结束位置
*/
private int endIndex;
/**
* 掩盖码,默认是 * 号
*/
private char cover = coverDefault;

/**
* 无参构造器
*/
public Override() {
}

/**
* 构造器
*
* @param beginIndex 初始掩码位置
* @param endIndex   结束掩码位置
*/
public Override(String initialCode, int beginIndex, int endIndex) {
this.initialCode = initialCode;
this.beginIndex = beginIndex;
this.endIndex = endIndex;
}

/**
* 构造器
*
* @param beginIndex 初始掩码位置
* @param endIndex   结束掩码位置
*/
public Override(String initialCode, int beginIndex, int endIndex, char cover) {
this.initialCode = initialCode;
this.beginIndex = beginIndex;
this.endIndex = endIndex;
this.cover = cover;
}

/**
* 提供四种静态方法(静态方法调用实例方法)
* 两种实例方法
*/
public static String getMaskSubWay(String initialCode, int beginIndex, int endIndex) {
Override override = new Override(initialCode, beginIndex, endIndex);
return override.getMaskSubWay();
}

public static String getMaskSubWay(String initialCode, int beginIndex, int endIndex, char cover) {
Override override = new Override(initialCode, beginIndex, endIndex, cover);
return override.getMaskSubWay();
}

public static String getMaskCharWay(String initialCode, int beginIndex, int endIndex, char cover) {
Override override = new Override(initialCode, beginIndex, endIndex, cover);
return override.getMaskCharWay();
}

public static String getMaskCharWay(String initialCode, int beginIndex, int endIndex) {
Override override = new Override(initialCode, beginIndex, endIndex);
return override.getMaskCharWay();
}

/**
* 通过substring方式截取
*
* @return 完成掩码的字符串
*/
public String getMaskSubWay() {
if (null == initialCode || "".equals(initialCode) || "null".equals(initialCode)) {
return "Error : variable is null. ";
}
return initialCode.substring(0, beginIndex - 1) +
cover(beginIndex, endIndex, cover) +
initialCode.substring(endIndex, initialCode.length());
}

/**
* 通过char[]方式截取
*
* @return 完成掩码的字符串
*/
public String getMaskCharWay() {
if (null == initialCode || "".equals(initialCode) || "null".equals(initialCode)) {
return "Error : variable is null. ";
}
char[] chars = initialCode.toCharArray();
char[] tempBegin = new char[beginIndex - 1];
char[] tempEnd = new char[initialCode.length() - endIndex];
for (int varBegin = 0; varBegin < tempBegin.length; varBegin++)
/**
* 把掩码字符串前半段提取出来,放到一个临时变量中
*/
tempBegin[varBegin] = chars[varBegin];
for (int varEnd = 0; varEnd < tempEnd.length; varEnd++)
/**
* 把掩码字符串后半段提取出来,放到一个临时变量中
*/
tempEnd[varEnd] = chars[endIndex + varEnd];
/**
* JDK系统自带的数组拷贝方法,效率要比for循环效率稍微低一些
*/
//  System.arraycopy(chars,0,tempBegin,0,tempBegin.length);
//  System.arraycopy(chars,0,tempEnd,0,tempEnd.length);
return new String(tempBegin) +
cover(beginIndex, endIndex, cover) +
new String(tempEnd);
}

/**
* 获取掩码
*
* @param beginIndex 起始掩码位置
* @param endIndex   结束掩码位置
* @return 一个掩码串
*/
private static String cover(int beginIndex, int endIndex, char cover) {
/**
* 这2个值不能为负数
*/
if (beginIndex < 0 || endIndex < 0)
return "";
if (beginIndex > endIndex) {
/**
* 方法容错
*
* If someone accidentally wrote it backwards
* That doesn't matter, either
*
* 如果某人把参数写反了,那也没有关系
* 程序会自动把参数切换过来
*/
beginIndex = beginIndex ^ endIndex;
endIndex = endIndex ^ beginIndex;
beginIndex = beginIndex ^ endIndex;
}
/**
* 如果可以使用缓存,那就使用缓存
*/
if (endIndex - beginIndex < cache.length && cover == coverDefault)
return cache[endIndex - beginIndex];
/**
* 指定 char[] 的长度是掩码的长度,最大化利用资源
* 如果不指定,默认会是缓存最大值:16
* 如果程序运行到这里,那么很有可能掩码数量大于16
*/
StringBuilder sb = new StringBuilder(endIndex - beginIndex);
for (; beginIndex <= endIndex; beginIndex++)
sb.append(cover);
return sb.toString();
}

/**
* Getter and Setter in here
*/
public static String[] getCache() {
return cache;
}

public static char getCoverDefault() {
return coverDefault;
}

public String getInitialCode() {
return initialCode;
}

public void setInitialCode(String initialCode) {
this.initialCode = initialCode;
}

public int getBeginIndex() {
return beginIndex;
}

public void setBeginIndex(int beginIndex) {
this.beginIndex = beginIndex;
}

public int getEndIndex() {
return endIndex;
}

public void setEndIndex(int endIndex) {
this.endIndex = endIndex;
}

public char getCover() {
return cover;
}

public void setCover(char cover) {
this.cover = cover;
}
}


如何使用这个类

public static void main(String[] args) {
// 静态方法调用
Override.getMaskCharWay("15197447018",3,9);
Override.getMaskCharWay("15197447018",3,9);
Override.getMaskCharWay("15197447018",3,9,'-');
Override.getMaskCharWay("15197447018",3,9,'-');
// 实例方法调用
Override override = new Override();
override.setCover('+');
override.setBeginIndex(3);
override.setEndIndex(9);
override.setInitialCode("15197447018");
// 构造器调用
Override overrideX = new Override("15197447018",3,9);
Override overrideZ = new Override("15197447018",3,9,'-');
// 错误调用 - 反向调用
Override overrideW = new Override("15197447018",9,3);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: