I need to generate strings that have an extremely high probability of being unique on all machines that they're generated on, and be different every time the code is run as well. The probability of being unique doesn't have to be 100 percent, and this is not security related, only the uniqueness matters (use case is seeding large state non-crypto PRNGs).
My current idea is to SHA512 hash the network adapter info, including adapter statistics, the computer name, process ID, computer up time in ticks and UTC current time in ticks, and convert this to a 64 character base 64 Unicode string.
Seems sound, but are there any better, as in .net functions for example, ways to do this?
Working code:
using System;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
static class UniqueString
{
private static SHA512 sha = SHA512.Create();
public static string Gen()
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
StringBuilder uniqueString = new StringBuilder();
foreach (NetworkInterface adapter in adapters)
{
IPInterfaceStatistics stats = adapter.GetIPStatistics();
uniqueString.AppendFormat("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15} {16} {17} ",
adapter.Description,
adapter.Id,
adapter.Name,
adapter.Speed,
adapter.GetPhysicalAddress(),
adapter.NetworkInterfaceType,
stats.BytesReceived,
stats.BytesSent,
stats.IncomingPacketsDiscarded,
stats.IncomingPacketsWithErrors,
stats.IncomingUnknownProtocolPackets,
stats.NonUnicastPacketsReceived,
stats.NonUnicastPacketsSent,
stats.OutgoingPacketsDiscarded,
stats.OutgoingPacketsWithErrors,
stats.OutputQueueLength,
stats.UnicastPacketsReceived,
stats.UnicastPacketsSent);
}
uniqueString.AppendFormat("{0} {1} {2} {3}",
Environment.MachineName,
Process.GetCurrentProcess().Id,
Environment.TickCount.ToString(),
DateTime.UtcNow.Ticks);
return Convert.ToBase64String(sha.ComputeHash(Encoding.Unicode.GetBytes(uniqueString.ToString())), 0, 48);
}
}