summaryrefslogtreecommitdiff
path: root/server/utils/aadhaar.js
blob: 559a3416b8791553a7db36b1bdef4df39429b93e (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
114
115
116
117
118
119
120
import {
  ONE_TEN_TIMES_ZERO,
  NINE_TEN_TIMES_ZERO,
} from "../constants/numbers.js";

// const generateCheckSumDigit = (randomNumberArray) => {
//   const d = [
//     [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],
//   ];

//   const p = [
//     [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],
//   ];

//   const inv = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9];

//   let c = 0;
//   for (let i = 0; i < randomNumberArray.length; i++) {
//     c = d[c][p[(i + 1) % 8][randomNumberArray[i]]];
//   }
//   return inv[c];
// };

// const generateAadhaar = () => {
//   const aadhaarNumberArray = [];
//   let randomNumber = Math.floor(Math.random() * ELEVEN_TIMES_NINE + 1);

//   while (randomNumber != 0) {
//     const digit = randomNumber % 10;
//     aadhaarNumberArray.push(digit);
//     randomNumber = Math.floor(randomNumber / 10);
//   }

//   const checkSumDigit = generateCheckSumDigit(aadhaarNumberArray);
//   aadhaarNumberArray.reverse();
//   aadhaarNumberArray.push(checkSumDigit);
//   let aadhaarNumber = 0;
//   aadhaarNumberArray.forEach((value, index) => {
//     aadhaarNumber = aadhaarNumber + value;
//     if (index !== aadhaarNumberArray.length - 1) {
//       aadhaarNumber = aadhaarNumber * 10;
//     }
//   });

//   return aadhaarNumber;
// };

function generate(array) {
  const d = [
    [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],
  ];

  const p = [
    [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],
  ];
  const inv = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9];

  let c = 0;
  for (let i = 0; i < array.length; i++) {
    c = d[c][p[(i + 1) % 8][array[i]]];
  }
  return inv[c];
}

function generateAadhaar() {
  const aadhaarArray = [];
  let x = Math.floor(ONE_TEN_TIMES_ZERO + Math.random() * NINE_TEN_TIMES_ZERO);

  while (x != 0) {
    const digit = x % 10;
    aadhaarArray.push(digit);
    x = Math.floor(x / 10);
  }
  const last_digit = generate(aadhaarArray);
  aadhaarArray.reverse();
  aadhaarArray.push(last_digit);
  let ans = 0;
  aadhaarArray.forEach((value, index) => {
    ans = ans + value;
    if (index !== aadhaarArray.length - 1) {
      ans = ans * 10;
    }
  });

  return ans;
}

export default generateAadhaar;