Reverse algorythm
I found on this site this algorytm, it's already explained but I can't understand it exactly:
var safety = /^[0-9a-z]$/i;
/**
* safeChars(String code)
* Rewrites input into a 'safe' code, remapping common mistakes into
* their most likely equivalent and removing invalid characters.
*
* String code: The string which contains the code
* returns: A corrected String containing the code as it most likely should be.
**/
function safeChars(code) {
code = code.toUpperCase();
var replace = { "A":false, "B":"B", "C":"D", "D":"D", "E":false, "F":"F", "G":"G", "H":"M", "I":"L", "J":"J", "K":"K", "L":"L", "M":"M", "N":"M", "O":"D", "P":"P", "Q":"D", "R":"R", "S":"S", "T":"T", "U":"V", "V":"V", "W":"W", "X":"X", "Y":false, "Z":"Z", "0":"D", "1":"L", "2":"Z", "3":false, "4":false, "5":"S", "6":"G", "7":"T", "8":"B", "9":false};
var i=0;
while (i < code.length) {
var charVal = code.charAt(i);
if (safety.test(charVal) && replace[charVal]!=false) {
code = code.replace(charVal,replace[charVal]);
i++;
} else code = code.replace(charVal,"");
}
return code;
}
/**
* toHexCode(String code)
* Rewrites input into it's hexadecimal equivalent for further
* checking of it's validity
*
* String code: The string which contains the code in characters
* returns: A string containing the hexadecimal value of the characters.
**/
function toHexCode(code) {
var hexCode="";
code = code.toUpperCase();
var toHex = {"B":"0", "D":"1", "F":"2", "G":"3", "J":"4", "K":"5", "L":"6", "M":"7", "P":"8", "R":"9", "S":"A", "T":"B", "V":"C", "W":"D", "X":"E", "Z":"F"};
for (var i=0; i<code.length;hexCode+=toHex[code.charAt(i++)]);
return hexCode;
}
/**
* hexDoubleOdd(String code)
* Doubles every other decimal, starting with the first.
*
* String code: The string which contains the code in hexadecimals
* returns: A string containing the hexadecimal value of the characters.
**/
function hexDoubleOdd(code){
var evenCode = "";
for (var i=0; i<code.length;i++){
var mod = i%2;
if (mod==0) evenCode+=(parseInt(code.charAt(i),16)*2).toString(16);
else evenCode+=code.charAt(i);
}
return evenCode;
}
/**
* addAll(String code)
* Adds all the individual decimals for the gross product of it's
* individual values.
*
* String code: The string which contains the code in hexadecimals
* returns: A numerical value with the product of all decimals.
**/
function addAll(code){
var numCode = 0;
for (var i=0;i<code.length;i++) {
var charVal = code.charAt(i).toUpperCase();
var val = parseInt(charVal,16);
numCode += val;
}
return numCode;
}
/**
* addAll(Input input)
* Uses all above methods to calculate the validity of the input.
*
* Input input: An html 'input' node which contains the code that
* has to be validated.
* returns: true if mod 16 is 0, false otherwise.
**/
function validateCode(input) {
var code = input.value.substring(0,input.value.length-1);
if (code.length < 9) return false;
var control = input.value.charAt(input.value.length-1);
if (!safety.test(control)) return false;
control = toHexCode(control);
code = toHexCode(safeChars(code));
//if (code.length < 9) return false;
code = hexDoubleOdd(code);
code = addAll(code + control);
return (code%16==0);
}
Can somebody tell me step by step what the algorythm does with the next input?
input: "SWFDXVFFGJ"
First it transformed it to HEX code:
"AD21EC2234"
Then it doubles every decimal:
2*2=4
1*2=2
2*2=4
2*2=4
3*2=6
4*2=8
And then it adds all the products of the decimals:
4+2+4+4+6+8= 28
At least 28%16= 12
So the value of the string "code" will be 12?
I hope somebody can explain the algorytm for me.

