Skip to content

Instantly share code, notes, and snippets.

@PaulStovell
Created August 26, 2013 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulStovell/6337411 to your computer and use it in GitHub Desktop.
Save PaulStovell/6337411 to your computer and use it in GitHub Desktop.
public static async Task<Guid> NewReallyRandomGuid()
{
// Got this value from the 'Create GUID' dialog in Visual
// Studio. Guaranteed to be unique.
var guid = Guid.Parse("43BB5AE8-653E-4985-88F8-A8C4A92C9BD5").ToByteArray();
//Prevent collisions by XORing with random data
byte[] bytes = new byte[16];
var rng = new RNGCryptoServiceProvider();
rng.GetBytes(bytes);
for(int i = 0; i < guid.Length; i++)
{
guid[i] = (byte) (guid[i] ^ bytes[i]);
}
return new Guid(guid);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment