sábado, 7 de julio de 2012

Validation Character for Peruvian DNI - 2

Following is the function that receives an eight character string, the DNI, and returns the validation character. The C++ code is published at GitHub and can be downloaded here.
char alphaValidationCharacter(char *DNI)

  const char serie[] = "89456789";
  char const *t = serie;

  char *s = DNI;

  int sum = 0, partial = 0;

    partial = (*s-'0')* (*t-'0');
    sum += partial;

  int modulus = 0;
  modulus = sum % 11;
  return 'A' + (modulus==0?11:modulus) - 1;

The complete project can be downloaded from here

sábado, 16 de junio de 2012

Validation Character for Peruvian DNI - 1

Well, as my first theme I want to tell you about the Peruvian National Identity Card, named DNI. The Peruvian DNI is given to all the peruvian nationals at the age of 18 and from some years ago is also given to all new born. To his parents, I mean.
As you guess the Peruvian DNI has a number, an 8-digit number that you have to remember for the rest of your life. But if you look carefully, following the 8-digit number there´s a number or a letter, the validation character.

I was googling for some time searching for the algorithm to calculate the validation character of the Peruvian DNI without success, so I decided to deduce it.  My approach was this:
"Consider a 9 characters length string where the last of this characters is calculated from first eight ones by an unknown algorithm you have to deduce the algorithm from a set of examples."
The set of examples can be obtained from a public service here.
I treated this problem like an encryption challenge, my first guess was that the algorithm is a modified form of modulus 11, and I was right. Then, with a set of 40 strings was easy to deduce the coeficients and the variations to the original modulus 11 algorithm.

In my next post I'll show you the algoritm in C++.