Created
April 1, 2017 07:45
-
-
Save smutneja03/26fd17a48ac9d7675fef295eb61888d0 to your computer and use it in GitHub Desktop.
Algorithm to check whether an aadhaar number is valid or not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class VerhoeffValidator { | |
/** | |
* @see <a href="http://en.wikipedia.org/wiki/Verhoeff_algorithm/">More | |
* Info</a> | |
* @see <a href="http://en.wikipedia.org/wiki/Dihedral_group">Dihedral | |
* Group</a> | |
* @see <a href="http://mathworld.wolfram.com/DihedralGroupD5.html">Dihedral | |
* Group Order 10</a> | |
* @author Colm Rice | |
*/ | |
public static class Verhoeff { | |
// The multiplication table | |
static int[][] sMultiplicationTable = new int[][] { { 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[][] sPermuationTable = new int[][] { { 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 } }; | |
// The inverse table | |
static int[] sInverseTable = { 0, 4, 3, 2, 1, 5, 6, 7, 8, 9 }; | |
/* | |
* For a given number generates a Verhoeff digit | |
*/ | |
public static String generateVerhoeff(String num) { | |
int c = 0; | |
int[] myArray = stringToReversedIntArray(num); | |
for (int i = 0; i < myArray.length; i++) { | |
c = sMultiplicationTable[c][sPermuationTable[((i + 1) % 8)][myArray[i]]]; | |
} | |
return Integer.toString(sInverseTable[c]); | |
} | |
/* | |
* Validates that an entered number is Verhoeff compliant. NB: Make sure | |
* the check digit is the last one. | |
*/ | |
public static boolean validateVerhoeff(String num) { | |
int c = 0; | |
int[] myArray = stringToReversedIntArray(num); | |
for (int i = 0; i < myArray.length; i++) { | |
c = sMultiplicationTable[c][sPermuationTable[(i % 8)][myArray[i]]]; | |
} | |
return (c == 0); | |
} | |
/* | |
* Converts a string to a reversed integer array. | |
*/ | |
private static int[] stringToReversedIntArray(String num) { | |
int[] myArray = new int[num.length()]; | |
for (int i = 0; i < num.length(); i++) { | |
try { | |
myArray[i] = Integer.parseInt(num.substring(i, i + 1)); | |
} catch (NumberFormatException e) { | |
myArray[i] = 0; | |
} | |
} | |
myArray = reverse(myArray); | |
return myArray; | |
} | |
/* | |
* Reverses an int array | |
*/ | |
private static int[] reverse(int[] myArray) { | |
int[] reversed = new int[myArray.length]; | |
for (int i = 0; i < myArray.length; i++) { | |
reversed[i] = myArray[myArray.length - (i + 1)]; | |
} | |
return reversed; | |
} | |
} | |
public static boolean validate(String s) { | |
s = s.replaceAll(" ", ""); | |
if (s.length() == 12 && Verhoeff.validateVerhoeff(s)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment