Flask接口签名sign原理与实例代码浅析
528
2023-01-03
JAVA实现社会统一信用代码校验的方法
网上找了几个,写的都不太适合,有的写出来了,也没有给出参考的算法链接。这样就导致了如果产生错误我们无法排查(不懂原理怎么排查对吧)。
如果在使用过程中有疑虑,请参考:较验规则
package com.mengyunzhi.common.utils;
import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.bidimap.TreeBidiMap;
import org.GivKXEXOvkslf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.ValidationException;
import java.util.Map;
import java.util.Random;
/**
* 统一社会停用用代码
*/
public interface UnifiedCreditCodeUtils {
Logger logger = LoggerFactory.getLogger(UnifiedCreditCodeUtils.class);
String baseCode = "0123456789ABCDEFGHJKLMNPQRTUWXY";
char[] baseCodeArray = baseCode.toCharArray();
int[] wi = {1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28};
/**
* 生成供较验使用的 Code Map
*
* @return BidiMap
*/
static BidiMap
BidiMap
for (int i = 0; i < baseCode.length(); i++) {
codes.put(baseCodeArray[i], i);
}
return codes;
}
/**
* 较验社会统一信用代码
*
* @param unifiedCreditCode 统一社会信息代码
* @return 符合: true
*/
static boolean validateUnifiedCreditCode(String unifiedCreditCode) {
if ((unifiedCreditCode.equals("")) || unifiedCreditCode.length() != 18) {
return false;
}
Map
int parityBit;
try {
parityBit = getParityBit(unifiedCreditCode, codes);
} catch (ValidationException e) {
return false;
}
return parityBit == codes.get(unifiedCreditCode.charAt(unifiedCreditCode.length() - 1));
}
/**
* 获取较验码
*
* @param unifiedCreditCode 统一社会信息代码
* @param codes 带有映射关系的国家代码
* @return 获取较验位的值
*/
static int getParityBit(String unifiedCreditCode, Map
char[] businessCodeArray = unifiedCreditCode.toCharArray();
int sum = 0;
for (int i = 0; i < 17; i++) {
char key = businessCodeArray[i];
if (baseCode.indexOf(key) == -1) {
throw new ValidationException("第" + String.valueOf(i + 1) + "位传入了非法的字符" + key);
}
sum += (codes.get(key) * wi[i]);
}
int result = 31 - sum % 31;
return result == 31 ? 0 : result;
}
/**
* 获取一个随机的统一社会信用代码
*
* @return 统一社会信用代码
*/
static String generateOneUnifiedCreditCode() {
Random random = new Random();
StringBuilder buf = new StringBuilder();
for (int i = 0; i < 17; ++i) {
int num = random.nextInt(baseCode.length() - 1);
buf.append(baseCode.charAt(num));
}
String code = buf.toString();
String upperCode = code.toUpperCase();
BidiMap
int parityBit = getParityBit(upperCode, codes);
if (codes.getKey(parityBit) == null) {
logger.debug("生成社会统一信用代码不符合规则");
upperCode = generateOneUnifiedCreditCode();
} else {
upperCode = upperCode + codes.getKey(parityBit);
}
return upperCode;
}
}
测试
package com.mengyunzhi.common.utils;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import static org.junit.Assert.*;
public class UnifiedCreditCodeUtilsTest {
@Test
public void validateUnifiedCreditCode() {
String code = "911310827965850580";
Assertions.assertThat(UnifiedCreditCodeUtils.validateUnifiedCreditCode(code)).isTrue();
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~