tools2

2023-02-12

[对身份证的校验]

//身份证的校验
import java.util.stream.IntStream; /**
* 身份证号码验证
* 1、号码的结构
* 公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成。从左至右依次为:六位数字地址码,
* 八位数字出生日期码,三位数字顺序码和一位数字校验码。
* 2、地址码(前六位数)
* 表示编码对象常住户口所在县(市、旗、区)的行政区划代码,按GB/T2260的规定执行。
* 3、出生日期码(第七位至十四位)
* 表示编码对象出生的年、月、日,按GB/T7408的规定执行,年、月、日代码之间不用分隔符。
* 4、顺序码(第十五位至十七位)
* 表示在同一地址码所标识的区域范围内,对同年、同月、同日出生的人编定的顺序号,
* 顺序码的奇数分配给男性,偶数分配给女性。
* 5、校验码(第十八位数)
* (1)十七位数字本体码加权求和公式 S = Sum(Ai Wi), i = 0, , 16 ,先对前17位数字的权求和 ;
* Ai:表示第i位置上的身份证号码数字值; Wi:表示第i位置上的加权因子 Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
* (2)计算模 Y = mod(S, 11)
* (3)通过模( 0 1 2 3 4 5 6 7 8 9 10)得到对应的校验码 Y:1 0 X 9 8 7 6 5 4 3 2
*
*/
public class IdentityUtils { /**
* 身份证校验码
*/
private static final int[] COEFFICIENT_ARRAY = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; /**
* 身份证号的尾数规则
*/
private static final String[] IDENTITY_MANTISSA = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"}; private static final String IDENTITY_PATTERN_15 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0-2]\\d)|3[0-1])\\d{3}$"; // 修改前未做X位的校验:"^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0-2]\\d)|3[0-1])\\d{4}$";
// 修改后:"^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0-2]\\d)|3[0-1])\\d{3}[0-9Xx]$";
private static final String IDENTITY_PATTERN_18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0-2]\\d)|3[0-1])\\d{3}[0-9Xx]$"; /**
* 身份证号校验
*/
public static boolean isLegalIdCard(String identity) {
if (identity == null) {
return false;
}
if (identity.length() == 15) {
return is15BitLegalIdCard(identity);
}
if (identity.length() == 18) {
return is18BitLegalIdCard(identity);
}
return false;
} public static boolean is15BitLegalIdCard(String identity) {
return identity.matches(IDENTITY_PATTERN_15);
} public static boolean is18BitLegalIdCard(String identity) {
if (identity.length() != 18) {
return false;
}
if (!identity.matches(IDENTITY_PATTERN_18)) {
return false;
}
// 将字符串对象中的字符转换为一个字符数组
char[] chars = identity.toCharArray();
long sum = IntStream.range(0, 17).map(index -> {
char ch = chars[index];
// 通俗理解:digit()是个边界值判断,不过边界返回字符数字本身数值,超过边界即返回 -1.
int digit = Character.digit(ch, 10);
int coefficient = COEFFICIENT_ARRAY[index];
return digit * coefficient;
}).summaryStatistics().getSum(); // 计算出的尾数索引
int mantissaIndex = (int) (sum % 11);
String mantissa = IDENTITY_MANTISSA[mantissaIndex]; String lastChar = identity.substring(17);
return lastChar.equalsIgnoreCase(mantissa);
}
}
//数组小写字母大小写的校验
public class CheckPasswordUtils {
//数字
public static final String REG_NUMBER = ".*\\d+.*";
//小写字母
public static final String REG_UPPERCASE = ".*[A-Z]+.*";
//大写字母
public static final String REG_LOWERCASE = ".*[a-z]+.*";
//特殊符号
public static final String REG_SYMBOL = ".*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\"]+.*";
public static boolean isPswComplex(String password){
//密码为空或者长度小于8位则返回false
if (password == null || password.length() <6 ||password.length() >16) return false;
int i = 0;
if (password.matches(REG_NUMBER)) i++;
if (password.matches(REG_LOWERCASE))i++;
if (password.matches(REG_UPPERCASE)) i++;
if (password.matches(REG_SYMBOL)) i++; if (i < 2 ) return false; return true;
} public static boolean isName(String name){
return name.length() >= 2 && name.length() <= 8;
}
}

[雪花算法id]

/**
* id自增器(雪花算法)
*
* @author renjie
* @version 1.0.0
*/
public class SnowFlake {
private final static long twepoch = 12888349746579L;
// 机器标识位数
private final static long workerIdBits = 5L;
// 数据中心标识位数
private final static long datacenterIdBits = 5L; // 毫秒内自增位数
private final static long sequenceBits = 12L;
// 机器ID偏左移12位
private final static long workerIdShift = sequenceBits;
// 数据中心ID左移17位
private final static long datacenterIdShift = sequenceBits + workerIdBits;
// 时间毫秒左移22位
private final static long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
//sequence掩码,确保sequnce不会超出上限
private final static long sequenceMask = -1L ^ (-1L << sequenceBits);
//上次时间戳
private static long lastTimestamp = -1L;
//序列
private long sequence = 0L;
//服务器ID
private long workerId = 1L;
private static long workerMask = -1L ^ (-1L << workerIdBits);
//进程编码
private long processId = 1L;
private static long processMask = -1L ^ (-1L << datacenterIdBits); private static SnowFlake snowFlake = null; static{
snowFlake = new SnowFlake();
}
public static synchronized String nextId(){
return String.valueOf(snowFlake.getNextId());
} private SnowFlake() { //获取机器编码
this.workerId=this.getMachineNum();
//获取进程编码
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
this.processId=Long.valueOf(runtimeMXBean.getName().split("@")[0]).longValue(); //避免编码超出最大值
this.workerId=workerId & workerMask;
this.processId=processId & processMask;
} public synchronized long getNextId() {
//获取时间戳
long timestamp = timeGen();
//如果时间戳小于上次时间戳则报错
if (timestamp < lastTimestamp) {
try {
throw new Exception("Clock moved backwards. Refusing to generate id for " + (lastTimestamp - timestamp) + " milliseconds");
} catch (Exception e) {
e.printStackTrace();
}
}
//如果时间戳与上次时间戳相同
if (lastTimestamp == timestamp) {
// 当前毫秒内,则+1,与sequenceMask确保sequence不会超出上限
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
// 当前毫秒内计数满了,则等待下一秒
timestamp = tilNextMillis(lastTimestamp);
}
} else {
sequence = 0;
}
lastTimestamp = timestamp;
// ID偏移组合生成最终的ID,并返回ID
long nextId = ((timestamp - twepoch) << timestampLeftShift) | (processId << datacenterIdShift) | (workerId << workerIdShift) | sequence;
return nextId;
} /**
* 再次获取时间戳直到获取的时间戳与现有的不同
* @param lastTimestamp
* @return 下一个时间戳
*/
private long tilNextMillis(final long lastTimestamp) {
long timestamp = this.timeGen();
while (timestamp <= lastTimestamp) {
timestamp = this.timeGen();
}
return timestamp;
} private long timeGen() {
return System.currentTimeMillis();
} /**
* 获取机器编码
* @return
*/
private long getMachineNum(){
long machinePiece;
StringBuilder sb = new StringBuilder();
Enumeration<NetworkInterface> e = null;
try {
e = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e1) {
e1.printStackTrace();
}
while (e.hasMoreElements()) {
NetworkInterface ni = e.nextElement();
sb.append(ni.toString());
}
machinePiece = sb.toString().hashCode();
return machinePiece;
}
}

tools2的相关教程结束。

《tools2.doc》

下载本文的Word格式文档,以方便收藏与打印。