Non-Sequential unique Handle Generation
I'm designing a Socket-Based Communication System, and part of the system dpends on having Unique Handles.
It's pretty simple to generate the UHs, just request one from the Server, as the server is the only entity with authority to generate them.
Thing is, because they are sequencial, someone could easily change them.
So I'm trying to find a way to easily generate non-sequencial Unique Handles, that I know are unique, without having a huge array with all the handles already generated.
Any tips?
A (possibly silly?) idea would be to generate unique 64-bit values by using a 64-bit block cipher (e.g. DES) with a secret key, encrypting sequential numbers, like "int64 GetNewHandle() { static int64 counter = 0; return Encrypt(counter++, key); }". You can check that a handle is valid (i.e. has been generated in the past) by testing decrypt(handle, key) < counter. No handle will be repeated until the counter overflows. Nobody can generate a valid handle unless they break the encryption algorithm to determine your secret key, or unless they are lucky and choose a random value which happens to be a valid handle (and you could use 128-bit handles to decrease the chance of them being sufficiently lucky).
Or, more simply, if you have a known maximum number of handles, you can generate a queue (FILO) of available handles and then 'shuffle' it - i.e. sequentially generate the handles, then randomly swap 'em.
Every time you need a handle, just pull one off the queue, and when you're done, put it back on the end.
'Course you're still left with a limited range of values, but this can make debugging and message obfuscation easier - especially if you vary the precise range used on a per-connection basis (linked to any session key). Mind you, I can't really see a need for it.
Every time you need a handle, just pull one off the queue, and when you're done, put it back on the end.
'Course you're still left with a limited range of values, but this can make debugging and message obfuscation easier - especially if you vary the precise range used on a per-connection basis (linked to any session key). Mind you, I can't really see a need for it.
So you want a function that uniquley maps one integer value to another integer value?
eg:
#define PRIME_NUMBER_A 65521
#define PRIME_NUMBER_B 999721
#define PRIME_NUMBER_C 145709
unsigned int GetHandleForItemIndex( unsigned int in )
{
in =( PRIME_NUMBER_A*( in +PRIME_NUMBER_A ) );
in =( PRIME_NUMBER_A*( in +PRIME_NUMBER_B ) );
in =( PRIME_NUMBER_A*( in +PRIME_NUMBER_C ) );
in =( PRIME_NUMBER_B*( in +PRIME_NUMBER_A ) );
in =( PRIME_NUMBER_B*( in +PRIME_NUMBER_B ) );
in =( PRIME_NUMBER_B*( in +PRIME_NUMBER_C ) );
in =( PRIME_NUMBER_C*( in +PRIME_NUMBER_A ) );
in =( PRIME_NUMBER_C*( in +PRIME_NUMBER_B ) );
in =( PRIME_NUMBER_C*( in +PRIME_NUMBER_C ) );
return in;
}
eg:
#define PRIME_NUMBER_A 65521
#define PRIME_NUMBER_B 999721
#define PRIME_NUMBER_C 145709
unsigned int GetHandleForItemIndex( unsigned int in )
{
in =( PRIME_NUMBER_A*( in +PRIME_NUMBER_A ) );
in =( PRIME_NUMBER_A*( in +PRIME_NUMBER_B ) );
in =( PRIME_NUMBER_A*( in +PRIME_NUMBER_C ) );
in =( PRIME_NUMBER_B*( in +PRIME_NUMBER_A ) );
in =( PRIME_NUMBER_B*( in +PRIME_NUMBER_B ) );
in =( PRIME_NUMBER_B*( in +PRIME_NUMBER_C ) );
in =( PRIME_NUMBER_C*( in +PRIME_NUMBER_A ) );
in =( PRIME_NUMBER_C*( in +PRIME_NUMBER_B ) );
in =( PRIME_NUMBER_C*( in +PRIME_NUMBER_C ) );
return in;
}
Quote:Original post by Prozak
I'm designing a Socket-Based Communication System, and part of the system dpends on having Unique Handles.
It's pretty simple to generate the UHs, just request one from the Server, as the server is the only entity with authority to generate them.
Thing is, because they are sequencial, someone could easily change them.
So I'm trying to find a way to easily generate non-sequencial Unique Handles, that I know are unique, without having a huge array with all the handles already generated.
Any tips?
Hashing them would be the simplest way to distribute items uniformly across a random space.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement