trying to duplicate some PHP code in C#. The PHP code uses the uniqid function with the more entropy parameter set to true. Any idea of how to best duplicate this in C#?
something like:
string uniqid(string prefix, bool more_entropy) {
if(string.IsNullOrEmpty(prefix))
prefix = string.Empty;
if (!more_entropy) {
return (prefix + System.Guid.NewGuid().ToString()).Left(13);
} else {
return (prefix + System.Guid.NewGuid().ToString() + System.Guid.NewGuid().ToString()).Left(23);
}
}
Edit: To reflect comments
This is a very simple off the top of my head solution, and I'd suggest that for a more rigorous solution you'd investigate proper 'random' number' generation such as via System.Math namespace directly. Please see these other SO questions and answers on random number generation.
System.Guid.NewGuid().ToString()
Will give you a unique string.
After some googling I found how PHP's uniqid works and implemented it in c#:
private string GetUniqID()
{
var ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
double t = ts.TotalMilliseconds / 1000;
int a = (int)Math.Floor(t);
int b = (int)((t - Math.Floor(t)) * 1000000);
return a.ToString("x8") + b.ToString("x5");
}
This code provides the exact same result, the only difference is that extra parameters of uniqid() are not implemented.