How to create a random (or unique) string (or number)
Problem
You want to create a unique number for:
-
using an a primary key in a table
-
for security purposes like creating a salt to attach to a hash value
Possible solutions
-
Create a new GUID (Globally Unique Identifier) by using the GUID.NewGuid method
-
Use the RNG Crypto service provider to create a random byte array and convert
it into a string
-
Use DateTime.Now if that would suffice
Example for using RNG Crypto Service Provider:
byte[] saltInBytes = new byte[8];
RNGCryptoServiceProvider saltGenerator = new RNGCryptoServiceProvider();
saltGenerator.GetBytes(saltInBytes);
string saltAsString = Convert.ToBase64String(saltInBytes);
References
http://macronimous.com/resources/Secure_Password_Programming.asp