This commit is contained in:
Looly 2021-07-10 18:56:07 +08:00
parent 0a2524faf7
commit 859aac43b3
2 changed files with 35 additions and 33 deletions

View File

@ -3,7 +3,7 @@
-------------------------------------------------------------------------------------------------------------
# 5.7.4 (2021-07-09)
# 5.7.4 (2021-07-10)
### 🐣新特性
* 【crypto 】 SmUtil.sm4统一返回类型issue#I3YKD4@Gitee
@ -22,6 +22,7 @@
* 【core 】 修复EqualsBuilder数组判断问题pr#1694@Github
* 【setting】 修复Props中Charset对象无法序列化的问题pr#1694@Github
* 【db 】 修复PageResult首页判断逻辑问题issue#1699@Github
* 【core 】 修复IdcardUtil可能数组越界问题pr#1702@Github
-------------------------------------------------------------------------------------------------------------

View File

@ -46,7 +46,7 @@ public class IdcardUtil {
/**
* 台湾身份首字母对应数字
*/
private static final Map<String, Integer> TW_FIRST_CODE = new HashMap<>();
private static final Map<Character, Integer> TW_FIRST_CODE = new HashMap<>();
static {
CITY_CODES.put("11", "北京");
@ -87,32 +87,32 @@ public class IdcardUtil {
CITY_CODES.put("83", "台湾");
CITY_CODES.put("91", "国外");
TW_FIRST_CODE.put("A", 10);
TW_FIRST_CODE.put("B", 11);
TW_FIRST_CODE.put("C", 12);
TW_FIRST_CODE.put("D", 13);
TW_FIRST_CODE.put("E", 14);
TW_FIRST_CODE.put("F", 15);
TW_FIRST_CODE.put("G", 16);
TW_FIRST_CODE.put("H", 17);
TW_FIRST_CODE.put("J", 18);
TW_FIRST_CODE.put("K", 19);
TW_FIRST_CODE.put("L", 20);
TW_FIRST_CODE.put("M", 21);
TW_FIRST_CODE.put("N", 22);
TW_FIRST_CODE.put("P", 23);
TW_FIRST_CODE.put("Q", 24);
TW_FIRST_CODE.put("R", 25);
TW_FIRST_CODE.put("S", 26);
TW_FIRST_CODE.put("T", 27);
TW_FIRST_CODE.put("U", 28);
TW_FIRST_CODE.put("V", 29);
TW_FIRST_CODE.put("X", 30);
TW_FIRST_CODE.put("Y", 31);
TW_FIRST_CODE.put("W", 32);
TW_FIRST_CODE.put("Z", 33);
TW_FIRST_CODE.put("I", 34);
TW_FIRST_CODE.put("O", 35);
TW_FIRST_CODE.put('A', 10);
TW_FIRST_CODE.put('B', 11);
TW_FIRST_CODE.put('C', 12);
TW_FIRST_CODE.put('D', 13);
TW_FIRST_CODE.put('E', 14);
TW_FIRST_CODE.put('F', 15);
TW_FIRST_CODE.put('G', 16);
TW_FIRST_CODE.put('H', 17);
TW_FIRST_CODE.put('J', 18);
TW_FIRST_CODE.put('K', 19);
TW_FIRST_CODE.put('L', 20);
TW_FIRST_CODE.put('M', 21);
TW_FIRST_CODE.put('N', 22);
TW_FIRST_CODE.put('P', 23);
TW_FIRST_CODE.put('Q', 24);
TW_FIRST_CODE.put('R', 25);
TW_FIRST_CODE.put('S', 26);
TW_FIRST_CODE.put('T', 27);
TW_FIRST_CODE.put('U', 28);
TW_FIRST_CODE.put('V', 29);
TW_FIRST_CODE.put('X', 30);
TW_FIRST_CODE.put('Y', 31);
TW_FIRST_CODE.put('W', 32);
TW_FIRST_CODE.put('Z', 33);
TW_FIRST_CODE.put('I', 34);
TW_FIRST_CODE.put('O', 35);
}
/**
@ -343,23 +343,24 @@ public class IdcardUtil {
* @return 验证码是否符合
*/
public static boolean isValidTWCard(String idcard) {
if (StrUtil.isEmpty(idcard) || idcard.length() != 10) {
if (null == idcard || idcard.length() != 10) {
return false;
}
String start = idcard.substring(0, 1);
Integer iStart = TW_FIRST_CODE.get(start);
final Integer iStart = TW_FIRST_CODE.get(idcard.charAt(0));
if (null == iStart) {
return false;
}
String mid = idcard.substring(1, 9);
String end = idcard.substring(9, 10);
int sum = iStart / 10 + (iStart % 10) * 9;
final String mid = idcard.substring(1, 9);
final char[] chars = mid.toCharArray();
int iflag = 8;
for (char c : chars) {
sum += Integer.parseInt(String.valueOf(c)) * iflag;
iflag--;
}
final String end = idcard.substring(9, 10);
return (sum % 10 == 0 ? 0 : (10 - sum % 10)) == Integer.parseInt(end);
}