첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.00MB

▶ TemporaryPasswordHelper.cs

using System;
using System.Security.Cryptography;

namespace TestProject
{
    /// <summary>
    /// 임시 패스워드 헬퍼
    /// </summary>
    public class TemporaryPasswordHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 디폴트 최소 패스워드 길이
        /// </summary>
        private static int DEFAULT_MINIMUM_PASSWORD_LENGTH = 8;

        /// <summary>
        /// 디폴트 최대 패스워드 길이
        /// </summary>
        private static int DEFAULT_MAXIMUM_PASSWORD_LENGTH = 10;

        /// <summary>
        /// 소문자 문자 리스트
        /// </summary>
        private static string LOWER_CASE_CHARACTER_LIST = "abcdefgijkmnopqrstwxyz";

        /// <summary>
        /// 대문자 문자 리스트
        /// </summary>
        private static string UPPER_CASE_CHARACTER_LIST = "ABCDEFGHJKLMNPQRSTWXYZ";

        /// <summary>
        /// 숫자 문자 리스트
        /// </summary>
        private static string NUMERIC_CHARACTER_LIST = "23456789";

        /// <summary>
        /// 특수 문자 리스트
        /// </summary>
        private static string SPECIAL_CHARACTER_LIST = "*$-+?_&=!%{}/";

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 생성하기 - Generate(minimumLength, maximumLength)

        /// <summary>
        /// 생성하기
        /// </summary>
        /// <param name="minimumLength">최소 길이</param>
        /// <param name="maximumLength">최대 길이</param>
        /// <returns>임시 패스워드</returns>
        public static string Generate(int minimumLength, int maximumLength)
        {
            if(minimumLength <= 0 || maximumLength <= 0 || minimumLength > maximumLength)
            {
                return null;
            }

            char[][] characterArrayArray = new char[][]
            {
                LOWER_CASE_CHARACTER_LIST.ToCharArray(),
                UPPER_CASE_CHARACTER_LIST.ToCharArray(),
                NUMERIC_CHARACTER_LIST.ToCharArray(),
                SPECIAL_CHARACTER_LIST.ToCharArray()
            };

            int[] characterArrayLeftInGroup = new int[characterArrayArray.Length];

            for(int i = 0; i < characterArrayLeftInGroup.Length; i++)
            {
                characterArrayLeftInGroup[i] = characterArrayArray[i].Length;
            }

            int[] leftGroupOrderArray = new int[characterArrayArray.Length];

            for(int i = 0; i < leftGroupOrderArray.Length; i++)
            {
                leftGroupOrderArray[i] = i;
            }

            byte[] randomByteArray = new byte[4];

            RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();

            provider.GetBytes(randomByteArray);

            int seed = BitConverter.ToInt32(randomByteArray, 0);

            Random random = new Random(seed);

            char[] passwordCharacterArray = null;

            if(minimumLength < maximumLength)
            {
                passwordCharacterArray = new char[random.Next(minimumLength, maximumLength + 1)];
            }
            else
            {
                passwordCharacterArray = new char[minimumLength];
            }

            int nextCharacterIndex;
            int nextGroupIndex;
            int nextLeftGroupOrderIndex;
            int lastCharacterIndex;
            int lastLeftGroupOrderIndex = leftGroupOrderArray.Length - 1;

            for(int i = 0; i < passwordCharacterArray.Length; i++)
            {
                if(lastLeftGroupOrderIndex == 0)
                {
                    nextLeftGroupOrderIndex = 0;
                }
                else
                {
                    nextLeftGroupOrderIndex = random.Next(0, lastLeftGroupOrderIndex);
                }

                nextGroupIndex = leftGroupOrderArray[nextLeftGroupOrderIndex];

                lastCharacterIndex = characterArrayLeftInGroup[nextGroupIndex] - 1;

                if(lastCharacterIndex == 0)
                {
                    nextCharacterIndex = 0;
                }
                else
                {
                    nextCharacterIndex = random.Next(0, lastCharacterIndex + 1);
                }

                passwordCharacterArray[i] = characterArrayArray[nextGroupIndex][nextCharacterIndex];

                if(lastCharacterIndex == 0)
                {
                    characterArrayLeftInGroup[nextGroupIndex] = characterArrayArray[nextGroupIndex].Length;
                }
                else
                {
                    if(lastCharacterIndex != nextCharacterIndex)
                    {
                        char temporaryCharacter = characterArrayArray[nextGroupIndex][lastCharacterIndex];

                        characterArrayArray[nextGroupIndex][lastCharacterIndex] = characterArrayArray[nextGroupIndex][nextCharacterIndex];
                        characterArrayArray[nextGroupIndex][nextCharacterIndex] = temporaryCharacter;
                    }

                    characterArrayLeftInGroup[nextGroupIndex]--;
                }

                if(lastLeftGroupOrderIndex == 0)
                {
                    lastLeftGroupOrderIndex = leftGroupOrderArray.Length - 1;
                }
                else
                {
                    if(lastLeftGroupOrderIndex != nextLeftGroupOrderIndex)
                    {
                        int temporaryValue = leftGroupOrderArray[lastLeftGroupOrderIndex];

                        leftGroupOrderArray[lastLeftGroupOrderIndex] = leftGroupOrderArray[nextLeftGroupOrderIndex];
                        leftGroupOrderArray[nextLeftGroupOrderIndex] = temporaryValue;
                    }

                    lastLeftGroupOrderIndex--;
                }
            }

            return new string(passwordCharacterArray);
        }

        #endregion
        #region 생성하기 - Generate()

        /// <summary>
        /// 생성하기
        /// </summary>
        /// <returns>임시 패스워드</returns>
        public static string Generate()
        {
            return Generate(DEFAULT_MINIMUM_PASSWORD_LENGTH, DEFAULT_MAXIMUM_PASSWORD_LENGTH);
        }

        #endregion
        #region 생성하기 - Generate(length)

        /// <summary>
        /// 생성하기
        /// </summary>
        /// <param name="length">길이</param>
        /// <returns>임시 패스워드</returns>
        public static string Generate(int length)
        {
            return Generate(length, length);
        }

        #endregion
    }
}

 

728x90

 

▶ Program.cs

using System;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            Console.WriteLine(TemporaryPasswordHelper.Generate(10, 15));
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요