|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace IPFilterTests
{
public static class RandomUtil
{
private static readonly char[] _randomStringValues = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray();
public static string CreateRandomString(int length)
{
return CreateRandomString(length, _randomStringValues);
}
public static string CreateRandomString(int length, char[] values)
{
// Get the seed.
int seed = GetSeed();
// Create the random class
Random rnd = new Random(seed);
// Create array for password.
char[] str = new char[length];
int valueLen = values.Length;
for (int i = 0; i < str.Length; i++)
{
str[i] = values[rnd.Next(0, valueLen - 1)];
}
return new string(str);
}
private static int GetSeed()
{
byte[] bytes = new byte[4];
RandomNumberGenerator rng = RandomNumberGenerator.Create();
rng.GetBytes(bytes);
return
bytes[0] |
bytes[1] << 8 |
bytes[2] << 16 |
bytes[3] << 24;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.