anastasis

Credential backup and recovery protocol and service
Log | Files | Refs | Submodules | README | LICENSE

validation_IN_AADHAR.c (2982B)


      1 /*
      2   This file is part of Anastasis
      3   Copyright (C) 2020, 2021 Anastasis SARL
      4 
      5   Anastasis is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 
     13   You should have received a copy of the GNU General Public License along with
     14   Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file reducer/validation_IN_AADHAR.c
     18  * @brief validation logic for Indian Aadhar numbers
     19  * @author Christian Grothoff
     20  */
     21 #include <string.h>
     22 #include <stdbool.h>
     23 #include <ctype.h>
     24 
     25 /**
     26  * The multiplication table.
     27  */
     28 static int m[10][10] = {
     29   {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
     30   {1, 2, 3, 4, 0, 6, 7, 8, 9, 5},
     31   {2, 3, 4, 0, 1, 7, 8, 9, 5, 6},
     32   {3, 4, 0, 1, 2, 8, 9, 5, 6, 7},
     33   {4, 0, 1, 2, 3, 9, 5, 6, 7, 8},
     34   {5, 9, 8, 7, 6, 0, 4, 3, 2, 1},
     35   {6, 5, 9, 8, 7, 1, 0, 4, 3, 2},
     36   {7, 6, 5, 9, 8, 2, 1, 0, 4, 3},
     37   {8, 7, 6, 5, 9, 3, 2, 1, 0, 4},
     38   {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
     39 };
     40 
     41 
     42 /**
     43  * The permutation table.
     44  */
     45 static int p[10][10] = {
     46   {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
     47   {1, 5, 7, 6, 2, 8, 3, 0, 9, 4},
     48   {5, 8, 0, 3, 7, 9, 6, 1, 4, 2},
     49   {8, 9, 1, 6, 0, 4, 3, 5, 2, 7},
     50   {9, 4, 5, 3, 1, 2, 6, 8, 7, 0},
     51   {4, 2, 8, 6, 5, 7, 3, 9, 0, 1},
     52   {2, 7, 9, 3, 8, 0, 6, 4, 1, 5},
     53   {7, 0, 4, 6, 9, 1, 3, 2, 5, 8}
     54 };
     55 
     56 
     57 /**
     58  * Converts a string to a reversed integer array.
     59  *
     60  * @param input The numeric string data converted to reversed int array.
     61  * @param[out] output array containing the digits in the numeric string
     62  * in reverse order
     63  */
     64 static bool
     65 string_to_vals (const char *input,
     66                 int output[12])
     67 {
     68   unsigned int off = 0;
     69 
     70   for (unsigned int i = 0; i < 12;)
     71   {
     72     int c = input[i + off];
     73 
     74     if (0 == c)
     75       return false;
     76     if (isspace (c))
     77     {
     78       off++;
     79       continue;
     80     }
     81     if (! isdigit (c))
     82       return false;
     83     output[11 - i++] = c - '0';
     84   }
     85   if ('\0' != input[12 + off])
     86     return false;
     87   return true;
     88 }
     89 
     90 
     91 /**
     92  * Function to validate an Indian Aadhar number.
     93  *
     94  * See https://www.geeksforgeeks.org/how-to-check-aadhar-number-is-valid-or-not-using-regular-expression/
     95  * and http://en.wikipedia.org/wiki/Verhoeff_algorithm/.
     96  *
     97  * @param aadhar_number aadhar number to validate (input)
     98  * @return true if validation passed, else false
     99  */
    100 bool
    101 IN_AADHAR_check (const char *aadhar_number);
    102 
    103 /* declaration to fix compiler warning */
    104 bool
    105 IN_AADHAR_check (const char *aadhar_number)
    106 {
    107   int c = 0;
    108   int vals[12];
    109 
    110   if (! string_to_vals (aadhar_number,
    111                         vals))
    112     return false;
    113   for (unsigned int i = 0; i < 12; i++)
    114     c = m[c][p[(i % 8)][vals[i]]];
    115 
    116   return (0 == c);
    117 }