验证应用程序中的Aadhar卡号

时间:2014-12-29 09:35:20

标签: java spring-mvc

我们正在开发一个应用程序,需要检查用户是否输入有效的" AADHAR"数量与否。我找到一些链接和一些" apis"但是没有达到最终要求请给我一些有用的材料来解决这个问题

What is Aadhaar?

Aadhaar是印度独特身份认证机构代表印度政府签发的12位个人识别号码。

6 个答案:

答案 0 :(得分:20)

我认为你正在寻找Verhoeff algorithm,因为UIDAI使用这个算法来验证aadhar数。你只需要创建并使用下面的类。

class VerhoeffAlgorithm{
        static int[][] d  = 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}
                };
        static int[][] p = 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}
                };
        static int[] inv = {0, 4, 3, 2, 1, 5, 6, 7, 8, 9};

        public static boolean validateVerhoeff(String num){
            int c = 0;
            int[] myArray = StringToReversedIntArray(num);
            for (int i = 0; i < myArray.length; i++){
                c = d[c][p[(i % 8)][myArray[i]]];
            }

            return (c == 0);
        }
        private static int[] StringToReversedIntArray(String num){
            int[] myArray = new int[num.length()];
            for(int i = 0; i < num.length(); i++){
                myArray[i] = Integer.parseInt(num.substring(i, i + 1));
            }
            myArray = Reverse(myArray);
            return myArray;
        }
        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;
        }
    }

更多信息: -

http://en.wikibooks.org/wiki/Algorithm_Implementation/Checksums/Verhoeff_Algorithm https://groups.google.com/forum/#!topic/aadhaarauth/eB5hOU-Qtq0 http://simplybanking.wordpress.com/2013/07/14/the-actual-uidai-aadhaar-number-is-11-digits-long-and-not-12-digits/

修改: -

public static boolean validateAadharNumber(String aadharNumber){
        Pattern aadharPattern = Pattern.compile("\\d{12}");
        boolean isValidAadhar = aadharPattern.matcher(aadharNumber).matches();
        if(isValidAadhar){
            isValidAadhar = VerhoeffAlgorithm.validateVerhoeff(aadharNumber);
        }
        return isValidAadhar;
    }

答案 1 :(得分:2)

python3

def aadharNumVerify(adharNum: str) -> bool:
    """
    Takes a N digit aadhar number and
    returns a boolean value whether that is Correct or Not
    """
    verhoeff_table_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))

    verhoeff_table_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))

    # verhoeff_table_inv = (0, 4, 3, 2, 1, 5, 6, 7, 8, 9)

    def checksum(s: str) -> int:
        """For a given number generates a Verhoeff digit and
        returns number + digit"""
        c = 0
        for i, item in enumerate(reversed(s)):
            c = verhoeff_table_d[c][verhoeff_table_p[i % 8][int(item)]]
        return c

    # Validate Verhoeff checksum
    return checksum(adharNum) == 0 and len(adharNum) == 12

答案 2 :(得分:1)

适用于iOS的目标C

导入“AadhaarValidate.h”

@implementation AadhaarValidate

int _multiplicationTable[][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 }
};
int _permutationTable[][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 }
};
int _inverseTable[] = { 0, 4, 3, 2, 1, 5, 6, 7, 8, 9 };
+(BOOL)isValidateAadhaar:(NSString *)aaDhaarNumber{
    int c = 0;
    int len = (int)[aaDhaarNumber length];

    const char *numberInChar = [aaDhaarNumber UTF8String];

    for (int i = 0; i < len; ++i)
        c = _multiplicationTable[c][_permutationTable[(i % 8)][numberInCharenter code here[len - i - 1] - '0']];
    return c == 0;
}
@end
**Test**
> NSLog(@"Check aadhar number:%d",[AadhaarValidate
> isValidateAadhaar:@"*******"]);   

答案 3 :(得分:0)

使用此javascript

// 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);
}

 $(document).ready(function () {
  $('#UserForm').formValidation({
                    message: 'This value is not valid',
                    feedbackIcons: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                    },
                    fields: {aadhaar_no: {
                            validators: {
                                digits: {
                                    message: 'Please use numeric characters only.'
                                },
                                stringLength: {
                                    min: 12,
                                    max: 12,
                                    message: 'The aadhaar number must be 12 characters long'
                                }, identical: {
                                    field: 'c_aadhaar_number',
                                    message: 'The aadhaar number and its confirm field are not the same'
                                }, callback: {
                                    message: 'The input string is not a valid Aadhaar number.',
                                    callback: function (value, validator, $field) {
                                        return validate(value);
                                    }
                                }
                            }
                        }
 });
            });
