realbasic-nug
[Top] [All Lists]

Advice on Generating Unique Digits From String

To: REALbasic NUG <realbasic-nug at lists dot realsoftware dot com>
Subject: Advice on Generating Unique Digits From String
From: Kimball Larsen <kimball at kimballlarsen dot com>
Date: Tue, 30 Oct 2007 12:34:25 -0600
Delivered-to: listarchive at realsoftware dot com
Delivered-to: realbasic-nug at lists dot realsoftware dot com
So, I have a need to take any old string of any length, in any  
encoding, with any content (ok, it will typically be 32 chars or  
less, but that's not important - you'll see why in a moment) and  
convert it into a short(ish) number in integer form.

I've noodled this or a few minutes, and came up with this:  (s is the  
string in question)

   Dim m as MemoryBlock
   m = NewMemoryBlock(17)
   m.CString(0) = md5(s)
   return m.UInt16Value(0)

So, I allocate a memory block large enough to hold the 16 byte has  
>from the md5 function, pass s through MD5 to get the hash, and stuff  
that into the memory block as a string.  Then I grab the value of the  
memory block as an unsigned 2 byte integer, so the largest number it  
will return is 65535.

Now, my question for everyone on this list (all of whom are smarter  
than I) is this:  Will I have a reasonable expectation that each  
string I push into this will produce unique output?  ie, by using the  
MD5 hash, I guarantee that the string passed to the memory block only  
has 2^64 or 2^128 chance of having the same digest as any other  
string (which are acceptable odds for me).  Does that necessarily  
mean that the returned uint16value will have simliar odds of being  
the same value for arbitrarily passed in strings?

Thanks!


- Kimball
http://www.kimballlarsen.com

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>


<Prev in Thread] Current Thread [Next in Thread>