Re: Unique code for every user
On Tue, 6 Oct 2009, rossum wrote:
On Tue, 6 Oct 2009 06:03:46 -0700 (PDT), Barry
<magnus.moraberg@gmail.com> wrote:
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.
You need something reversible, so a cypher is indicated. For 128 bit
numbers use AES, for 64 bit numbers use 3DES.
For smaller numbers use the Hasty Pudding cypher which allows variable
block sizes, as many bits as you want.
Alternatively, build your own simple block cypher using the Feistel
structure. The NSA will be able to break it but you may not need
something that secure.
Curses! Someone beat me to it! Hasty Pudding lets you have non-integer
number of bits, BTW.
This post:
http://stackoverflow.com/questions/931247/ensuring-that-items-that-have-been-viewed-are-not-seen-again/966822#966822
Is fundamentally about "a function which maps the combination of an
integer in the range 0...N and a key to another integer in the range
0...N, such that for any value off the key, the mapping between integers
is bijective", which is precisely what you need. You put your internal
count in, and get a cryptic number out - and you can turn that cryptic
number back into a count. The linked thread on sci.crypt is perhaps more
informative for you.
Here's a small implementation:
http://urchin.earth.li/~twic/Code/HastyPudding/HastyPuddingCipher.java
Which sits on top of a little Feistel cipher i cooked up:
http://urchin.earth.li/~twic/Code/HastyPudding/TinyCipher.java
Using the demo to encrypt and decrypt the first twenty integers, we get
(encrypted counter in the middle):
0 247322710 0
1 397471032 1
2 265460931 2
3 116603216 3
4 789076229 4
5 118350116 5
6 061011385 6
7 093714976 7
8 871443473 8
9 949200669 9
10 257431838 10
11 614149477 11
12 902471066 12
13 541231587 13
14 403434035 14
15 486488836 15
16 741761752 16
17 925791929 17
18 767002650 18
19 256853025 19
Would that suit you?
If you want you use the code itself, the license is here:
http://urchin.earth.li/~twic/Code/license.txt
tom
--
i'm prepared to do anything as long as someone else works out how to do
it and gives me simple instructions... -- Sean