using System; using System.Text; namespace Swarmops.Logic.Support { /// /// This class implements the Verhoeff check digit scheme. /// This is one of the best available check digit algorithms /// that works with any length input. /// See: http://www.cs.utsa.edu/~wagner/laws/verhoeff.html /// http://www.augustana.ca/~mohrj/algorithms/checkdigit.html /// http://modp.com/release/checkdigits/ /// public sealed class CheckDigit { #region Private Static Variables //----------------------------------------------------------------------------------------------- private static CheckDigit _instance; //----------------------------------------------------------------------------------------------- #endregion #region Private Instance Variables //----------------------------------------------------------------------------------------------- private readonly int[][] F = new int[8][]; private readonly int[] inv = {0, 4, 3, 2, 1, 5, 6, 7, 8, 9}; private readonly int[][] op = new int[10][]; //----------------------------------------------------------------------------------------------- #endregion #region Private Constructor //----------------------------------------------------------------------------------------------- private CheckDigit() { this.op[0] = new[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; this.op[1] = new[] {1, 2, 3, 4, 0, 6, 7, 8, 9, 5}; this.op[2] = new[] {2, 3, 4, 0, 1, 7, 8, 9, 5, 6}; this.op[3] = new[] {3, 4, 0, 1, 2, 8, 9, 5, 6, 7}; this.op[4] = new[] {4, 0, 1, 2, 3, 9, 5, 6, 7, 8}; this.op[5] = new[] {5, 9, 8, 7, 6, 0, 4, 3, 2, 1}; this.op[6] = new[] {6, 5, 9, 8, 7, 1, 0, 4, 3, 2}; this.op[7] = new[] {7, 6, 5, 9, 8, 2, 1, 0, 4, 3}; this.op[8] = new[] {8, 7, 6, 5, 9, 3, 2, 1, 0, 4}; this.op[9] = new[] {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; this.F[0] = new[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // identity permutation this.F[1] = new[] {1, 5, 7, 6, 2, 8, 3, 0, 9, 4}; // "magic" permutation for (int i = 2; i < 8; i++) { // iterate for remaining permutations this.F[i] = new int[10]; for (int j = 0; j < 10; j++) this.F[i][j] = this.F[i - 1][this.F[1][j]]; } } //----------------------------------------------------------------------------------------------- #endregion #region Public Static Methods #region AppendCheckDigit Method //----------------------------------------------------------------------------------------------- /// /// Calculates the Verhoeff check digit for the given input, then returns /// the input with the check digit appended at the end. /// /// The string for which the check digit is to be calculated. /// The input with the calculated check digit appended. public static string AppendCheckDigit (string input) { int[] resultArray = Instance._AppendCheckDigit (_ConvertToIntArray (input)); StringBuilder resultString = new StringBuilder(); for (int i = 0; i < resultArray.Length; i++) { resultString.Append (resultArray[i]); } return resultString.ToString(); } /// /// Calculates the Verhoeff check digit for the given input, then returns /// the input with the check digit appended at the end. /// /// The long integer for which the check digit is to be calculated. /// The input with the calculated check digit appended. public static long AppendCheckDigit (long input) { int[] resultArray = Instance._AppendCheckDigit (_ConvertToIntArray (input)); return _ConvertToLong (resultArray); } /// /// Calculates the Verhoeff check digit for the given input, then returns /// the input with the check digit appended at the end. /// /// The integer for which the check digit is to be calculated. /// The input with the calculated check digit appended. public static int AppendCheckDigit (int input) { int[] resultArray = Instance._AppendCheckDigit (_ConvertToIntArray (input)); long resultLong = _ConvertToLong (resultArray); return (int) resultLong; } /// /// Calculates the Verhoeff check digit for the given input, then returns /// the input with the check digit appended at the end. /// /// The integer array for which the check digit is to be calculated. /// The input with the calculated check digit appended. public static int[] AppendCheckDigit (int[] input) { return Instance._AppendCheckDigit (input); } //----------------------------------------------------------------------------------------------- #endregion #region CalculateCheckDigit Method //----------------------------------------------------------------------------------------------- /// /// Calculates the Verhoeff check digit for the given input. /// /// The string for which the check digit is to be calculated. /// The check digit for the input. public static int CalculateCheckDigit (string input) { return Instance._CalculateCheckDigit (_ConvertToIntArray (input)); } /// /// Calculates the Verhoeff check digit for the given input. /// /// The long integer for which the check digit is to be calculated. /// The check digit for the input. public static int CalculateCheckDigit (long input) { return Instance._CalculateCheckDigit (_ConvertToIntArray (input)); } /// /// Calculates the Verhoeff check digit for the given input. /// /// The integer for which the check digit is to be calculated. /// The check digit for the input. public static int CalculateCheckDigit (int input) { return Instance._CalculateCheckDigit (_ConvertToIntArray (input)); } /// /// Calculates the Verhoeff check digit for the given input. /// /// The integer array for which the check digit is to be calculated. /// The check digit for the input. public static int CalculateCheckDigit (int[] input) { return Instance._CalculateCheckDigit (input); } //----------------------------------------------------------------------------------------------- #endregion #region Check Method //----------------------------------------------------------------------------------------------- /// /// Verifies that a given string has a valid Verhoeff check digit as the last digit. /// /// /// The string for which the check digit is to be checked. The check digit is the last digit in the /// string. /// /// /// Returns true if the last digit of the input is the valid check digit for /// the input. Otherwise returns false. /// public static bool Check (string input) { return Instance._Check (_ConvertToIntArray (input)); } /// /// Verifies that a given long integer has a valid Verhoeff check digit as the last digit. /// /// /// The long integer for which the check digit is to be checked. The check digit is the last digit in /// the input. /// /// /// Returns true if the last digit of the input is the valid check digit for /// the input. Otherwise returns false. /// public static bool Check (long input) { return Instance._Check (_ConvertToIntArray (input)); } /// /// Verifies that a given integer has a valid Verhoeff check digit as the last digit. /// /// /// The integer for which the check digit is to be checked. The check digit is the last digit in the /// input. /// /// /// Returns true if the last digit of the input is the valid check digit for /// the input. Otherwise returns false. /// public static bool Check (int input) { return Instance._Check (_ConvertToIntArray (input)); } /// /// Verifies that a given integer array has a valid Verhoeff check digit as the last digit /// in the array. /// /// /// The integer array for which the check digit is to be checked. The check digit is the last element /// of the array. /// /// /// Returns true if the last digit of the input is the valid check digit for /// the input. Otherwise returns false. /// public static bool Check (int[] input) { return Instance._Check (input); } /// /// Verifies the Verhoeff check digit for a given string. /// /// /// The string for which the check digit is to be verified. The input /// does not include the check digit. /// /// The check digit to be verified. /// /// Returns true if the check digit is valid for /// the input. Otherwise returns false. /// public static bool Check (string input, int checkDigit) { return Instance._Check (_ConvertToIntArray (input), checkDigit); } /// /// Verifies the Verhoeff check digit for a given long integer. /// /// /// The long integer for which the check digit is to be verified. The input /// does not include the check digit. /// /// The check digit to be verified. /// /// Returns true if the check digit is valid for /// the input. Otherwise returns false. /// public static bool Check (long input, int checkDigit) { return Instance._Check (_ConvertToIntArray (input), checkDigit); } /// /// Verifies the Verhoeff check digit for a given integer. /// /// /// The integer for which the check digit is to be verified. The input /// does not include the check digit. /// /// The check digit to be verified. /// /// Returns true if the check digit is valid for /// the input. Otherwise returns false. /// public static bool Check (int input, int checkDigit) { return Instance._Check (_ConvertToIntArray (input), checkDigit); } /// /// Verifies the Verhoeff check digit for a given integer array. /// /// /// The integer array for which the check digit is to be verified. The input /// does not include the check digit. /// /// The check digit to be verified. /// /// Returns true if the check digit is valid for /// the input. Otherwise returns false. /// public static bool Check (int[] input, int checkDigit) { return Instance._Check (input, checkDigit); } //----------------------------------------------------------------------------------------------- #endregion //----------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------- #endregion #region Private Static Properties //----------------------------------------------------------------------------------------------- private static CheckDigit Instance { get { if (_instance == null) _instance = new CheckDigit(); return _instance; } } //----------------------------------------------------------------------------------------------- #endregion #region Private Static Methods //----------------------------------------------------------------------------------------------- private static int[] _ConvertToIntArray (string input) { int[] inputArray = new int[input.Length]; for (int i = 0; i < input.Length; i++) inputArray[i] = Convert.ToInt32 (input.Substring (i, 1)); return inputArray; } private static int[] _ConvertToIntArray (long input) { return _ConvertToIntArray (input.ToString()); } private static int[] _ConvertToIntArray (int input) { return _ConvertToIntArray (input.ToString()); } private static long _ConvertToLong (int[] input) { long result = 0; long power = 1; for (int i = 0; i < input.Length; i++) { result += input[input.Length - (i + 1)]*power; power *= 10; } return result; } //----------------------------------------------------------------------------------------------- #endregion #region Private Instance Methods //----------------------------------------------------------------------------------------------- private int[] _AppendCheckDigit (int[] input) { int checkDigit = _CalculateCheckDigit (input); int[] result = new int[input.Length + 1]; input.CopyTo (result, 0); result[result.Length - 1] = checkDigit; return result; } private int _CalculateCheckDigit (int[] input) { // First we need to reverse the order of the input digits int[] reversedInput = new int[input.Length]; for (int i = 0; i < input.Length; i++) reversedInput[i] = input[input.Length - (i + 1)]; int check = 0; for (int i = 0; i < reversedInput.Length; i++) check = this.op[check][this.F[(i + 1)%8][reversedInput[i]]]; int checkDigit = this.inv[check]; return checkDigit; } private bool _Check (int[] input) { // First we need to reverse the order of the input digits int[] reversedInput = new int[input.Length]; for (int i = 0; i < input.Length; i++) reversedInput[i] = input[input.Length - (i + 1)]; int check = 0; for (int i = 0; i < reversedInput.Length; i++) check = this.op[check][this.F[i%8][reversedInput[i]]]; return (check == 0); } private bool _Check (int[] input, int checkDigit) { int[] newInput = new int[input.Length + 1]; input.CopyTo (newInput, 0); newInput[newInput.Length - 1] = checkDigit; return _Check (newInput); } //----------------------------------------------------------------------------------------------- #endregion } }