csharp 随机字符串生成器和支付ReferenceNumber生成器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 随机字符串生成器和支付ReferenceNumber生成器相关的知识,希望对你有一定的参考价值。

   public static class ReferenceNumberBuilder
    {
        const string defaultChars = "abcdefghijklmnoprstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        private static Random random = new Random();
        public static string RandomString(int length)
        {
            return RandomString(null, length);
        }

        /// <summary>
        /// Generates a random string from the provided string characters
        /// </summary>
        /// <param name="chars">if you pass emtpy string or null defaultChars are provided "abcdefghijklmnoprstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"</param>
        /// <param name="length">the length of the random string to generate</param>
        /// <returns>random string from character set and length you have provided </returns>
        public static string RandomString(string chars, int length)
        {
            if (string.IsNullOrWhiteSpace(chars))
                chars = defaultChars;

            return new string(Enumerable.Repeat(chars, length)
              .Select(s => s[random.Next(s.Length)]).ToArray());
        }

        public static string NewReferenceNumber()
        {
            /* 
			Struktura reference SI:
			- na 1. in 2. mestu: konstanta SI
			- na 3. in 4. mestu: številka modela
			- od 5. do 26. mesta: vsebina modela, ki je sestavljena iz največ 22 znakov, od tega do 20
			številk in največ dva vezaja		
			
			Št. Modela		Vsebina modela (razdelitev polja (sklopi P) in pozicija kontrolnih številk (K) v modelu)	Število sklopov (P)
			01 				(P1 - P2 - P3) K 																			Obvezno 1, Dovoljeno 3					
			*/

            var randomString = RandomString("0123456789", 15);
            var controlNumber = ControlNumberModule11(randomString);
            return string.Format("SI01{0}{1}", randomString, controlNumber);
        }

        public static int ControlNumberModule11(string referenceNumber)
        {
            if (string.IsNullOrWhiteSpace(referenceNumber))
                throw new ArgumentOutOfRangeException("referenceNumber", "reference number should contain at least 1 character");

            Dictionary<char, int> conversionTable = new Dictionary<char, int>() {
                {'A' ,10},
                {'B' ,11},
                {'C' ,12},
                {'D' ,13},
                {'E' ,14},
                {'F' ,15},
                {'G' ,16},
                {'H' ,17},
                {'I' ,18},
                {'J' ,19},
                {'K' ,20},
                {'L' ,21},
                {'M' ,22},
                {'N' ,23},
                {'O' ,24},
                {'P' ,25},
                {'Q' ,26},
                {'R' ,27},
                {'S' ,28},
                {'T' ,29},
                {'U' ,30},
                {'V' ,31},
                {'W' ,32},
                {'X' ,33},
                {'Y' ,34},
                {'Z' ,35}};

            int ponder = 2;
            int sum = 0;
            int parsedValue = -1;

            for (var pos = (referenceNumber.Length - 1); pos > 0; pos--)
            {
                parsedValue = -1;
                var convertedValue =
                    int.TryParse($"{referenceNumber[pos]}", out parsedValue) ? parsedValue
                        : (conversionTable.ContainsKey(referenceNumber[pos]) ? conversionTable[referenceNumber[pos]] : 0);
                sum += (ponder * convertedValue);
            }

            int modulo = sum % 11;
            int controlNumber = 11 - modulo;
            controlNumber = controlNumber >= 10 ? 0 : controlNumber;

            return controlNumber;
        }
    }

以上是关于csharp 随机字符串生成器和支付ReferenceNumber生成器的主要内容,如果未能解决你的问题,请参考以下文章

关于在微信支付接口和支付宝接口中使用到的辅助函数

csharp 随机密码生成器

csharp 随机密码生成器

js 生成32位随机数,可用于微信支付流水号(前端生成)

csharp 生成真正的随机整数

csharp RandomBigInteger - 用于生成随机BigIntegers的C#Random类的扩展