 |
|
|
 |
|
 |
Hi,
I downloaded and tried to run the code but it generates 234 keys using Guid and it did not generate any key if I am trying to generate it using RNG Character Mask, please reply is there any thing wrong I am doing?
love_chopra1 2:39 1 Aug '06
|
|
|
|
 |
|
 |
Why cant you use double entry to keep it unique-? Instead of going through the whole extra coding-?
Dim stA as string
stA=ucase(Guid.NewGuid().ToString().GetHashCode().ToString("x") & Guid.NewGuid().ToString().GetHashCode().ToString("x"))
Or if you need to be more unique just append more lines to it.
stA=ucase(Guid.NewGuid().ToString().GetHashCode().ToString("x") & Guid.NewGuid().ToString().GetHashCode().ToString("x") & Guid.NewGuid().ToString().GetHashCode().ToString("x"))
debug.pring(stA)
You get the point..
|
|
|
|
 |
|
|
 |
|
 |
Hi,
thanks for this information.
first i have rewritten your code a bit and i got different execution times. Then i downloaded also your code and an my executions are similar then the once of my rewritten once. In your article you describe, that using "RNGCryptoServiceProvider and Character Masking" you have an execution time of: 00.40 sec. In all my tests this is the slowest one but yes it does not contain dublication even with 10 mio. generations
here is a result set:
Total of: 999997, GenTime: 2406.3578 ms
Total of: 131, GenTime: 2390.7321 ms
Total of: 0, GenTime: 8094.1126 ms
Done!
When i'm chaning the mask to: 1234567890 then its a bit faster:
Total of: 999997, GenTime: 2328.2293 ms
Total of: 105, GenTime: 2312.6036 ms
Total of: 999999, GenTime: 6547.1683 ms
Done!
<big>UPDATE:</big>
I done some additional researches and I found the following link: <a href="http://madskristensen.net/post/Generate-unique-strings-and-numbers-in-C.aspx">http://madskristensen.net/post/Generate-unique-strings-and-numbers-in-C.aspx</a>[<a href="http://madskristensen.net/post/Generate-unique-strings-and-numbers-in-C.aspx" target="_blank" title="New Window">^</a>]
private long GenerateId1()
{
byte[] buffer = Guid.NewGuid().ToByteArray();
return BitConverter.ToInt64(buffer, 0);
}
private string GenerateId2()
{
long i = 1;
foreach (byte b in Guid.NewGuid().ToByteArray())
{
i *= ((int)b + 1);
}
return string.Format("{0:x}", i - DateTime.Now.Ticks);
}
so i added both methods and here are the results:
Total of: 999997, GenTime: 2312.5296 ms
Total of: 112, GenTime: 2312.5296 ms
Total of: 999999, GenTime: 6500.0832 ms
Total of: 0, GenTime: 1546.8948 ms -> GenerateId1
Total of: 0, GenTime: 3093.7896 ms -> GenerateId2
Done!
I should mention that I used GenerateId1().ToString() to receive an ID.
and here an additional result set with 10'000'000:
Total of: 9999976, GenTime: 22937.7936 ms
Total of: 11666, GenTime: 43500.5568 ms
Total of: 9999999, GenTime: 65547.714 ms
Total of: 0, GenTime: 23594.052 ms
Total of: 0, GenTime: 62313.2976 ms
Done!
regards,
roni schuetz
modified on Tuesday, August 18, 2009 3:57 AM
|
|
|
|
 |
|
 |
In Cryptograph namespace, there are many other classes, for many uses, for example: Symmetric Algorithms, Asymmetric algorithms, NonKeyed Hashing algorithms, and Keyed Hashing algorithms.
|
|
|
|
 |
|
 |
I had a problem using the
Guid.NewGuid().ToString().GetHashCode().ToString("x");
and the
DateTime.Now.Ticks.ToString("x")
i kept it in a for loop of 10 and it gave me duplicates
so i knew it was useless,
but your method using the crypto is great.
Thanks again.
Regards
|
|
|
|
 |
|
|
 |
|
 |
thanks for youre helpful guide , thanks to anybody how writes code example an put it free to others
|
|
|
|
 |
|
 |
Altaf Najvani
I tried to translate the CS Version of your uniqueKey Program, but some is lost in the translation. Was wondering if you could help me figure it out. I keep getting the a string back 8 times in a long string. Tried to link to you on Link website, but was not successful.
Dim maxSize As Integer = 8
Dim minSize As Integer = 5
Dim chars(61) As Char
Dim a As String
a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
chars = a.ToCharArray()
Dim size As Integer = maxSize
Dim data(0) As Byte
Dim crypto As New RNGCryptoServiceProvider()
crypto.GetNonZeroBytes(data)
size = maxSize
data = New Byte(size - 1) {}
crypto.GetNonZeroBytes(data)
Dim result As New StringBuilder(size)
For Each b As Byte In data
result.Append(chars)
Next b
Return result.ToString()
|
|
|
|
 |
