首页 > 解决方案 > 如何在 Java 中验证 LEI 校验和?

问题描述

我正在关注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 校验和验证!

标签: javachecksummod

解决方案


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

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


推荐阅读