-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo.js
81 lines (71 loc) · 2.16 KB
/
algo.js
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
// multiplication table d
var 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]
];
// permutation table p
var 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]
];
// inverse table inv
var inv = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9];
// converts string or number to an array and inverts it
function invArray(array){
if (Object.prototype.toString.call(array) == "[object Number]"){
array = String(array);
}
if (Object.prototype.toString.call(array) == "[object String]"){
array = array.split("").map(Number);
}
return array.reverse();
}
// generates checksum
function generate(array){
var c = 0;
var invertedArray = invArray(array);
for (var i = 0; i < invertedArray.length; i++){
c = d[c][p[((i + 1) % 8)][invertedArray[i]]];
}
return inv[c];
}
// validates checksum
function validate(array) {
var c = 0;
var invertedArray = invArray(array);
for (var i = 0; i < invertedArray.length; i++){
c=d[c][p[(i % 8)][invertedArray[i]]];
}
return (c === 0);
}
function validate_adhar(adhar) {
console.clear();
//pretty dumb but the easiest solution to know if the number is 12 digit or not :)
if (adhar >= 100000000000 && adhar <= 999999999999) {
if(validate(adhar) == false) {
console.log("Invalid Adhar Number");
return false;
}
else {
return true;
}
}
else {
console.log("Adhar Number Should Be 12 Digits");
return false;
}
}