Skip to content

Instantly share code, notes, and snippets.

@syamsathyan
Last active May 23, 2016 23:38
Show Gist options
  • Save syamsathyan/1f813eb7e78119f21c2f3d474b47b90d to your computer and use it in GitHub Desktop.
Save syamsathyan/1f813eb7e78119f21c2f3d474b47b90d to your computer and use it in GitHub Desktop.
public static class BaseKeyGenerator
{
public static void TestBaseKeyGenerator()
{
// create five IDs of six, base 62 characters
for (int i=0; i<5; i++) Console.WriteLine(BaseKeyGenerator.GetBase62(6));
// create five IDs of eight base 36 characters
for (int i=0; i<5; i++) Console.WriteLine(BaseKeyGenerator.GetBase36(8));
}
private static char[] _base62chars =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
.ToCharArray();
private static Random _random = new Random();
public static string GetBase62(int length)
{
var sb = new StringBuilder(length);
for (int i=0; i<length; i++)
sb.Append(_base62chars[_random.Next(62)]);
return sb.ToString();
}
public static string GetBase36(int length)
{
var sb = new StringBuilder(length);
for (int i=0; i<length; i++)
sb.Append(_base62chars[_random.Next(36)]);
return sb.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment