/ walking bear:D / 信用卡號檢驗

信用卡號檢驗

2019-11-12 posted in [筆記]

Brian W. Kernighan 世界是數字的 (D is for Digital) 9.8 错误检测和校正

BM 的 Peter Luhn 于 1954 年设计的一个 checksum 算法, 来检测在实际操作中最常见的两种错误:单个数字错误、由于两个数字写错位置 而引起的大多数换位错误。这个算法很简单: 从最右一位数开始向左,把每个数字交替乘 1 或 2,如果结果大于 9 就减 9。 如果把各位数的计算结果加起来,最后得到的总和能被 10 整除,那这个卡号 就是有效卡号。你可以用这个方法测试一下自己的银行卡,或者某些银行广告中出 现的卡号,如“4417 1234 5678 9112”。由于这个卡号计算的结果是 69, 所以不是真卡号;如果把它的最后一个数字换成 3,那就是有效卡号了。

Luhn 算法 一般用于验证身份识别码,例如发卡行识别码、国际移动设备辨识码(IMEI),美国国家提供商标识号码,或是加拿大社会保险号码,C++ Java sample code ,下列 Javascript 實作

function checksum(s) {
        let i = s.length;
        let sum = 0;
        let odd = true;
        while (--i >= 0) {
                if (s[i] < '0' || s[i] > '9')
                        throw "invalid string";
                let v = s[i] - '0';
                if (!odd) v *= 2;
                odd = !odd;
                sum += Math.floor(v / 10) + (v % 10);
        }
        return sum % 10 ? false : true;
}
console.log(`checksum :${checksum("79927398713")}`)
console.log(`checksum :${checksum("4417123456789112")}`)
console.log(`checksum :${checksum("4417123456789113")}`)

. reply_count: 0 get_replies : 0 .

walking bear:DRSS feed

关于

wkliang

Clarke's Three Laws:

  • When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
  • The only way of discovering the limits of the possible is to venture a little way past them into the impossible.
  • Any sufficiently advanced technology is indistinguishable from magic.
  • 版权申明

    知识共享许可协议

    Fork me on GitHub

    Powered by

    Disqus, GitHub, Google Custom Search, Gravatar, HighlightJS, jekyll