Aadhaar Validation in C# using Verhoeff algorithm.

 To validate Aadhaar numbers in C#, you can use the Verhoeff algorithm, which is the checksum algorithm used by the Indian UIDAI to verify Aadhaar numbers. Here's an example implementation:

Code Example for Aadhaar Validation in C#


using System;

public class AadhaarValidator
{
    // Multiplication table
    private static readonly int[,] d = new int[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}
    };

    // Permutation table
    private static readonly int[,] p = new int[8, 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}
    };

    // Inverse table
    private static readonly int[] inv = { 0, 4, 3, 2, 1, 5, 6, 7, 8, 9 };

    public static bool IsValidAadhaar(string aadhaar)
    {
        if (aadhaar.Length != 12 || !ulong.TryParse(aadhaar, out _))
        {
            return false; // Must be 12 digits
        }

        int c = 0; // Initial checksum value
        int[] aadhaarDigits = Array.ConvertAll(aadhaar.ToCharArray(), c => c - '0');

        // Process digits in reverse order
        for (int i = 0; i < aadhaarDigits.Length; i++)
        {
            c = d[c, p[(i % 8), aadhaarDigits[aadhaarDigits.Length - 1 - i]]];
        }

        return c == 0; // Valid if checksum is 0
    }

    public static void Main()
    {
        string[] aadhaarNumbers = 
        {
            "458758412433"
        };

        foreach (string aadhaar in aadhaarNumbers)
        {
            Console.WriteLine($"Aadhaar {aadhaar} is " + (IsValidAadhaar(aadhaar) ? "Valid" : "Invalid"));
        }
    }
}

Explanation:

  1. Input Validation: Aadhaar must be a 12-digit numeric string.
  2. Checksum Calculation: Process each digit in reverse order using the Verhoeff algorithm. Use the d table for multiplication and p table for permutations. Check if the final checksum value is 0.

Verhoeff Algorithm

The Aadhaar validation using the Verhoeff algorithm ensures that the given Aadhaar number follows a specific checksum pattern. This mechanism helps detect errors in the number (e.g., accidental digit changes or swaps).

Here’s a detailed breakdown of how it works:


1. Understanding the Verhoeff Algorithm

The Verhoeff algorithm is a checksum validation algorithm that uses:

  • multiplication table (d) for calculating intermediate results.
  • permutation table (p) for scrambling the positions of the digits.
  • An inverse table (inv) to determine whether the number is valid.

The algorithm works by processing digits from right to left, applying rules to compute a checksum.


2. Components of the Algorithm

Multiplication Table (d)

This table determines how digits are combined in intermediate steps. For example:

  • If d[x, y] is 3, the result of multiplying digit x by y is 3 under this algorithm.

Permutation Table (p)

This table rearranges the positions of digits for each step in the checksum calculation. For example:

  • p[0, y] is the original position of digit y, while p[1, y], p[2, y], etc., represent how that position is scrambled based on step number.

Inverse Table (inv)

This table provides the "valid checksum" value. For a number to be valid, the final checksum (c) must be 0.


3. Steps of the Verhoeff Algorithm

  1. Input Validation: Ensure the input is exactly 12 digits (Aadhaar numbers must be numeric and have a fixed length).
  2. Reverse Processing: Process the digits from rightmost to leftmost. This helps calculate the checksum accurately.
  3. Checksum Calculation:
  4. Validation:


4. Code Explanation

Key Parts of the Code

  1. Input Check:
  2. Reverse Processing:
  3. Final Validation:

Complete Flow with Example

Let’s validate the Aadhaar number 458758412433:

  1. Initial Conditions:
  2. Checksum Calculation:
  3. Result:


5. Test the Implementation

You can run the provided C# program to validate multiple Aadhaar numbers. Each number will be processed, and the program will indicate whether it is valid or not.


Why Use the Verhoeff Algorithm?

  • Error Detection: Catches common errors such as: A single incorrect digit. Two digits swapped.
  • Lightweight: It’s computationally efficient and well-suited for applications requiring fast validation.


Previous Post Next Post

ads

ads

نموذج الاتصال