Unique String Generator

using System;
using System.Security.Cryptography;

public class UniqueStringGenerator
{
private static int DEFAULT_MIN_STRING_LENGTH = 20;
private static int DEFAULT_MAX_STRING_LENGTH = 20;


private static string STRING_CHARS_LCASE = "abcdefgijkmnopqrstwxyz";
private static string STRING_CHARS_UCASE = "ABCDEFGHJKLMNPQRSTWXYZ";
private static string STRING_CHARS_NUMERIC = "23456789";
private static string STRING_CHARS_SPECIAL = "*$-+?_&=!%{}/";

public static string Generate()
{
return Generate(DEFAULT_MIN_STRING_LENGTH,
DEFAULT_MAX_STRING_LENGTH);
}

public static string Generate(int length)
{
return Generate(length, length);
}

public static string GenerateWithSpecialCharacters()
{
return GenerateWithSpecialCharacters(DEFAULT_MIN_STRING_LENGTH,
DEFAULT_MAX_STRING_LENGTH);
}

public static string GenerateWithSpecialCharacters(int length)
{
return GenerateWithSpecialCharacters(length, length);
}

public static string GenerateWithSpecialCharacters(int minLength,
int maxLength)
{

if (minLength <= 0 || maxLength <= 0 || minLength > maxLength)
return null;

char[][] charGroups = new char[][]
{
STRING_CHARS_LCASE.ToCharArray(),
STRING_CHARS_UCASE.ToCharArray(),
STRING_CHARS_NUMERIC.ToCharArray(),
STRING_CHARS_SPECIAL.ToCharArray()
};

int[] charsLeftInGroup = new int[charGroups.Length];

for (int i = 0; i < charsLeftInGroup.Length; i++)
charsLeftInGroup[i] = charGroups[i].Length;

int[] leftGroupsOrder = new int[charGroups.Length];

for (int i = 0; i < leftGroupsOrder.Length; i++)
leftGroupsOrder[i] = i;

byte[] randomBytes = new byte[4];

RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(randomBytes);

int seed = (randomBytes[0] & 0x7f) << 24 |
randomBytes[1] << 16 |
randomBytes[2] << 8 |
randomBytes[3];

Random random = new Random(seed);

char[] UniqueString = null;

if (minLength < maxLength)
UniqueString = new char[random.Next(minLength, maxLength + 1)];
else
UniqueString = new char[minLength];

int nextCharIdx;

int nextGroupIdx;

int nextLeftGroupsOrderIdx;

int lastCharIdx;

int lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1;

for (int i = 0; i < UniqueString.Length; i++)
{

if (lastLeftGroupsOrderIdx == 0)
nextLeftGroupsOrderIdx = 0;
else
nextLeftGroupsOrderIdx = random.Next(0,
lastLeftGroupsOrderIdx);


nextGroupIdx = leftGroupsOrder[nextLeftGroupsOrderIdx];


lastCharIdx = charsLeftInGroup[nextGroupIdx] - 1;


if (lastCharIdx == 0)
nextCharIdx = 0;
else
nextCharIdx = random.Next(0, lastCharIdx + 1);


UniqueString[i] = charGroups[nextGroupIdx][nextCharIdx];


if (lastCharIdx == 0)
charsLeftInGroup[nextGroupIdx] =
charGroups[nextGroupIdx].Length;

else
{

if (lastCharIdx != nextCharIdx)
{
char temp = charGroups[nextGroupIdx][lastCharIdx];
charGroups[nextGroupIdx][lastCharIdx] =
charGroups[nextGroupIdx][nextCharIdx];
charGroups[nextGroupIdx][nextCharIdx] = temp;
}

charsLeftInGroup[nextGroupIdx]--;
}

if (lastLeftGroupsOrderIdx == 0)
lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1;

else
{

if (lastLeftGroupsOrderIdx != nextLeftGroupsOrderIdx)
{
int temp = leftGroupsOrder[lastLeftGroupsOrderIdx];
leftGroupsOrder[lastLeftGroupsOrderIdx] =
leftGroupsOrder[nextLeftGroupsOrderIdx];
leftGroupsOrder[nextLeftGroupsOrderIdx] = temp;
}
lastLeftGroupsOrderIdx--;
}
}

return new string(UniqueString);
}

public static string Generate(int minLength,
int maxLength)
{

if (minLength <= 0 || maxLength <= 0 || minLength > maxLength)
return null;

char[][] charGroups = new char[][]
{
STRING_CHARS_LCASE.ToCharArray(),
STRING_CHARS_UCASE.ToCharArray(),
STRING_CHARS_NUMERIC.ToCharArray(),
};

int[] charsLeftInGroup = new int[charGroups.Length];

for (int i = 0; i < charsLeftInGroup.Length; i++)
charsLeftInGroup[i] = charGroups[i].Length;

int[] leftGroupsOrder = new int[charGroups.Length];

for (int i = 0; i < leftGroupsOrder.Length; i++)
leftGroupsOrder[i] = i;

byte[] randomBytes = new byte[4];

RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(randomBytes);

int seed = (randomBytes[0] & 0x7f) << 24 |
randomBytes[1] << 16 |
randomBytes[2] << 8 |
randomBytes[3];

Random random = new Random(seed);

char[] UniqueString = null;

if (minLength < maxLength)
UniqueString = new char[random.Next(minLength, maxLength + 1)];
else
UniqueString = new char[minLength];

int nextCharIdx;

int nextGroupIdx;

int nextLeftGroupsOrderIdx;

int lastCharIdx;

int lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1;

for (int i = 0; i < UniqueString.Length; i++)
{

if (lastLeftGroupsOrderIdx == 0)
nextLeftGroupsOrderIdx = 0;
else
nextLeftGroupsOrderIdx = random.Next(0,
lastLeftGroupsOrderIdx);


nextGroupIdx = leftGroupsOrder[nextLeftGroupsOrderIdx];


lastCharIdx = charsLeftInGroup[nextGroupIdx] - 1;


if (lastCharIdx == 0)
nextCharIdx = 0;
else
nextCharIdx = random.Next(0, lastCharIdx + 1);


UniqueString[i] = charGroups[nextGroupIdx][nextCharIdx];


if (lastCharIdx == 0)
charsLeftInGroup[nextGroupIdx] =
charGroups[nextGroupIdx].Length;

else
{

if (lastCharIdx != nextCharIdx)
{
char temp = charGroups[nextGroupIdx][lastCharIdx];
charGroups[nextGroupIdx][lastCharIdx] =
charGroups[nextGroupIdx][nextCharIdx];
charGroups[nextGroupIdx][nextCharIdx] = temp;
}

charsLeftInGroup[nextGroupIdx]--;
}

if (lastLeftGroupsOrderIdx == 0)
lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1;

else
{

if (lastLeftGroupsOrderIdx != nextLeftGroupsOrderIdx)
{
int temp = leftGroupsOrder[lastLeftGroupsOrderIdx];
leftGroupsOrder[lastLeftGroupsOrderIdx] =
leftGroupsOrder[nextLeftGroupsOrderIdx];
leftGroupsOrder[nextLeftGroupsOrderIdx] = temp;
}
lastLeftGroupsOrderIdx--;
}
}

return new string(UniqueString);
}
}





There are two functions

* Generate
o This function returns the string with no special characters
* GenerateWithSpecialCharacters
o This function returns the string with special characters included
* Example Code
o string temp = UniqueStringGenerator.GenerateWithSpecialCharacters(20,20);
o string temp = UniqueStringGenerator.GenerateWithSpecialCharacters(20);
o string temp = UniqueStringGenerator.GenerateWithSpecialCharacters();
o .......................
o string temp = UniqueStringGenerator.Generate(20,20);
o string temp = UniqueStringGenerator.Generate(20);
o string temp = UniqueStringGenerator.Generate();

Comments

Popular posts from this blog

Use configSource attribute to manage Web.Config sections in ASP.Net 2.0

DevOps - Key Concepts - Roles & Responsibilities - Tools & Technologies