{"id":2003,"date":"2023-04-01T11:34:54","date_gmt":"2023-04-01T03:34:54","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2003"},"modified":"2023-04-07T18:08:52","modified_gmt":"2023-04-07T10:08:52","slug":"bank-card-number-verification-algorithm-luhn-algorithm","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/bank-card-number-verification-algorithm-luhn-algorithm\/","title":{"rendered":"\u94f6\u884c\u5361\u53f7\u7801\u6821\u9a8c\u7b97\u6cd5\uff08Luhn\u7b97\u6cd5\uff0c\u53c8\u53eb\u6a2110\u7b97\u6cd5\uff09"},"content":{"rendered":"<p>\u6709\u65f6\u5019\u5728\u7f51\u4e0a\u529e\u7406\u4e00\u4e9b\u4e1a\u52a1\u65f6\u6709\u4e9b\u9700\u8981\u586b\u5199\u94f6\u884c\u5361\u53f7\u7801\uff0c\u5f53\u80e1\u4e71\u586b\u5199\u65f6\u4f1a\u7acb\u5373\u62a5\u9519\uff0c\u4f46\u662f\u5e76\u6ca1\u6709\u53d1\u73b0\u5411\u540e\u7aef\u53d1\u9001\u8bf7\u6c42\uff0c\u90a3\u4e48\u8fd9\u4e2a\u6548\u679c\u662f\u600e\u4e48\u5b9e\u73b0\u7684\u5462\u3002<\/p>\n<p>\u5bf9\u4e8e\u94f6\u884c\u5361\u53f7\u6709\u4e00\u4e2a\u6821\u9a8c\u7b97\u6cd5\uff0c\u53eb\u505aLuhn\u7b97\u6cd5\u3002<\/p>\n<h2>\u94f6\u884c\u5361\u53f7\u7801\u7684\u6821\u9a8c\u89c4\u5219<\/h2>\n<p><!-- more --><\/p>\n<p>\u94f6\u884c\u5361\u53f7\u7801\u7684\u6821\u9a8c\u91c7\u7528Luhn\u7b97\u6cd5\uff0c\u6821\u9a8c\u8fc7\u7a0b\u5927\u81f4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u4ece\u53f3\u5230\u5de6\u7ed9\u5361\u53f7\u5b57\u7b26\u4e32\u7f16\u53f7\uff0c\u6700\u53f3\u8fb9\u7b2c\u4e00\u4f4d\u662f1\uff0c\u6700\u53f3\u8fb9\u7b2c\u4e8c\u4f4d\u662f2\uff0c\u6700\u53f3\u8fb9\u7b2c\u4e09\u4f4d\u662f3<\/li>\n<li>\u4ece\u53f3\u5411\u5de6\u904d\u5386\uff0c\u5bf9\u6bcf\u4e00\u4f4d\u5b57\u7b26t\u6267\u884c\u7b2c\u4e09\u4e2a\u6b65\u9aa4\uff0c\u5e76\u5c06\u6bcf\u4e00\u4f4d\u7684\u8ba1\u7b97\u7ed3\u679c\u76f8\u52a0\u5f97\u5230\u4e00\u4e2a\u6570s<\/li>\n<li>\u5bf9\u6bcf\u4e00\u4f4d\u7684\u8ba1\u7b97\u89c4\u5219\uff1a\u5982\u679c\u8fd9\u4e00\u4f4d\u662f\u5947\u6570\u4f4d\uff0c\u5219\u8fd4\u56det\u672c\u8eab\uff0c\u5982\u679c\u662f\u5076\u6570\u4f4d\uff0c\u5219\u5148\u5c06t\u4e58\u4ee52\u5f97\u5230\u4e00\u4e2a\u6570n\uff0c\u5982\u679cn\u662f\u4e00\u4f4d\u6570\uff08\u5c0f\u4e8e10\uff09\uff0c\u76f4\u63a5\u8fd4\u56den\uff0c\u5426\u5219\u5c06n\u7684\u4e2a\u4f4d\u6570\u548c\u5341\u4f4d\u6570\u76f8\u52a0\u8fd4\u56de<\/li>\n<li>\u5982\u679cs\u80fd\u591f\u6574\u966410\uff0c\u5219\u6b64\u53f7\u7801\u6709\u6548\uff0c\u5426\u5219\u53f7\u7801\u65e0\u6548<\/li>\n<\/ol>\n<p>\u56e0\u4e3a\u6700\u7ec8\u7684\u7ed3\u679c\u4f1a\u5bf910\u53d6\u4f59\u6765\u5224\u65ad\u662f\u5426\u80fd\u591f\u6574\u966410\uff0c\u6240\u4ee5\u53c8\u53eb\u505a\u6a2110\u7b97\u6cd5\u3002<\/p>\n<p>\u6821\u9a8c\u7b97\u6cd5\u6bd4\u8f83\u7b80\u5355\uff0c\u4e00\u4e2apython\u7684\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\">#! \/usr\/bin\/python3\n# -*- coding: utf-8 -*-\n\ndef luhn(card_num):\n    s = 0\n    card_num_length = len(card_num)\n    for _ in range(1, card_num_length + 1):\n        t = int(card_num[card_num_length - _])\n        if _ % 2 == 0:\n            t *= 2\n            s += t if t &lt; 10 else t % 10 + t \/\/ 10\n        else:\n            s += t\n    return s % 10 == 0\n\nif __name__ == &#039;__main__&#039;:\n    print(luhn(&#039;6226095711989751&#039;))<\/code><\/pre>\n<h2>\u751f\u6210\u7b26\u5408Luhn\u89c4\u5219\u7684\u94f6\u884c\u5361\u53f7\u6d4b\u8bd5\u6570\u636e<\/h2>\n<p>\u524d\u9762\u65e2\u7136\u6478\u6e05\u4e86\u94f6\u884c\u5361\u53f7\u7684\u6821\u9a8c\u89c4\u5219\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u6839\u636e\u6b64\u89c4\u5219\u751f\u6210\u4e00\u4e9b\u80fd\u591f\u901a\u8fc7Luhn\u6821\u9a8c\u7684\u6d4b\u8bd5\u6570\u636e\u3002<\/p>\n<p>\u601d\u8def\uff1a\u56e0\u4e3a\u6700\u53f3\u8fb9\u7684\u4e00\u4f4d\u662f\u5947\u6570\u4f4d\uff0c\u5947\u6570\u4f4d\u4e0d\u9700\u8981\u6539\u53d8\u503c\u76f4\u63a5\u653e\u5565\u5c31\u662f\u5565\uff0c\u8fd9\u4e2a\u7279\u6027\u5f88\u91cd\u8981\uff0c\u6b63\u597d\u53ef\u4ee5\u7528\u6765\u8865\u9f50\u5230\u6b63\u597d\u80fd\u591f\u6574\u966410\u3002<\/p>\n<p>\u6240\u4ee5\u663e\u7136\u80fd\u591f\u63a8\u6d4b\u51fa\u751f\u6210n\u4f4d\u7b26\u5408Luhn\u89c4\u5219\u7684\u7b97\u6cd5\uff1a<\/p>\n<ol>\n<li>\u968f\u673a\u751f\u6210n-1\u4f4d\u5b57\u7b26\uff0c\u79f0\u4e3a\u5b57\u7b26\u4e32x<\/li>\n<li>\u5148\u5047\u8bbe\u5b57\u7b26\u4e32x\u6709n\u4f4d\uff08\u5b9e\u9645\u4e0a\u6700\u53f3\u8fb9\u4e00\u4f4d\u7f3a\u5931\u662fn-1\u4f4d\uff09\uff0c\u5c06x\u6309\u7167n\u4f4d\u957f\u5ea6\u8ba1\u7b97\u548cs\uff0c\u56e0\u4e3a\u6700\u53f3\u8fb9\u7b2c\u4e00\u4f4d\u662f\u7f3a\u5931\u7684\uff0c\u5ffd\u7565\u8df3\u8fc7\uff0c\u6240\u4ee5\u8ba1\u7b97\u65f6\u6700\u53f3\u8fb9\u4e00\u4f4d\u4ece2\u5f00\u59cb<\/li>\n<li>\u4e0a\u4e00\u6b65\u5f97\u5230\u5b57\u7b26\u4e32x\u7684\u6821\u9a8c\u548cs\uff0c\u5c06s\u52a0\u4e0a\u4e00\u4e2a\u6570\u5b57y\uff0c\u4f7f\u5f97\u5b83\u6b63\u597d\u53ef\u4ee5\u6574\u966410\uff0c\u8fd9\u4e2ay\u5c31\u662f\u6700\u53f3\u8fb9\u7b2c\u4e00\u4f4d\u5e94\u8be5\u653e\u7684\u6570\u5b57<\/li>\n<li>x+y\u505a\u5b57\u7b26\u4e32\u62fc\u63a5\u8fd0\u7b97\uff0c\u5f97\u5230\u6700\u7ec8\u7684n\u4f4d\u7b26\u5408Luhn\u89c4\u5219\u7684\u5b57\u7b26\u4e32<\/li>\n<\/ol>\n<pre><code class=\"language-python\">#! \/usr\/bin\/python3\n# -*- coding: utf-8 -*-\n\nimport random\n\ndef gen_card_num(start_with, total_num):\n    result = start_with\n\n    # \u968f\u673a\u751f\u6210\u524dN-1\u4f4d\n    while len(result) &lt; total_num - 1:\n        result += str(random.randint(0, 9))\n\n    # \u8ba1\u7b97\u524dN-1\u4f4d\u7684\u6821\u9a8c\u548c\n    s = 0\n    card_num_length = len(result)\n    for _ in range(2, card_num_length + 2):\n        t = int(result[card_num_length - _ + 1])\n        if _ % 2 == 0:\n            t *= 2\n            s += t if t &lt; 10 else t % 10 + t \/\/ 10\n        else:\n            s += t\n\n    # \u6700\u540e\u4e00\u4f4d\u5f53\u505a\u662f\u6821\u9a8c\u4f4d\uff0c\u7528\u6765\u8865\u9f50\u5230\u80fd\u591f\u6574\u966410\n    t = 10 - s % 10\n    result += str(0 if t == 10 else t)\n    return result\n\ndef luhn(card_num):\n    s = 0\n    card_num_length = len(card_num)\n    for _ in range(1, card_num_length + 1):\n        t = int(card_num[card_num_length - _])\n        if _ % 2 == 0:\n            t *= 2\n            s += t if t &lt; 10 else t % 10 + t \/\/ 10\n        else:\n            s += t\n    return s % 10 == 0\n\nif __name__ == &#039;__main__&#039;:\n    for _ in range(1000):\n        random_card_num = gen_card_num(&#039;622609&#039;, 16)\n        valid_result = luhn(random_card_num)\n        print(&#039;%s %s&#039; % (random_card_num, valid_result))<\/code><\/pre>\n<blockquote>\n<p>\u8f6c\u8f7d\u81f3\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/www.haimengli.com\/yin-hang-ka-hao-ma-jiao-yan\/\">http:\/\/www.haimengli.com\/yin-hang-ka-hao-ma-jiao-yan\/<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u65f6\u5019\u5728\u7f51\u4e0a\u529e\u7406\u4e00\u4e9b\u4e1a\u52a1\u65f6\u6709\u4e9b\u9700\u8981\u586b\u5199\u94f6\u884c\u5361\u53f7\u7801\uff0c\u5f53\u80e1\u4e71\u586b\u5199\u65f6\u4f1a\u7acb\u5373\u62a5\u9519\uff0c\u4f46\u662f\u5e76\u6ca1\u6709\u53d1\u73b0\u5411\u540e\u7aef\u53d1\u9001\u8bf7\u6c42\uff0c\u90a3\u4e48\u8fd9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[97],"tags":[506,507],"class_list":["post-2003","post","type-post","status-publish","format-standard","hentry","category-tools-skills","tag-luhn","tag-507"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2003","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2003"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2003\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}