tag:blogger.com,1999:blog-40601577465686311762018-03-05T13:49:12.970-08:00An Inka Coding4Funinka jhonhttp://www.blogger.com/profile/10451555135592926867noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-4060157746568631176.post-29697886898759225112012-07-07T12:04:00.003-07:002012-07-07T16:16:44.206-07:00Validation Character for Peruvian DNI - 2Following 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 <a href="https://github.com/inkacoder/pirhua/blob/master/dni.cpp" target="_blank">here</a>. <pre class="brush:cpp;">char alphaValidationCharacter(char *DNI)<br />{<br /><br /> const char serie[] = "89456789";<br /> char const *t = serie;<br /><br /> char *s = DNI;<br /><br /> int sum = 0, partial = 0;<br /><br /> while(*s!='\0')<br /> {<br /> partial = (*s-'0')* (*t-'0');<br /> sum += partial;<br /> s++;<br /> t++;<br /> }<br /><br /> int modulus = 0;<br /> modulus = sum % 11;<br /> <br /> return 'A' + (modulus==0?11:modulus) - 1;<br /><br />} </pre> The complete project can be downloaded from <a href="https://github.com/inkacoder/pirhua/tarball/master" target="_blank">here</a>inka jhonhttp://www.blogger.com/profile/10451555135592926867noreply@blogger.com0tag:blogger.com,1999:blog-4060157746568631176.post-196679461054886622012-06-16T22:42:00.002-07:002016-04-27T07:42:24.444-07:00Validation Character for Peruvian DNI - 1Well, 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.<br />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.<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://1.bp.blogspot.com/-qrbReKFpbOI/T91wq496pBI/AAAAAAAAAAo/FEB3n5KbeOg/s1600/dni.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="48" src="https://1.bp.blogspot.com/-qrbReKFpbOI/T91wq496pBI/AAAAAAAAAAo/FEB3n5KbeOg/s320/dni.jpg" width="320" /></a></div><br />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:<br />"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."<br />The set of examples can be obtained from a public service <a href="https://cel.reniec.gob.pe/valreg/valreg.do?accion=ini" target="_blank">here</a>.<br />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.<br /><br />In my next post I'll show you the algoritm in C++. inka jhonhttp://www.blogger.com/profile/10451555135592926867noreply@blogger.com03142-3100 NW 57th Ave, Aeropuerto de Miami (MIA), Miami, Florida 33122, EEUU25.799891182088331 -80.288085937522.182317682088332 -85.341796937499993 29.41746468208833 -75.234374937500007