realbasic-nug
[Top] [All Lists]

Re: Advice on Generating Unique Digits From String

To: REALbasic NUG <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: Advice on Generating Unique Digits From String
From: Charles Yeomans <charles at declareSub dot com>
Date: Tue, 30 Oct 2007 15:11:06 -0400
Delivered-to: listarchive at realsoftware dot com
Delivered-to: realbasic-nug at lists dot realsoftware dot com
References: <72FE594F-FD4C-4427-8F5D-6C48208DC1F7 at kimballlarsen dot com>
On Oct 30, 2007, at 2:34 PM, Kimball Larsen wrote:

> 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?


No, because there are only 2^16 distinct values of a UInt16.  And I  
don't believe that MD5 guarantee that it reproduces equidistributed  
results; it claims that given a string, it is hard to find another  
string having the same MD5 hash as the first string.

I'd suggest first using Variant.Hash, which returns a 32-bit value,  
and see if that suffices for your needs.

Charles Yeomans
_______________________________________________
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>