■ RNGCryptoServiceProvider 클래스의 GetBytes 메소드를 사용해 임시 패스워드를 생성하는 방법을 보여준다.
▶ TemporaryPasswordHelper.cs
1 2 3 4 5 6 7 8 9 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
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 } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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 } } |