zoukankan      html  css  js  c++  java
  • 如何得到一个随机密码

    public sealed class PasswordEngine
    {
        private static PasswordEngine engine = new PasswordEngine();
     
        public static PasswordEngine Default
        {
            get
            {
                return engine;
            }
        }
            
        private readonly Random rnd = new Random();
        private string[] allowedCharacters = new string[]
        {
            "abcdefghijklmnopqrstuvwxyz",
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
            "1234567890",
            "!@#$%^&*()_"
        };
     
        public string Generate(int size)
        {
            return Generate(size, 0, 0, 0);
        }
     
        public string Generate(int size,  int minCapitalLetters, int minNumbers, int minSpecial)
        {
            //Parameter validation
            if (minCapitalLetters + minNumbers + minSpecial > size)
            {
                throw new Exception("Parameter size should be less than or equal to minCapitalLetters + minSmallLetters + minNumbers + minSpecial");
            }
     
     
            //Buffer for 
            char[] buffer = new char[size];
     
            int currentIndex = 0;
            //Fill Capital Letters
            currentIndex = FillBuffer(currentIndex, minCapitalLetters, 1, buffer);
            //Fill Numbers Characters
            currentIndex = FillBuffer(currentIndex, minNumbers, 2, buffer);
            //Fill Special Characters
            currentIndex = FillBuffer(currentIndex, minSpecial, 3, buffer);
     
            //Fill remaining buffer with small characters
            int minSmallLetters= size - (minCapitalLetters  + minNumbers + minSpecial);
            currentIndex = FillBuffer(currentIndex, minSmallLetters, 0, buffer);
     
            RandomizeBuffer(size, buffer);
     
            return new string(buffer);
        }
     
        private void RandomizeBuffer(int size, char[] buffer)
        {
            for (int i = 0; i < size; i++)
            {
                char source = buffer[i];
                int swapIndex = rnd.Next(size);
                buffer[i] = buffer[swapIndex];
                buffer[swapIndex] = source;
            }
        }
     
        private int FillBuffer(int startIndex, int count, int row, char[] buffer)
        {
            for (int i = 0; i < count; i++)
            {
                rnd.Next(3);
                int col = rnd.Next(allowedCharacters[row].Length);
     
                buffer[i+startIndex] = allowedCharacters[row][col];
            }
     
            return startIndex + count;
        }
    }
    
    //This will generate a simple password
    string password1 = PasswordEngine.Default.Generate(10);
     
    //This will generate a password with 2 numbers, 2 special and 2 capital letters
    string password2 = PasswordEngine.Default.Generate(10, 2, 2, 2);
  • 相关阅读:
    大搬家--百度之星 (递推)
    Scrambled Polygon--poj2007(极角排序模板)
    Space Ant--poj1696(极角排序)
    A. Link/Cut Tree--cf614A ()
    Ultra-QuickSort--POJ2299(归并排序求逆序数对)
    An Easy Problem?!--
    C. The Two Routes---cf602C(Dij)
    java 中jar的使用
    两种方法解决tomcat的 Failed to initialize end point associated with ProtocolHandler ["http-apr-8080"]
    Ajax(6) Ajax向servlet请求数据库操作 并显示到当前页面 这个未经测试
  • 原文地址:https://www.cnblogs.com/tommyli/p/3125779.html
Copyright © 2011-2022 走看看