Re: Hash/permutation function for object ID creation



On Jul 29, 4:20 am, Bruza <benr...@xxxxxxxxx> wrote:
I need a system to generate a series of "semi-random-non-repeating"
32-bit object IDs. For this, I plan to create a sequence counter
(starting from 0), then feed the sequence number, s, to a permutation
function, F(s). And use F(s) as the generated object ID. F() needs to
have the following properties:

  1. F(x) is semi-random so that if the ID is partitioned into any
     number of "buckets" (by equal range, or MOD hashing), each bucket
     has roughly same number of objects.
  2. F() has an inverse F' s.t. F(F'(x)) = x
  3. Both F() and F'() are computationally "inexpensive" to compute

When I have to do this sort of thing for a simple hash table or
something, I usually do something simple like:

hash = ID+SOME_CONSTANT
hash *= SOME_ODD_CONSTANT
hash ^= (hash>>16)
hash ^= (hash>>8)

each line is reversible in modular arithmetic (mod 2^x), so the whole
function is reversible.

If you need something stronger, you can divide the ID into two halves
(L,R), and with a good hash function H(), do:

L' = L XOR H(R)
R' = R XOR H(L')

hash = (L',R')

This is called a Feistel network (googlable), and again each step is
obviously reversible.

Cheers,

Matt
.



Relevant Pages

  • =?windows-1252?Q?Re=3A_What=E2=80=99s_the_best_general_implementation_of?= =?windows-1252?Q?
    ... two objects are eql if their ivars are eql) a good general approach? ...     VALUE ary; ... This looks like a typical cryptographic hash algorithm pattern ... This scrambles the bits more than just a straight xor ...
    (comp.lang.ruby)
  • Re: Problem with search
    ...   ... What I am currently doing is using a hash table for the lowest level and ... the FSM has 5 states. ... If you end up at an accepting state when the whole reverse ...
    (comp.lang.lisp)
  • Re: URL. Hash, Encrypt, ...
    ...   For example with length equal to 8, which is the minimum length I ... is the basic minimum for security at the moment. ... The user will need to store their private key securely at their end to ... A SHA-256 hash is an array of bytes, which cannot be directly typed on ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Extend OpenStructs functionality with explicit default value
    ... store them in a Hash because of the nicer syntax of OpenStruct. ...   case sym ... I don't view it so much as complex initialization but rather ... If we are asking for just the OpenStruct ...
    (comp.lang.ruby)
  • Re: I am needing a gentle introduction to accessing a perl array from a reference
    ... It's still a hash, right? ... we still do not know where the hashes are coming from... ... values (the values associated with particular keys) ...     perldoc -f keys ...
    (comp.lang.perl.misc)