Skip to content

Instantly share code, notes, and snippets.

@thensg
Last active November 30, 2021 19:27
Show Gist options
  • Save thensg/07bd82f73a1f784a35f0 to your computer and use it in GitHub Desktop.
Save thensg/07bd82f73a1f784a35f0 to your computer and use it in GitHub Desktop.
Calculate or verify a Luhn (Mod10) checksum
/**
* MIT No Attribution
*
* Copyright 2015 Giovanni Thenstead
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*******************************************************************************
*
* Calculate a Luhn (Mod10) checksum
* -or-
* Verify the Luhn checksum of a credit card or IMEI number
*
* This implementation was adapted from the following sources:
* - https://en.wikipedia.org/w/index.php?title=Luhn_algorithm&oldid=688221366
* - https://gist.github.com/ShirtlessKirk/2134376
*/
var luhn = {
/* Calculates the Luhn checksum */
calculate: function(digits) {
var sum = this.sum(digits, false);
return (sum * 9) % 10;
},
/* Verifies if a number is a valid Luhn checksum */
verify: function(digits) {
var sum = this.sum(digits, true);
return sum > 0 && sum % 10 === 0;
},
/* Sum each digit from right to left, and double
every second digit. If the double exceeds 9,
then sum its digits (i.e., 654321 -> 358341
-> 24) */
sum: function(digits, even) {
var sum = 0,
digit = 0,
i = digits.length;
while (i--) {
digit = Number(digits[i]);
sum += (even = !even) ? this.computed[digit] : digit;
}
return sum;
},
/* Create a precomputed list based on doubling
each digit, as described in sum(). */
computed: [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
};
var helpers = {
/* Appends a Luhn checksum to the end of a number */
createLuhnId: function(number) {
var digits = String(number);
return digits + luhn.calculate(digits);
},
/* Checks if a credit card or IMEI number is valid */
isLuhnId: function(number) {
return luhn.verify(String(number));
}
};
@Wintersunner
Copy link

Thank you.

@heymartinadams
Copy link

heymartinadams commented Sep 29, 2017

Wonderful!

@hrieke
Copy link

hrieke commented Nov 30, 2021

Hi,
Do you have a license for your code?
Thanks

@thensg
Copy link
Author

thensg commented Nov 30, 2021

@hrieke Thanks for mentioning this. An open license (MIT-0) is now included with this code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment