Generate LEI Number in ISO 7064 Format

Here, we will learn about how to generate the ISO 7064 compatible number. When I got this task, I also searched for whole day, but didn’t got anything for c#, I got for JavaScript, so converted the code from JavaScript to C# and now its working perfectly fine.

Structure of an LEI code :

A LEI code is normalized with International Organization for Standardization (ISO) declaration 17442. This Consists of a blend of 20 numbers and letters.

  • Characters 1-4: Prefix used to guarantee the uniqness among from LEI guarantors (Local Operating Units or LOUs).
  • Characters 5-18: Entity-explicit piece of the code produced and appointed by LOUs as indicated by straightforward, sound and powerful designation strategies. As needed by ISO 17442, it contains no inserted insight.
  • Characters 19-20: Two check digits as portrayed in the ISO 17442 norm.

Generate LEI Number in ISO 7064 Format:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Testing
{
    class GenerateLeiNumber
    {
        private static string _leiPrefix = "558600";
        private static int _leiLength = 12;
        private static readonly RNGCryptoServiceProvider RandomGenerator = new RNGCryptoServiceProvider();
        private static readonly char[] ValidCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();

        public static string GenrateLeiNumberService()
        {
            var randomData = new byte[_leiLength];
            RandomGenerator.GetNonZeroBytes(randomData);

            var result = new StringBuilder(_leiLength);
            int counter = 0;
            foreach (var value in randomData)
            {
                counter = (counter + value) % (ValidCharacters.Length - 1);
                result.Append(ValidCharacters[counter]);
            }
            string randomString = result.ToString();

            randomString = string.Format("{0}{1}", _leiPrefix, randomString);
            string checkDigit = CheckDigits.ComputeWithoutCheck(randomString);

            if (string.IsNullOrEmpty(checkDigit))
                return "";

            return string.Format("{0}{1}", randomString, checkDigit);

        }
        public static void Main(string[] args)
        {
            var leiNumber = GenrateLeiNumberService();
            Console.WriteLine("Lei Number: " + leiNumber);
        }
    }
    public class CheckDigits
    {
        const int CHARCODE_A = 65;
        const int CHARCODE_0 = 48;

        private static int Mod97(string value)
        {
            int buffer = 0;
            byte[] ASCIIValues = Encoding.ASCII.GetBytes(value);
            for (var i = 0; i < value.Length; ++i)
            {
                int charCode = ASCIIValues[i];
                buffer = charCode + (charCode >= CHARCODE_A ? buffer * 100 - CHARCODE_A + 10 : buffer * 10 - CHARCODE_0);

                if (buffer > 1000000)
                {
                    buffer %= 97;
                }
            }
            return buffer % 97;
        }

        public static string ComputeWithoutCheck(string value)
        {
            var fullNumber = value + ("0" + (98 - Mod97(value + "00")));
            return fullNumber.Substring(fullNumber.Length - 2);
        }
    }
}

That’s it.

Submit a Comment

Your email address will not be published. Required fields are marked *

Footer Logo

Subscribe

Select Categories