TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Luhn Algorithm for validating credit cards

53 pointsby mlissneralmost 10 years ago

12 comments

contingenciesalmost 10 years ago
Many of us from the pre-stripe era have implemented Luhn once or twice I&#x27;d say, very often in Javascript and web scripting languages! However, you really should avoid re-implementing this sort of thing, and particularly additional credit card checks as it&#x27;s easy to make spurious assumptions (eg. credit card prefix should equal country of address of credit card, or of mailing address) which are in fact invalid. This stuff prevents actual sales and frustrates your users.<p>For a slightly out there and arguably more interesting financial checksum algorithm, and one that is actually amazingly effective, do some statistics on mod97&#x27;s performance. Realizing how good it was, a few years back for a cool hack in my IBAN library I wrote a mistranscription error suggestion system, which plays off the strengths of mod97. See <a href="https:&#x2F;&#x2F;github.com&#x2F;globalcitizen&#x2F;php-iban&#x2F;blob&#x2F;master&#x2F;trunk&#x2F;php-iban.php#L320" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;globalcitizen&#x2F;php-iban&#x2F;blob&#x2F;master&#x2F;trunk&#x2F;...</a> and <a href="https:&#x2F;&#x2F;github.com&#x2F;globalcitizen&#x2F;php-iban&#x2F;blob&#x2F;master&#x2F;trunk&#x2F;mistranscriptions.txt" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;globalcitizen&#x2F;php-iban&#x2F;blob&#x2F;master&#x2F;trunk&#x2F;...</a> - for a quick hack, it&#x27;s amazingly effective.<p>PS. I am available for remote gigs doing creative or challenging stuff in finance-land at the moment, if anyone appreciates this sort of thing.
评论 #9824073 未加载
daspalmost 10 years ago
The first time I&#x27;ve seen the Luhn algorithm was on a compromised web server. The cracker has put several fake ebay login&#x2F;registration pages, as well as a couple of Javascript files.<p>The pages were obviously intended to be used for phishing. However, what really made me go &quot;wow&quot; was the extra mile they went: one of the Javascript files contained methods that took the credit card number from the fake registration page and checked its validity w&#x2F; the Luhn algorithm before emailing it to a random email address. If the number was invalid, the user got an error and they forced him to re-enter.<p>I didn&#x27;t know about the algorithm at the time (this was around 2005), so I was a bit baffled what the heck they are trying to do.
justinzollarsalmost 10 years ago
This was an interview question at both Square and Braintree. Its weird that interview questions trend from year to year.
评论 #9824027 未加载
评论 #9823363 未加载
评论 #9824156 未加载
Sharlinalmost 10 years ago
I find the most interesting thing about the algorithm is that, as mentioned in the article, it was originally designed to be computable by a simple mechanical device.
ratpikalmost 10 years ago
I had come across this while generating test credit card credentials while integrating a payment gateway. Had written a small Python utility for that. Here - <a href="https:&#x2F;&#x2F;github.com&#x2F;ratpik&#x2F;luhn" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ratpik&#x2F;luhn</a>
B-Conalmost 10 years ago
I wrote a bit of code that generates a random number that passes the Luhn test.[0] It was one of those random things I did in high school when I was learning to program.<p>[0]: <a href="http:&#x2F;&#x2F;bradconte.com&#x2F;cc_generator" rel="nofollow">http:&#x2F;&#x2F;bradconte.com&#x2F;cc_generator</a>
k8ttealmost 10 years ago
Luhn is also used for the last digit in Swedish social security numbers, <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Personal_identity_number_%28Sweden%29#Checksum" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Personal_identity_number_%28Sw...</a> which makes me remember doing this in school around age of 10. i wish the teacher then would call it an &quot;algorithm&quot;, so one wouldnt grow up thinking &quot;algorithms&quot; is something complex and hard to understand
评论 #9825017 未加载
lnanek2almost 10 years ago
This was great for getting a free month of AOL every month for years back when that was how many people connected to the internet. You just came up with a fake number that worked with this algorithm and you were good. There were even lots of automated applications to do that for you near the end such as AOHell: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;AOHell" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;AOHell</a>
woadwarrior01almost 10 years ago
There&#x27;s also the Damm algorithm[1], which is similar but better. [1]: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Damm_algorithm" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Damm_algorithm</a>
crazybobalmost 10 years ago
Square&#x27;s &quot;Luhny Bin&quot; challenge: <a href="https:&#x2F;&#x2F;corner.squareup.com&#x2F;2011&#x2F;11&#x2F;luhny-bin.html" rel="nofollow">https:&#x2F;&#x2F;corner.squareup.com&#x2F;2011&#x2F;11&#x2F;luhny-bin.html</a>
Nullset14almost 10 years ago
I came to know about this algorithm while solving Ruby Quiz[0]. Simple yet powerful [0]: <a href="http:&#x2F;&#x2F;rubyquiz.com&#x2F;quiz122.html" rel="nofollow">http:&#x2F;&#x2F;rubyquiz.com&#x2F;quiz122.html</a>
jimktrains2almost 10 years ago
I wish more common numbers had a built in check-code like that.