summaryrefslogtreecommitdiff
path: root/src/reducer/validation_IN_AADHAR.c
blob: f8a8ca68cc73dcd5694be39b36a0928cc89dd3dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
  This file is part of Anastasis
  Copyright (C) 2020, 2021 Anastasis SARL

  Anastasis is free software; you can redistribute it and/or modify it under the
  terms of the GNU Lesser General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along with
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file reducer/validation_IN_AADHAR.c
 * @brief validation logic for Indian Aadhar numbers
 * @author Christian Grothoff
 */
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

/**
 * The multiplication table.
 */
static int m[10][10] = {
  {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
  {1, 2, 3, 4, 0, 6, 7, 8, 9, 5},
  {2, 3, 4, 0, 1, 7, 8, 9, 5, 6},
  {3, 4, 0, 1, 2, 8, 9, 5, 6, 7},
  {4, 0, 1, 2, 3, 9, 5, 6, 7, 8},
  {5, 9, 8, 7, 6, 0, 4, 3, 2, 1},
  {6, 5, 9, 8, 7, 1, 0, 4, 3, 2},
  {7, 6, 5, 9, 8, 2, 1, 0, 4, 3},
  {8, 7, 6, 5, 9, 3, 2, 1, 0, 4},
  {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
};


/**
 * The permutation table.
 */
static int p[10][10] = {
  {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
  {1, 5, 7, 6, 2, 8, 3, 0, 9, 4},
  {5, 8, 0, 3, 7, 9, 6, 1, 4, 2},
  {8, 9, 1, 6, 0, 4, 3, 5, 2, 7},
  {9, 4, 5, 3, 1, 2, 6, 8, 7, 0},
  {4, 2, 8, 6, 5, 7, 3, 9, 0, 1},
  {2, 7, 9, 3, 8, 0, 6, 4, 1, 5},
  {7, 0, 4, 6, 9, 1, 3, 2, 5, 8}
};


/**
 * Converts a string to a reversed integer array.
 *
 * @param input The numeric string data converted to reversed int array.
 * @param[out] output array containing the digits in the numeric string
 * in reverse order
 */
static bool
string_to_vals (const char *input,
                int output[12])
{
  unsigned int off = 0;

  for (unsigned int i = 0; i < 12;)
  {
    int c = input[i + off];

    if (0 == c)
      return false;
    if (isspace (c))
    {
      off++;
      continue;
    }
    if (! isdigit (c))
      return false;
    output[11 - i++] = c - '0';
  }
  if ('\0' != input[12 + off])
    return false;
  return true;
}


/**
 * Function to validate an Indian Aadhar number.
 *
 * See https://www.geeksforgeeks.org/how-to-check-aadhar-number-is-valid-or-not-using-regular-expression/
 * and http://en.wikipedia.org/wiki/Verhoeff_algorithm/.
 *
 * @param aadhar_number aadhar number to validate (input)
 * @return true if validation passed, else false
 */
bool
IN_AADHAR_check (const char *aadhar_number)
{
  int c = 0;
  int vals[12];

  if (! string_to_vals (aadhar_number,
                        vals))
    return false;
  for (unsigned int i = 0; i < 12; i++)
    c = m[c][p[(i % 8)][vals[i]]];

  return (0 == c);
}