Reputation: 2232
I've read a similar question and Raymond Chan's blog post, but still have a question around using a shortened GUID as an id. I understand that GUIDs are unique, but substrings of GUIDs are not.
I am building a site that supplies the user with a reference number for an order. The client does not want to use an auto-incremented Int, as this will expose the total number of orders. They also consider GUIDs too long and difficult to repeat over the phone.
However, as we will only have one web server, it has been suggested that we can use the non-MAC address part of a GUID, to give a shorter, unique identifier.
My understanding is that the current GUID algorithm does not allow for this. Is this correct? Can GUIDs created on the same machine be shortened yet still unique?
Upvotes: 1
Views: 2727
Reputation: 66641
You can still go with an auto-incremented int (starting from 10000) and then ecoded it to an encrypted easy to read / hard to find string. This is still guarantee that the result is a fast unique number !
For example here are the code that you can do that
https://stackoverflow.com/a/5901201/159270
and some sample results.
value: 9999999999 encoded: SrYsNt
value: 4294965286 encoded: ZNGEvT
value: 2292964213 encoded: rHd24J
value: 1000000000 encoded: TrNVzD
And for encrypt it you only need to scramble your character map, and regular users having only few numbers and with out knowing the relation together, they are unable to recreate them.
Now if you won to go with the Guid and still search for find what you can cut off, here is the source code of it: http://www.webdav.org/specs/draft-leach-uuids-guids-01.txt
In this source code you can see how its build in, from all that part the next think that I will do it was to use the TimeStamp + a Random number, and then convert it again as I describe above.
Upvotes: 2
Reputation: 22076
I think this post address your problem and also tells solution as well.
http://madskristensen.net/post/Generate-unique-strings-and-numbers-in-C.aspx
It is taken from above link.
The System.Guid is used whenever we need to generate a unique key, but it is very long. That’s in many cases not an issue, but in a web scenario where it is part of the URL we need to use its string representation which is 36 characters long. It clutters up the URL and is just basically ugly.
It is not possible to shorten it without loosing some of the uniqueness of the GUID, but we can come a long way if we can accept a 16 character string instead.
We can change the standard GUID string representation:
21726045-e8f7-4b09-abd8-4bcc926e9e28
Into a shorter string:
3c4ebc5f5f2c4edc
The following method creates the shorter string and it is actually very unique. An iteration of 10 million didn’t create a duplicate. It uses the uniqueness of a GUID to create the string.
private string GenerateId() { long i = 1; foreach (byte b in Guid.NewGuid().ToByteArray()) { i *= ((int)b + 1); } return string.Format("{0:x}", i - DateTime.Now.Ticks); }
If you instead want numbers instead of a string, you can do that to but then you need to go up to 19 characters. The following method converts a GUID to an Int64.
private long GenerateId() { byte[] buffer = Guid.NewGuid().ToByteArray(); return BitConverter.ToInt64(buffer, 0); }
The standard GUID is still the best way to ensure the uniqueness even though it isn’t 100% unique.
Upvotes: 1