Rakesh Rajan's blog

Thoughts on .NET, software and a few trivial things...

How to create a random (or unique) string (or number)

Problem

You want to create a unique number for:
  1. using an a primary key in a table
  2. for security purposes like creating a salt to attach to a hash value

Possible solutions

  1. Create a new GUID (Globally Unique Identifier) by using the GUID.NewGuid method
  2. Use the RNG Crypto service provider to create a random byte array and convert it into a string
  3. 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

Posted: Apr 14 2005, 11:34 AM by rakeshrajan | with 2 comment(s)
Filed under:

Comments

TrackBack said:

How to create a random (or unique) string (or number)ooeess
# May 24, 2005 2:11 AM

TrackBack said:

How to create a random (or unique) string (or number)ooeess
# July 22, 2005 3:27 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)