SlxLuhnLibrary 1.0.2

# SlxLuhnLibrary
The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers in the United States, Canadian Social Insurance Numbers, Israel ID Numbers and Greek Social Security Numbers (ΑΜΚΑ). It was created by IBM scientist Hans Peter Luhn and described in U.S. Patent No. 2,950,048, filed on January 6, 1954, and granted on August 23, 1960.

Here I also implement the Luhn mod N algorithm which is an extension to the Luhn algorithm (also known as mod 10 algorithm) that allows it to work with sequences of non-numeric characters. This can be useful when a check digit is required to validate an identification string composed of letters, a combination of letters and digits or even any arbitrary set of characters.

Install-Package SlxLuhnLibrary -Version 1.0.2
dotnet add package SlxLuhnLibrary --version 1.0.2
paket add SlxLuhnLibrary --version 1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

SlxLuhnLibrary

The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers in the United States, Canadian Social Insurance Numbers, Israel ID Numbers and Greek Social Security Numbers (ΑΜΚΑ). It was created by IBM scientist Hans Peter Luhn and described in U.S. Patent No. 2,950,048, filed on January 6, 1954, and granted on August 23, 1960.

Here I implement the Luhn mod N algorithm which is an extension to the Luhn algorithm (also known as mod 10 algorithm) that allows it to work with sequences of non-numeric characters. This can be useful when a check digit is required to validate an identification string composed of letters, a combination of letters and digits or even any arbitrary set of characters.

To test:

  1. Generate a new string with the luhn verification charater for a string (composed with modulo 10 caracters) :
ClsLuhnLibrary.WithLuhn_Base10("35823805800008"); 
// Returns "358238058000088"
  1. Generate the luhn verification charater for a string (composed with modulo 10 caracters) :
ClsLuhnLibrary.GenerateCheckCharacter("453908962903274", ClsLuhnLibrary.CharacterSet.Base10);
// Returns '4'
  1. Validate a composed with modulo 10 characters String :
ClsLuhnLibrary.ValidateCheckCharacter("4539089629032744", ClsLuhnLibrary.CharacterSet.Base10);
//  Returns true

Bonus Points
You've got the same thing for built in Base36_0to9_atoz, Base62_0to9_atoz_AtoZ, and user set of characters

ClsLuhnLibrary.Init_BaseUser(new Char[] { '#', '!', '*' });
String str = "**##!!";
Char? cRc = ClsLuhnLibrary.GenerateCheckCharacter(str, ClsLuhnLibrary.CharacterSet.BaseUser);
Assert.AreEqual('*', cRc, $"The result of GenerateCheckCharacter for {str} is '*'");

2018-08-24 Patch the CheckLuhn_BaseUser

String strBaseUser = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Boolean blRc = ClsLuhnLibrary.CheckLuhn_BaseUser("A11993F", strBaseUser.ToCharArray());
Assert.AreEqual(blRc, true, $"The result of CheckLuhn_BaseUser for A11993L is OK");
blRc = ClsLuhnLibrary.CheckLuhn_BaseUser("A11993M", strBaseUser.ToCharArray());
Assert.AreEqual(blRc, false, $"The result of CheckLuhn_BaseUser for A11993L is OK");

SlxLuhnLibrary

The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers in the United States, Canadian Social Insurance Numbers, Israel ID Numbers and Greek Social Security Numbers (ΑΜΚΑ). It was created by IBM scientist Hans Peter Luhn and described in U.S. Patent No. 2,950,048, filed on January 6, 1954, and granted on August 23, 1960.

Here I implement the Luhn mod N algorithm which is an extension to the Luhn algorithm (also known as mod 10 algorithm) that allows it to work with sequences of non-numeric characters. This can be useful when a check digit is required to validate an identification string composed of letters, a combination of letters and digits or even any arbitrary set of characters.

To test:

  1. Generate a new string with the luhn verification charater for a string (composed with modulo 10 caracters) :
ClsLuhnLibrary.WithLuhn_Base10("35823805800008"); 
// Returns "358238058000088"
  1. Generate the luhn verification charater for a string (composed with modulo 10 caracters) :
ClsLuhnLibrary.GenerateCheckCharacter("453908962903274", ClsLuhnLibrary.CharacterSet.Base10);
// Returns '4'
  1. Validate a composed with modulo 10 characters String :
ClsLuhnLibrary.ValidateCheckCharacter("4539089629032744", ClsLuhnLibrary.CharacterSet.Base10);
//  Returns true

Bonus Points
You've got the same thing for built in Base36_0to9_atoz, Base62_0to9_atoz_AtoZ, and user set of characters

ClsLuhnLibrary.Init_BaseUser(new Char[] { '#', '!', '*' });
String str = "**##!!";
Char? cRc = ClsLuhnLibrary.GenerateCheckCharacter(str, ClsLuhnLibrary.CharacterSet.BaseUser);
Assert.AreEqual('*', cRc, $"The result of GenerateCheckCharacter for {str} is '*'");

2018-08-24 Patch the CheckLuhn_BaseUser

String strBaseUser = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Boolean blRc = ClsLuhnLibrary.CheckLuhn_BaseUser("A11993F", strBaseUser.ToCharArray());
Assert.AreEqual(blRc, true, $"The result of CheckLuhn_BaseUser for A11993L is OK");
blRc = ClsLuhnLibrary.CheckLuhn_BaseUser("A11993M", strBaseUser.ToCharArray());
Assert.AreEqual(blRc, false, $"The result of CheckLuhn_BaseUser for A11993L is OK");

  • .NETStandard 2.0

    • No dependencies.

Version History

Version Downloads Last updated
1.0.2 53 8/24/2018
1.0.1 133 4/16/2018
1.0.0 130 4/16/2018