Algorithm Implementation/Checksums/Damm Algorithm

From Wikibooks, open books for an open world
Jump to: navigation, search

Contents

C[edit]

/// <summary>
/// The Damm check digit
/// For more information cf. http://en.wikipedia.org/wiki/Damm_algorithm
/// </summary>
/// <remarks>
/// The array could be substituted with another 
/// totally anti-symmetric quasigroup.
/// This would affect the check digit.
/// </remarks>
char Taq(char* number)
{
  const char taqDhmd111rr[]=
    "0317598642""7092154863""4206871359""1750983426""6123045978"
    "3674209581""5869720134""8945362017""9438617205""2581436790";
  char interim='0';
  char* p;
  for(p=number;*p!='\0';++p){
    if((unsigned char)(*p-'0')>9)
      return '-'; //minus sign indicates an error: character is not a digit
    interim=taqDhmd111rr[(*p-'0')+(interim-'0')*10];
  }
  return interim;
}
 
char CalculateCheckDigit(char* numberWithoutCheckDigit)
{
  return Taq(numberWithoutCheckDigit);
}
 
typedef int BOOL;
BOOL IsCheckDigitValid(char* numberWithCheckDigit)
{
  return Taq(numberWithCheckDigit)=='0';
}


Python[edit]

"""Damm algorithm decimal check digit
 
For reference see http://en.wikipedia.org/wiki/Damm_algorithm
"""
 
# we use the matrix given in the WP article because it's a good one
matrix = (
    (0, 3, 1, 7, 5, 9, 8, 6, 4, 2),
    (7, 0, 9, 2, 1, 5, 4, 8, 6, 3),
    (4, 2, 0, 6, 8, 7, 1, 3, 5, 9),
    (1, 7, 5, 0, 9, 8, 3, 4, 2, 6),
    (6, 1, 2, 3, 0, 4, 5, 9, 7, 8),
    (3, 6, 7, 4, 2, 0, 9, 5, 8, 1),
    (5, 8, 6, 9, 7, 2, 0, 1, 3, 4),
    (8, 9, 4, 5, 3, 6, 2, 0, 1, 7),
    (9, 4, 3, 8, 6, 1, 7, 2, 0, 5),
    (2, 5, 8, 1, 4, 3, 6, 7, 9, 0)
)
 
 
def encode(number):
    number = str(number)
    interim = 0
 
    for digit in number:
        interim = matrix[interim][int(digit)]
 
    return interim
 
 
def check(number):
    return encode(number) == 0
 
 
if __name__ == '__main__':
    # quick sanity checking
    assert encode(572) == 4 # from wikipedia
    assert check(5724)
    assert encode('43881234567') == 9 # hand-computed

C#[edit]

    /// <summary>
    /// The implementation of the damm algorithm based on the details on http://en.wikipedia.org/wiki/Damm_algorithm
    /// </summary>
    public static class DammAlgorithm
    {
        /// <summary>
        /// The quasigroup table from http://en.wikipedia.org/wiki/Damm_algorithm
        /// </summary>
 
        static int[,] matrix = new int[,]
        {
            {0, 3, 1, 7, 5, 9, 8, 6, 4, 2},
            {7, 0, 9, 2, 1, 5, 4, 8, 6, 3},
            {4, 2, 0, 6, 8, 7, 1, 3, 5, 9},
            {1, 7, 5, 0, 9, 8, 3, 4, 2, 6},
            {6, 1, 2, 3, 0, 4, 5, 9, 7, 8},
            {3, 6, 7, 4, 2, 0, 9, 5, 8, 1},
            {5, 8, 6, 9, 7, 2, 0, 1, 3, 4},
            {8, 9, 4, 5, 3, 6, 2, 0, 1, 7},
            {9, 4, 3, 8, 6, 1, 7, 2, 0, 5},
            {2, 5, 8, 1, 4, 3, 6, 7, 9, 0}
        };
 
        /// <summary>
        /// Calculate the checksum digit from provided number
        /// </summary>
        /// <param name="number">the number</param>
        /// <returns>Damm checksum</returns>
        public static int CalculateCheckSum(string number)
        { 
            var numbers = (from n in number select int.Parse(n.ToString()));
            int interim = 0;
            var en = numbers.GetEnumerator();
            while (en.MoveNext())
            {
                interim = matrix[interim, en.Current];
            }
            return interim;
        }
 
        /// <summary>
        /// Calculate the checksum digit from provided number
        /// </summary>
        /// <param name="number">the number</param>
        /// <returns>Damm checksum</returns>
        public static int CalculateCheckSum(int number)
        {
            return CalculateCheckSum(number.ToString());
        }
 
        /// <summary>
        /// Calculate the checksum digit from provided number
        /// </summary>
        /// <param name="number">the number</param>
        /// <returns>Damm checksum</returns>
        public static int CalculateCheckSum(long number)
        {
            return CalculateCheckSum(number.ToString());
        }
 
        /// <summary>
        /// Calculate the checksum digit from provided number and return the full number with the checksum
        /// </summary>
        /// <param name="number">the number</param>
        /// <returns>full number with the Damm checksum</returns>
        public static string GenerateCheckSum(string number)
        {
            var checkSumNumber = CalculateCheckSum(number);
            return number + checkSumNumber.ToString();
        }
 
        /// <summary>
        /// Calculate the checksum digit from provided number and return the full number with the checksum
        /// </summary>
        /// <param name="number">the number</param>
        /// <returns>full number with the Damm checksum</returns>
        public static int GenerateCheckSum(int number)
        {
            var checkSumNumber = CalculateCheckSum(number);
            return (number * 10) + checkSumNumber;
        }
 
        /// <summary>
        /// Calculate the checksum digit from provided number and return the full number with the checksum
        /// </summary>
        /// <param name="number">the number</param>
        /// <returns>full number with the Damm checksum</returns>
        public static long GenerateCheckSum(long number)
        {
            var checkSumNumber = CalculateCheckSum(number);
            return (number * 10) + checkSumNumber;
        }
 
        /// <summary>
        /// validates the number using the last digit as the Damm checksum
        /// </summary>
        /// <param name="number">the number to check</param>
        /// <returns>True if valid; otherwise false</returns>
        public static bool Validate(string number)
        {
            return CalculateCheckSum(number) == 0;
        }
 
        /// <summary>
        /// validates the number using the last digit as the Damm checksum
        /// </summary>
        /// <param name="number">the number to check</param>
        /// <returns>True if valid; otherwise false</returns>
        public static bool Validate(int number)
        {
            return CalculateCheckSum(number) == 0;
        }
 
        /// <summary>
        /// validates the number using the last digit as the Damm checksum
        /// </summary>
        /// <param name="number">the number to check</param>
        /// <returns>True if valid; otherwise false</returns>
        public static bool Validate(long number)
        {
            return CalculateCheckSum(number) == 0;
        }
    }