Re: Unique code for every user
Barry wrote:
HI,
I'm building a system where I wish to give my users a unique code each
time they perform a transaction. On returning to my system, they can
then enter this code to retreive the data associated with it.
I number my transactions in assending order, 0, 1, 2 and so on, so I
need a function that will transform this value to a unique nine digit
number. I also need a function that will transform this value back
again to the transaction number.
Something like this -
long codeTransactionNumber(long transactionNumber)
{
return transactionNumber + 100000000;
}
long uncodeTransactionNumber(long transactionNumber)
{
return transactionNumber - 100000000;
}
One way to do this is with a Ruby-Lackoff Cipher. If your maximum number
is a billion, I don't see why you would need longs, so I'm using ints.
/**
* This generates code numbers in the range 100000000..999999999,
* which I presume is good enough.
*/
private static final int MOD=30000;
public static int encode(int key, int tranNumber) {
int right=tranNumber%MOD;
int left=tranNumber/MOD; // Ha! Probably always 0.
right=(hash30(key, left)+right)%MOD;
left=(hash30(key, right)+left)%MOD;
right=(hash30(key, left)+right)%MOD;
left=(hash30(key, right)+left)%MOD;
return left*MOD+right+100000000;
}
public static int decode(int key, int codeNumber) {
codeNumber-=100000000;
int right=codeNumber%MOD;
int left=codeNumber/MOD;
left=(MOD+left-hash30(key, right))%MOD;
right=(MOD+right-hash30(key, left))%MOD;
left=(MOD+left-hash30(key, right))%MOD;
right=(MOD+right-hash30(key, left))%MOD;
return left*MOD+right;
}
private static int hash30(int key, int data) {
return ((((((6385004+key)
*16777619)
^(data ^ 0x1FF))
*16777619)
^((data>>9) & 0x1FF))
&0x3FffFF)
%MOD;
}
You can choose the key arbitrarily, changing it when you want a
different correspondence between the transaction number and the 9-digit
code. Choosing the key as 1, I got
enc(1, 1)=542638646
enc(1, 2)=900674795
enc(1, 3)=261379172
enc(1, 4)=762769918
enc(1, 5)=419272937
enc(1, 6)=266757288
enc(1, 7)=783673375
enc(1, 8)=599992012
enc(1, 9)=972941767
enc(1, 10)=284982539
enc(1, 11)=663016948
enc(1, 12)=315379905
enc(1, 13)=803750650
enc(1, 14)=302380407
--Mike Amling