This commit is contained in:
Looly 2024-06-09 17:36:22 +08:00
parent 583c49fb39
commit 61a7767ca3
4 changed files with 123 additions and 1 deletions

View File

@ -0,0 +1,69 @@
/*
* Copyright (c) 2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* https://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.core.codec;
import org.dromara.hutool.core.regex.PatternPool;
import org.dromara.hutool.core.regex.ReUtil;
import org.dromara.hutool.core.text.StrUtil;
/**
* Luhn算法也称为模10算法是一种简单的校验和Checksum算法在ISO/IEC 7812-1中定义校验步骤如下
* <ol>
* <li>从右边第1个数字校验数字开始偶数位乘以2如果小于10直接返回否则将个位数和十位数相加</li>
* <li>把步骤1种获得的乘积的各位数字与原号码中未乘2的各位数字相加</li>
* <li>如果步骤2得到的总和模10为0则校验通过</li>
* </ol>
*
*/
public class Luhn {
/**
* 校验字符串
*
* @param withCheckDigitString 含校验数字的字符串
* @return true - 校验通过false-校验不通过
* @throws IllegalArgumentException 如果字符串为空或不是8~19位的数字
*/
public static boolean check(final String withCheckDigitString) {
if(StrUtil.isBlank(withCheckDigitString)){
return false;
}
if(!ReUtil.isMatch(PatternPool.NUMBERS, withCheckDigitString)){
// 必须为全数字
return false;
}
return sum(withCheckDigitString) % 10 == 0;
}
/**
* 根据Luhn算法计算字符串各位数字之和
*
* @param str 需要校验的数字字符串
* @return 数字之和
*/
private static int sum(final String str) {
final char[] strArray = str.toCharArray();
final int n = strArray.length;
int sum = strArray[n - 1] - '0';;
for (int i = 2; i <= n; i++) {
int a = strArray[n - i] - '0';
// 偶数位乘以2
if ((i & 1) == 0) {
a *= 2;
}
// 十位数和个位数相加如果不是偶数位不乘以2则十位数为0
sum += a / 10 + a % 10;
}
return sum;
}
}

View File

@ -0,0 +1,25 @@
/*
* Copyright (c) 2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* https://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.core.data;
/**
* 银行卡号Bank Card Number封装参考标准
* <ul>
* <li>银行卡发卡行标识代码及卡号JR/T 00082000https://std.samr.gov.cn/hb/search/stdHBDetailed?id=C362C05F3E35A68DE05397BE0A0A9B00</li>
* <li>ISO 7812-1:1997https://www.iso.org/standard/56907.html</li>
* </ul>
*
*
*/
public class BCN {
}

View File

@ -197,7 +197,6 @@ public class NumberValidator {
return true; return true;
} }
try { try {
//noinspection ResultOfMethodCallIgnored
Long.decode(s); Long.decode(s);
} catch (final NumberFormatException e) { } catch (final NumberFormatException e) {
return false; return false;

View File

@ -0,0 +1,29 @@
/*
* Copyright (c) 2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* https://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.core.codec;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class LuhnTest {
@Test
void checkTest() {
assertTrue(Luhn.check("6225760008219524"));
assertFalse(Luhn.check("123456"));
assertFalse(Luhn.check(""));
assertFalse(Luhn.check("abc"));
assertFalse(Luhn.check("622576000821952a"));
}
}