<div class="form-group">
                                        <label for="aadhaar_number" class="col-lg-5 control-label">Aadhaar Number&nbsp;<span style="color: red">*</span></label>
                                        <div class="col-lg-7 ">
                                            <input maxlength="12" type="password" class="form-control" id="aadhaar_number" name="aadhaar_no" placeholder="Aadhaar Number" required="">
                                        </div>
                                    </div> 

<强>提供者 Wikipedia

答案 4 :(得分:0)

试试这个。

function validateAadhaar1(fieldData){                        

    console.log(fieldData.id);
    var lengthOfAadhaar = fieldData.value;                        

    var checkDigits = /^\d+$/.test(lengthOfAadhaar);

    var zero  =  /.!=000000000000/.test(lengthOfAadhaar); 
    var one   =  /.!=111111111111/.test(lengthOfAadhaar);
    var two   =  /.!=222222222222/.test(lengthOfAadhaar);
    var three =  /.!=333333333333/.test(lengthOfAadhaar);
    var four =   /.!=444444444444/.test(lengthOfAadhaar);
    var five =   /.!=555555555555/.test(lengthOfAadhaar);
    var six =    /.!=666666666666/.test(lengthOfAadhaar);
    var seven =  /.!=777777777777/.test(lengthOfAadhaar);
    var eight =  /.!=888888888888/.test(lengthOfAadhaar);
    var nine =   /.!=999999999999/.test(lengthOfAadhaar);


    if(lengthOfAadhaar.length!=12 || checkDigits == false || zero == false || one ==false || two ==false ||
        three == false || four == false || five == false || six == false || seven == false || eight == false 
        || nine == false)
    {               

        document.getElementById(fieldData.id).style.color = "#ff0202";    

        return false;

    } else{                         

        document.getElementById(fieldData.id).style.color = "#555";      

    }
    return true;
}    

答案 5 :(得分:0)

科特林

    fun isAadhaarValid(aadhaarNo: String): Boolean {
        //Removed extra characters
        val newAadhaarNo = aadhaarNo.replace("-", "", true)

        if (newAadhaarNo.length != 12
            || newAadhaarNo.toBigIntegerOrNull() == null
            || newAadhaarNo.first().toString().toInt() in 0..1
        ) {
            return false
        }

        val firstMatrix = arrayOf(
            intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
            intArrayOf(1, 2, 3, 4, 0, 6, 7, 8, 9, 5),
            intArrayOf(2, 3, 4, 0, 1, 7, 8, 9, 5, 6),
            intArrayOf(3, 4, 0, 1, 2, 8, 9, 5, 6, 7),
            intArrayOf(4, 0, 1, 2, 3, 9, 5, 6, 7, 8),
            intArrayOf(5, 9, 8, 7, 6, 0, 4, 3, 2, 1),
            intArrayOf(6, 5, 9, 8, 7, 1, 0, 4, 3, 2),
            intArrayOf(7, 6, 5, 9, 8, 2, 1, 0, 4, 3),
            intArrayOf(8, 7, 6, 5, 9, 3, 2, 1, 0, 4),
            intArrayOf(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
        )
        val secondMatrix = arrayOf(
            intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
            intArrayOf(1, 5, 7, 6, 2, 8, 3, 0, 9, 4),
            intArrayOf(5, 8, 0, 3, 7, 9, 6, 1, 4, 2),
            intArrayOf(8, 9, 1, 6, 0, 4, 3, 5, 2, 7),
            intArrayOf(9, 4, 5, 3, 1, 2, 6, 8, 7, 0),
            intArrayOf(4, 2, 8, 6, 5, 7, 3, 9, 0, 1),
            intArrayOf(2, 7, 9, 3, 8, 0, 6, 4, 1, 5),
            intArrayOf(7, 0, 4, 6, 9, 1, 3, 2, 5, 8)
        )

        var isValid = 0

        //Created reverse array
        val reversedIntArray = newAadhaarNo.chunked(1).map { it.toInt() }.toIntArray().reversedArray()

        //Apply Verhoeff algorithm
        for (i in reversedIntArray.indices) {
            isValid = firstMatrix[isValid][secondMatrix[i % 8][reversedIntArray[i]]]
        }
        return isValid == 0
    }