java - 如何在java中验证LEI校验和?

标签 java checksum mod

我正在关注https://en.wikipedia.org/wiki/International_Bank_Account_Number使用以下代码验证法人机构标识符 (LEI) 的校验和:

import java. math. BigInteger;

public class LEIChecksumVerifier{

    public final String [] leiChars = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    public final String [] leiDigits = {"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35"};

    public boolean verifyLeiCheckSum(String lei){
        boolean result = false;
        String shiftedLei = "";
        String digitisedLei= "";
        BigInteger leiBigInt = new BigInteger("0");
        BigInteger leiDivisorBigInt = new BigInteger("97");
        BigInteger remainderBigInt = new BigInteger("0");

        if(lei != null){
            lei = lei.toUpperCase().trim();
            if(lei.length() == 20){
                //Step1: Shift the first 4 digits to the end
                shiftedLei =  lei.substring(4, 20).concat(lei.substring(0, 4));
                //System.out.println("LEI: "+lei+"\tShifted LEI: "+shiftedLei);
                digitisedLei= shiftedLei;
                //Step2: Replace the alphabets with their integer values
                for ( int e = 0; e < leiChars.length; e++){
                    digitisedLei= digitisedLei.replaceAll(leiChars[e], leiDigits[e]);
                }
                //System.out.println("LEI: "+lei+"\tShifted LEI: "+shiftedLei+"\tDigitised LEI: "+digitisedLei);
                leiBigInt = new BigInteger(digitisedLei);
                //System.out.println("LEI: "+lei+"\tShifted LEI: "+shiftedLei+"\tDigitised LEI: "+digitisedLei+"\t LEI Big Integer: "+leiBigInt);
                remainderBigInt = leiBigInt.mod(leiDivisorBigInt);
                System.out.println("LEI: "+lei+"\tShifted LEI: "+shiftedLei+"\tDigitised LEI: "+digitisedLei+"\t LEI Big Integer: "+leiBigInt+"\t "+lei+" mod 97 : "+remainderBigInt);
                //Step3: if digitiesedLEI mod 97 == 1, checksum is valid!
                result = remainderBigInt.equals(new BigInteger("1"));
            }else{
                System.out.println("Error: "+lei+" length "+lei.length()+" differs from its standard value 20!");
            }
        }else{
            System.out.println("Error: lei is null!");
        }
    System.out.println("LEI :"+lei+" has valid checksum:" + result+"!");
    return result;      
    }

    public static void main(String [] args){
        LEIChecksumVerifier lEIChecksumVerifier = new LEIChecksumVerifier();
        //LEI of British Broadcasting Corporation : 5493-00-0IBP32UQZ0KL-24     
        lEIChecksumVerifier.verifyLeiCheckSum("5493000IBP32UQZ0KL24");
    }
}

但是,它为有效 LEI 提供了无效的校验和。请指导我进行正确的 LEI 校验和验证!

最佳答案

很难找到详细信息,但是https://github.com/EDumdum/iso-17442-java有一个实现。

LEI 中的校验和已经位于末尾,您在代码中所做的所有更改就是跳过移位步骤。现在它为我报告了一个有效的校验和。

关于java - 如何在java中验证LEI校验和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56394750/

相关文章:

java - LibGDX 1.5 围绕其中心旋转 Sprite

java - 在 Java 和 C 中,负数 Mod 给出负结果

mySql 为每 3 条记录选择唯一的编号

android - 如何处理 .tar.md5 文件

algorithm - 计算 1^X + 2^X + ... + N^X mod 1000000007

Java 连接池选项

java - Felix 如何从更新的包中获取所有依赖包的列表

java - 使用 Cobertura 从代码覆盖范围中排除方法

c# - 使用内置校验和创建唯一 ID?

python - 对于非常大的文件来说,Python "cksum"等效项是什么?它是如何工作的?