If we compared my method below with RCIX's method in this answer to a question, which is more efficient and why?
private string RandomString(int length = 25)
{
const string chars = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789#@%&";
StringBuilder sbSalt = new StringBuilder();
for (int i = 0; i < length; i++)
{
int inx = 0;
inx = rnd.Next(0, chars.Length);
sbSalt.Append(chars[inx]);
}
return sbSalt.ToString();
}
private Random rnd = new Random();
The reason I did "aAbBcC..." instead of "abc...ABC..." was to increase the chances of randomization between the lower and capital case of each letter since they are next to each other.
And also, for a password salt, is it best to keep duplicate characters or should I make sure that each character is only encountered once per salt? Then again, if I allow duplicate characters, the salt combination amount that can be generated is greater.
Thanks in advance!
UPDATE #1:
I realised that if I called the RandomString function again, it would return the exact same Random String, so to fix that I declared rnd as new Random only once by moving it outside the function.