/* isikukood.cpp Copyright (C) 2008 n0nst0p This source code is provided 'as-is', without any express or implied warranty. In no event will the author be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this source code must not be misrepresented; you must not claim that you wrote the original source code (provide reference to this code). If you use this source code in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original source code. -n0nst0p */ #include #include #include bool validate(unsigned char sn[11]); void output_serial(unsigned char sn[11]); void usage(); int quotient(int z) { return z/10; } int reminder(int z) { return z%10; } int main(int argc, char **argv) { unsigned char serialnumber[11]; memset(serialnumber, 0, 11); if (argc!=2) { usage(); return 1; } if (strlen(argv[1])!=7) { usage(); return 1; } int n=atoi(argv[1]); serialnumber[6]=reminder(n); n=quotient(n); serialnumber[5]=reminder(n); n=quotient(n); serialnumber[4]=reminder(n); n=quotient(n); serialnumber[3]=reminder(n); n=quotient(n); serialnumber[2]=reminder(n); n=quotient(n); serialnumber[1]=reminder(n); serialnumber[0]=quotient(n); int valid=0; int invalid=0; for (int i=0; i<10000; i++) { unsigned char num[4]; int n=i; num[3]=reminder(n); n=quotient(n); num[2]=reminder(n); n=quotient(n); num[1]=reminder(n); num[0]=quotient(n); serialnumber[7]=num[0]; serialnumber[8]=num[1]; serialnumber[9]=num[2]; serialnumber[10]=num[3]; if (validate(serialnumber)) { valid++; output_serial(serialnumber); } else { invalid++; } } //printf("Valid: %d (%d%%); Invalid: %d (%d%%)\n", valid, valid*100/10000, invalid, invalid*100/10000); return 0; } bool validate(unsigned char sn[11]) { int sum=0; for (int i=0; i<10; i++) { sum+=sn[i]*(i==9?1:(i+1)); } int mod=sum%11; if (mod<10 && mod==sn[10]) return true; if (mod==10) { sum = sn[0]*3 + sn[1]*4 + sn[2]*5 + sn[3]*6 + sn[4]*7 + sn[5]*8 + sn[6]*8 + sn[7]*1 + sn[8]*2 + sn[9]*3; mod=sum%11; if (mod<10 && mod==sn[10]) return true; if (mod==10 && sn[10]==0) return true; } return false; } void output_serial(unsigned char sn[11]) { for(int i=0; i<11; i++) printf("%d", sn[i]); printf("\n"); } void usage() { printf( "Usage: ./isikukood \n" "\tIsikukood: GYYMMDDxxxx\n" "\tIsikukoodi algus = GYYMMDD\n" ); }