Checksum Using the Verhoeff Algorithm

Introduction

A crucial component of many applications, particularly those involving sensitive data or financial transactions, is data integrity. Checksum validation is a frequently employed technique for guaranteeing data integrity. The Verhoeff algorithm is a checksum technique that is used to identify mistakes such consecutive digit transpositions. We'll explain and use the Verhoeff method in C# in this article.

Previous article

How to implement the Verhoeff algorithm in C# for calculating checksums?

Now let's get started with the C# implementation of the Verhoeff algorithm. The Verhoeff algorithm's C# implementation and checksum calculation and validation procedures are shown below.

using System;
using System.Linq;

public class Verhoeff
{
    // The multiplication table
    static int[,] mulTable = 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[] permutation = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    // The inverse table
    static int[] inverse = new int[] { 0, 4, 3, 2, 1, 5, 6, 7, 8, 9 };

    // Method to calculate the checksum
    public static int CalculateChecksum(int[] number)
    {
        int checksum = 0;
        int[] reversedNumber = number.Reverse().ToArray(); // Reverse the number
        for (int i = 0; i < reversedNumber.Length; i++)
        {
            checksum = mulTable[checksum, permutation[reversedNumber[i]]];
        }
        return inverse[checksum];
    }

    // Method to validate the checksum
    public static bool ValidateChecksum(int[] numberWithChecksum)
    {
        int checksum = 0;
        for (int i = 0; i < numberWithChecksum.Length; i++)
        {
            checksum = mulTable[checksum, permutation[numberWithChecksum[i]]];
        }
        return checksum == 0;
    }

    // Test the algorithm
    static void Main(string[] args)
    {
        int[] number = { 1, 2, 3, 4, 5 }; // Example input
        int checksum = CalculateChecksum(number);
        Console.WriteLine("Checksum: " + checksum);
        
        int[] numberWithChecksum = { 1, 2, 3, 4, 5, checksum }; // Example input with checksum
        bool isValid = ValidateChecksum(numberWithChecksum);
        Console.WriteLine("Is valid: " + isValid);
    }
}
Output:
Checksum: 5
Is valid: True

Conclusion

The Verhoeff technique offers a dependable way to identify data entry errors, particularly those brought on by transpositions or swapping neighboring digits. Developers can improve the integrity of their application's validation and data processing processes by implementing the Verhoeff algorithm in C#. An explanation of the Verhoeff method and a workable C# implementation have been given in this article. To increase data integrity and reduce errors, think about including this technique into your apps.


Recommended Free Ebook
Similar Articles