|
 |
JKIRKERX,
I found this to work with your translation :
For Each b As Byte In data
result.Append(chars(b Mod (chars.Length - 1)))
Next
Otherwise the loop is continuously adding the contents of chars.
|
|
|
|
 |
|
 |
That was a long time ago, I trying to remember if that was the unique key for software protection, or the unique key for payment procesing transactions. I ended up using this, that allows for up to 100,000 unique keys, but I know I need several million of them.
This program could use a boost to increase the amount of unique keys. I use it for SecureNet Transactions, because each transaction needs a unique key for submission. I didn't want to use the record index with identity seed, in case of accidental duplication.
Public Shared Function MakeUniqueKey2() As String
Dim maxSize As Integer = 8
Dim minSize As Integer = 5
Dim chars(61) As Char
Dim a As String
a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
chars = a.ToCharArray()
Dim size As Integer = maxSize
Dim data(0) As Byte
Dim crypto As New RNGCryptoServiceProvider()
crypto.GetNonZeroBytes(data)
size = maxSize
data = New Byte(size - 1) {}
crypto.GetNonZeroBytes(data)
Dim result As New StringBuilder(size)
For Each b As Byte In data
result.Append(chars)
Next b
Return result.ToString()
End Function
|
|
|
|
 |
|
 |
Now I remember, I could not get the program to work, but wanted to use it. Thanks a million, for the help, I will try it out later today.
I ended up using this, not sure which is better
Dim UniqueID As String = Nothing
UniqueID = DateTime.Now.ToString().GetHashCode().ToString("x")
|
|
|
|
 |
|
 |
What happen if I want to use just numbers?
chars = New Char(9) {}
a = "1234567890"
|
|
|
|
 |
|
 |
it will work! I m using it in my system !! there wil b collisions but not so much !! u can experiment it with little change in attached source code.
|
|
|
|
 |
|
 |
Thank you for a very nice and usefull article.
|
|
|
|
 |
|
 |
A short and concise example is like an image. It's worth a thousand words.
Thanks, Co.
|
|
|
|
 |
|
 |
It's really an amazing article. And it is going to help me in my project.
|
|
|
|
 |
|
|
 |
|
 |
In your code i.e
private string GetUniqueKey()
{
int maxSize = 8 ;
int minSize = 5 ;
char[] chars = new char[62];
string a;
a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
chars = a.ToCharArray();
int size = maxSize ;
byte[] data = new byte[1];
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data) ;
size = maxSize ;
data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size) ;
foreach(byte b in data )
{ result.Append(chars1)>); }
return result.ToString();
}
I am getting an error in the line"result.Append(chars1)>);" Can u point out d error. Please as i am finding your code very useful. If it works. Please help me out.
|
|
|
|
 |
|
 |
come on man.. take a look at the sources
laziness will eventually get to you
Co.
|
|
|
|
 |
|
 |
100,000 keys
0 dups
1,000,000 keys
0 dups
10,000,000 keys
0 dups
I ran each of these with 5 concurrent threads generating and adding keys to the same hashtable. Each test was executed 3 times.
w/ the speed that these are generated in it wouldn't be hard to grab a new one if it wasn't unique. I'm going to use this method for a file system data store to uniquely name the files. Granted I'll never even come close to having even 10,000 files under one directory so this solution is both efficient and effective for my needs.
|
|
|
|
 |
|
 |
private string GetUniqueKey()
{
int maxSize = 8 ;
int minSize = 5 ;
char[] chars = new char[62];
string a;
a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
chars = a.ToCharArray();
int size = maxSize ;
byte[] data = new byte[1];
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data) ;
size = maxSize ;
data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size) ;
foreach(byte b in data )
{ result.Append(chars1)>;); }
return result.ToString();
}
i have to use it for my project.... wen i ran this code i got an error in the 'foreach' loop...
the error is coming on the... result.append(chars1)>;); line..
please let me know wot the error is and how i can solve it....
thanks..
|
|
|
|
 |
|
 |
i have a gridview which is storing a record .i want to generate a unique no while saving one record.
the format should be like (pscode(2place),year(4place)and num(4place))
example:- 01-2007-0001
where ps_code is char(2),
year comes form date-which is of date and time datattype
and the last 4 place are char(4),
where ps_code and date are the field of my data base.i want retrive ps_code and year from my database to generate the unique number.
can tell me how will i do it?
i am devloping a web applcation where the backend is vb.net2005.
|
|
|
|
 |
|
 |
I beleive that it would be a better approach with the collision with seeded values would make the collisions negligible.
|
|
|
|
 |