첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;

#region 임시 패스워드 생성하기 - CreateTemporaryPassword(length)

/// <summary>
/// 임시 패스워드 생성하기
/// </summary>
/// <param name="length">길이</param>
/// <returns>임시 패스워드</returns>
public string CreateTemporaryPassword(int length)
{
    const string characterText = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    return GetRandomString(characterText, length);
}

#endregion
#region 임의 문자열 구하기 - GetRandomString(characterEnumerable, length)

/// <summary>
/// 임의 문자열 구하기
/// </summary>
/// <param name="characterEnumerable">문자 열거 가능형</param>
/// <param name="length">길이</param>
/// <returns>임의 문자열</returns>
private string GetRandomString(IEnumerable<char> characterEnumerable, int length)
{
    if(length < 0)
    {
        throw new ArgumentException("length must not be negative", "length");
    }

    if(length > int.MaxValue / 8)
    {
        throw new ArgumentException("length is too big", "length");
    }

    if(characterEnumerable == null)
    {
        throw new ArgumentNullException("characterEnumerable");
    }

    char[] characterArray = characterEnumerable.Distinct().ToArray();

    if(characterArray.Length == 0)
    {
        throw new ArgumentException("characterEnumerable must not be empty", "characterEnumerable");
    }

    byte[] targetByteArray = new byte[length * 8];

    new RNGCryptoServiceProvider().GetBytes(targetByteArray);

    char[] targetCharacterArray = new char[length];

    for(int i = 0; i < length; i++)
    {
        ulong value = BitConverter.ToUInt64(targetByteArray, i * 8);

        targetCharacterArray[i] = characterArray[value % (uint)characterArray.Length];
    }

    return new string(targetCharacterArray);
}

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

댓글을 달아 주세요