Generate Unique Key for Vouchers (asp.net vb.net)
findel | Posted 8:35am 14. June 2007 Server Time |
Hello All! Some of you may remember me, I used to post on here a long time ago. Anyway, I'm struggling with something and I thought that this might be a good place to bring it up.
I'm trying to create a Voucher system to go onto a booking website. One of the requirements is that the admin can create a load of vouchers in bulk. Each voucher has a Voucher Code, which allows the customer to use the voucher online. Each Voucher Code must obviously be Unique, and not easy to guess by people, so it can not be incremental.
So, the question is; How would you recommend I handle and create the Unique Voucher Codes?
I have thought about using RNGCryptoServiceProvider as used here : http://www.codeproject.com/useritems/UniqueKeys.asp : But I am not too sure about that.
Any ideas?
P.s. Hello to all you oldies! |
tribe | Posted 11:10am 14. June 2007 Server Time |
Dim strRandom As String = System.Guid.NewGuid().ToString()
mp3cdman | Posted 6:49am 15. June 2007 Server Time |
You could create an MD5 hash or SHA1 hash to give a longer code... and MD5 hash of the current time should be fairly unique...
you'll obviously have to store them somewhere (like a db) so you could make that field a unique field and then catch any errors that are generated when inserting the code and take these as duplicates.
alternatively just create your own random string but obviously you'll still have to check for uniqueness
mp3cdman ;-P
findel | Posted 6:59am 15. June 2007 Server Time |
Yeah, but how long is a GUID? pretty long.
E.G. 541894c1-667c-4ba2-af4c-e8a4fda668d2
it's not the sort of code that you will want to read out over the phone, or have to enter into a text input.
I'm thinking of something more like 10 chars in length. I am also thinking that maybe it doesn't have to be garenteed unique, I can put a check into the DB to make sure that it hasn't already been used.
findel | Posted 7:03am 15. June 2007 Server Time |
mp3, yes, you have beat me to it. i should have learned not to take 15 minutes to post a message. haha.
findel | Posted 8:55am 15. June 2007 Server Time |
I'm using something like this, if anyone wants to reuse this:
----------
Private Function GetUniqueCode() As String
Dim allChars As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
Dim GotUniqueCode As Boolean = False
Dim uniqueCode As String = ""
While Not GotUniqueCode
Dim str As New System.Text.StringBuilder
For i As Byte = 1 To 10 'length of req key
Dim xx As Integer
Randomize()
xx = Rnd() * (Len(allChars) - 1) 'number of rawchars
str.Append(allChars.Trim.Chars(xx))
Next
uniqueCode = str.ToString
GotUniqueCode = IsUniqueCode(uniqueCode)
End While
Return uniqueCode
End Function
--------------
IsUniqueCode checks the DB to make sure it is a unique code, but the chances of it actually being a match is very very slim.
mp3cdman | Posted 12:05am 15. June 2007 Server Time |
why not just do an insert into the database and detect any exceptions that are thrown..
it just means that you are only hitting the database once each time around the loop.
mp3cdman ;-)
Also don't Dim xx within the loop as you don't need to keep recreating it. just move this out the loop
findel | Posted 1:40am 18. June 2007 Server Time |
Im not to sure about this now anway. It produced far more collisions that I would have expected. I don't know if any of you can see the problem in my code?
mp3cdman | Posted 3:56am 18. June 2007 Server Time |
if you are trying to use just upper/lower alphabet and numbers then why not just generate an MD5 has and get a 32 digit hash..
Imports System.Text
Imports System.Security.Cryptography
Private Function GenerateUniqueKey() As String
Dim U As New UnicodeEncoding()
'Create an MD5 object
Dim Md5 As New MD5CryptoServiceProvider()
'Calculate a hash value from the Time
Dim MyUniqueKey() As Byte = Md5.ComputeHash(Ue.GetBytes(System.DateTime.Now().ToString()))
'And convert it to String format to return
Return Convert.ToBase64String(MyUniqueKey)
End Function
then you can split it in two to get 2 16-character hashes
Dim MyKey As String = GenerateUniqueKey()
Dim Key1 As String = MyKey.SubString(0,16)
Dim Key2 As String = MyKey.SubString(16,16)
you could even do what MS sometimes do and split it into segments with -
Dim Key1 As String = MyKey.SubString(0,4) + "-" + MyKey.SubString(4,4) + "-" + MyKey.SubString(8,4) + "-" + MyKey.SubString(12,4)
Dim Key1 As String = MyKey.SubString(16,4) + "-" + MyKey.SubString(20,4) + "-" + MyKey.SubString(24,4) + "-" + MyKey.SubString(28,4)
mp3cdman ;-P
findel | Posted 5:12am 18. June 2007 Server Time |
32 characters is far too long, and 16 is to really. these voucher codes need to be usable.
What I have done instead is generate a 5 character code, using my previous code, but I now prefix it with the db ID of the voucher. 13232-2De5G.
This means that every code will be unique (because of the ID), but because of the 5 char code no body will be able to easily guess the full voucher code. the 5 char code has 916,132,832 different possibilities.
I'm pretty sure that this will work nicely. Thanks for all your help!
findel | Posted 5:13am 18. June 2007 Server Time |
Oh, obviously this means that I don't have to double check with the DB before creating the code, because it is 100% unique now.
mp3cdman | Posted 9:45am 18. June 2007 Server Time |
interesting solution...
very well done...
mp3cdman ;-O
justtopost | Posted 12:08am 2. February 2011 Server Time |
I'm looking for similar way to generate an unique id for parking voucher. I like your solution. Thumbs up
Reply to Post Generate Unique Key for Vouchers (asp.net vb.net)
|
|
|