[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Hashing on more than one attribute?
- From: William Ahern <william@...>
- Date: Wed, 7 Nov 2012 21:10:34 -0800
On Wed, Nov 07, 2012 at 10:36:08PM -0500, Marc Lepage wrote:
> I know it's trivial to hash on a single value, by putting it into a table
> field. I want to effectively store a composite key in a table: say, a
> string, a number, and another string.
>
> Is there a way I can somehow produce a single hash of that combination of
> three values, to use as a key in a field? Something not obvious? Or must I
> effectively have a few levels of tables to hash 3 values? (And more for 4,
> 5, 6 values...)
>
The only way is the obvious way. To prevent malicious collisions you need
delimiters between the values when you concatenate. Those delimiters either
need to be something the user can't send (e.g. '\0'), or you'll need an
escaping mechanism (e.g. backslash escaping, or transform the strings to
something like base64 or hex, and choose a delimiter not in that character
set).
Or alternatively you could use a prefix system that encodes information in
the begining of the string.
local a = "foo", b = 2, c = "bar"
local key = string.format("%d:%d::%s:%d:%s", #a, #c, a, b, c)
Again you have to be careful that the user can't generate non-unique string
keys. For example:
a = "1"
c = "11111111111"
v
a = "11111111111"
c = "1"
In both cases, if you merely concatenated the strings you'd get the same
result. Also, if you merely concatented the decimal string length, you get
111 in both cases. With a delimiter between the string lengths, you'd get
1:11 and 11:1.