mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add Luhn
This commit is contained in:
parent
583c49fb39
commit
61a7767ca3
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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 0008—2000),见:https://std.samr.gov.cn/hb/search/stdHBDetailed?id=C362C05F3E35A68DE05397BE0A0A9B00</li>
|
||||||
|
* <li>ISO 7812-1:1997,见:https://www.iso.org/standard/56907.html</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class BCN {
|
||||||
|
}
|
@ -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;
|
||||||
|
@ -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"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user