Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm for incrementing a String in a non-obvious manner

I want to create random-looking 5 or 6 character alpha-numeric strings, something like:

Vg78KY

Creating (pseudo-)random Strings has been answered, but I am wondering if there is an algorithm for incrementing a String in a non-obvious manner. A simple increment of the above String might yield:

Vg78KZ

But I don't want this next String to be guessable, I want it to look completely different. Of course, successive increments should not yield a previous result as each should be unique.

Any thoughts on how to achieve this much appreciated!

Thanks

like image 204
Richard H Avatar asked Jul 06 '11 13:07

Richard H


2 Answers

An easy approach that avoids the need for lookup tables would be:

  • Increment an integer normally
  • Permute the bits in a non-obvious way (a fixed permutation is probably fine, but if you want something more sophisticated you could use something like George Marsaglia's XORShift algorithm that produces a psuedorandom sequence of integers that only repeats after a very long cycle)
  • Convert to Base64 encoded strings
like image 148
mikera Avatar answered Oct 11 '22 22:10

mikera


If we assume there must be a 1:1 mapping from "sequence number" to "random-looking string", then the truncated hash approach will not work as there is no guarantee that the truncated hash won't be subject to collisions.

I'd do something like this:

  • Take the next integer in sequence.
  • Xor with a fixed number.
  • Permute the bits.
  • Encode the number using Base64, Base36, or whatever.

Note that this will be subject to easy analysis by a determined attacker with access to a sufficiently large set of sequence numbers.

like image 44
Anomie Avatar answered Oct 11 '22 23:10

Anomie