From f44fdc7dd016de2ef1be64df79bd579015fa1967 Mon Sep 17 00:00:00 2001 From: Alex Brown Date: Sun, 21 Oct 2018 18:57:20 -0500 Subject: [PATCH] added luhn's checksum algorithm --- Checksums/luhn.js | 16 ++++++++++++++++ README.md | 7 +++++++ 2 files changed, 23 insertions(+) create mode 100644 Checksums/luhn.js diff --git a/Checksums/luhn.js b/Checksums/luhn.js new file mode 100644 index 000000000..486f57e97 --- /dev/null +++ b/Checksums/luhn.js @@ -0,0 +1,16 @@ +function luhn(card_number) { + var sum_of_odds = 0; + var sum_of_evens = 0; + for (var i = 15; i > 0; i-=2) { + sum_of_odds += parseInt(card_number[i]); + } + for (var i = 14; i > -1; i-=2) { + var current_number = parseInt(card_number[i]); + if (parseInt(card_number[i])*2 > 9) { + sum_of_evens += (i*2)-9; + } else { + sum_of_evens += i*2; + } + } + return 0 === ((sum_of_odds + sum_of_evens) % 10); +} diff --git a/README.md b/README.md index ebbfdd0ed..adcfe321b 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,13 @@ In cryptography, a **transposition cipher** is a method of encryption by which t Mathematically a bijective function is used on the characters' positions to encrypt and an inverse function to decrypt. ###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Transposition_cipher) +---------------------------------------------------------------------------------------------------------------------- + +## Checksums + +### Luhn's +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. +###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Transposition_cipher) [bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort [bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort [bubble-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Bubblesort-edited-color.svg/220px-Bubblesort-edited-color.svg.png "Bubble Sort"