Multiply by a reasonably large prime number p and reduce modulo 2^n where n
is big enough.
Since p and 2^n are relatively prime, this method will permute the numbers
in [0, 2^n). You can compute compute q such that p q = 1 mod 2^n pretty
easily which lets you invert this mapping easily.
This is definitely not cryptographic quality, but it works very well. If
somebody has access to nearly consecutive mapped numbers and they know your
method then they can invert it relatively easily. If they have access to
just a few numbers that are not near each other in the sequence it would be
difficult to reverse engineer.
If you want higher security, just use one of the standard ciphers built into
java such as AES (or even DES). It is just a few lines of code to encrypt a
64 bit number. Make sure you aren't using any kind of cipher feedback.
Speed is not super high, but is plenty fast compared to any disk operation
so it should be fine in the face of database delays.
On Thu, Jan 29, 2009 at 6:18 AM, nicolas de loof <nicolas@apache.org> wrote:
> Hi,
> I have to generate some alpanumeric ID based on a database sequence.
> The idea is to generate some ID that will not indicate the ordering in the
> database, or it's amount of data
>
> It's easy to convert some long to a String using a 26 based number
> conversion.
> I now need some mathematical function to "disperse" my sequencgenerated ID
> into the numbers space.
>
> Any suggestion ?
>
> Nicolas
>

Ted Dunning, CTO
DeepDyve
4600 Bohannon Drive, Suite 220
Menlo Park, CA 94025
www.deepdyve.com
6503240110, ext. 738
8584140013 (m)
