Created
January 7, 2017 06:37
-
-
Save TakaakiIchijo/4dbc5ed25f9b6ac65ec76c48e5aa9a28 to your computer and use it in GitHub Desktop.
いい感じの自動生成ID・パスワードに使える英数文字列を生成する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
public static class Utility | |
{ | |
public static string GenerateRandomAlphanumeric(int length = 44, bool removeMistakableChar = true) | |
{ | |
string guid = Guid.NewGuid().ToString("N"); | |
string str = Convert.ToBase64String(Encoding.UTF8.GetBytes(guid)); | |
//先頭length文字まで取り出す// | |
str = str.Substring(0, length); | |
//印字や手書きに負けない文字列づくり// | |
if (removeMistakableChar) | |
{ | |
//大文字化// | |
str = str.ToUpper(); | |
//だめそうな文字を適当に置換// | |
StringBuilder sb = new StringBuilder(str); | |
sb.Replace("0", "2"); | |
sb.Replace("O", "Z"); | |
sb.Replace("8", "4"); | |
sb.Replace("S", "Y"); | |
sb.Replace("1", "X"); //l | |
sb.Replace("9", "6"); //q | |
str = sb.ToString(); | |
} | |
return str; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment