I would like to generate a 6-character alphanumeric code (eg. A3SJ8D) from a 32-bit positive integer where each code in the sequence does not appear similar to the previous.
- A3SJ8D
- G54FGS
- ...
This code must be reversible so that G54FGS could be translated back to 2, for example (1:1 mapping).
This "randomness" is not for security purposes but for very simple obfuscation. In other words, the method need not be "secure".
Edit
To clarify, I understand that the max possible value for an unsigned 32-bit integer, (2^32)-1, exceeds the max possible value of a 6-character alpha-numeric code using 10 digits and 26 letters, (36^6)-1. So, the positive integer to be encoded must not overflow the bounds established by the number of characters available to the code set.
Answered!
Example
Here is a simple code example in Javascript based on @nwellnhof's accepted answer below.
var Skip32 = require('skip32').Skip32,
key = "0123456789".split("").map(function(c) { return c.charCodeAt(0) }),
cipher = new Skip32(key),
codelen = 6,
radix = 36,
max = Math.pow(radix,codelen);
function numToCode(num) {
while ((num = cipher.encrypt(num)) >= max) {}
return num.toString(radix).toUpperCase();
}
function codeToNum(code) {
var num = parseInt(code,radix);
while ((num = cipher.decrypt(num)) >= max) {}
return num;
}