In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you the relevant knowledge points about how to use the python bank card number verification Luhn algorithm. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
I. Calibration rules of bank card numbers
The bank card number is verified by Luhn algorithm, and the verification process is roughly as follows:
1. Give the card number string number from right to left, the first bit on the right is 1, the second bit on the right is 2, and the third bit on the right is 3. .
two。 Iterate from right to left, perform the third step for each character t, and add up the results of each bit to get a number s.
3. The rule of calculation for each bit: if this bit is odd, t itself is returned, if it is even, t is multiplied by 2 to get a number n, if n is one digit (less than 10), directly return n, otherwise the single digit and ten digits of n are returned.
4. If s is divisible by 10, then this number is valid, otherwise the number is invalid.
Because the final result will take the remainder of 10 to determine whether 10 can be divisible or not, it is also called modular 10 algorithm.
The verification algorithm is relatively simple, an implementation of python:
#! / usr/bin/python3#-*-coding: utf-8-*-def luhn (card_num): s = 0 card_num_length = len (card_num) for _ in range (1 Card_num_length + 1): t = int (card_ Num [card _ num_length -]) if _% 2 = = 0: t * = 2s + = t if t < 10 else t% 10 + t / / 10 else: s + = t return s% 10 = = 0if _ name__ = ='_ main__': print (luhn ('6226095711989751')) Generate bank card number test data in accordance with Luhn rules
Now that we have found out the verification rules of the bank card number, we can generate some test data that can pass the Luhn verification according to this rule.
Train of thought:
Because the rightmost bit is the odd bit, the odd bit is whatever you put without changing the value. This feature is very important and can be used to fill it up so that it can be divided by 10.
So it is obvious that it is possible to speculate an algorithm that generates n bits that conform to the Luhn rules:
1. Randomly generate a 1-bit character called the string x.
two。 First assume that the string x has n bits (in fact, the rightmost bit missing is nMur1 bit), calculate x and s according to the n-bit length, because the first bit on the rightmost side is missing and skip, so the rightmost bit starts at 2.
3. In the previous step, you get the checksum s of the string x, and add s to the number y so that it is exactly divisible by 10, which is the first digit on the far right.
4. XSecrety does string concatenation operation to get the final n-bit string that conforms to the Luhn rules.
Implementation code: #! / usr/bin/python3#-*-coding: utf-8-*-import randomdef gen_card_num (start_with, total_num): result = start_with # 1 bit while len (result) < total_num-1: result + = str (random.randint (0) before random generation 9)) # checksum of NMI 1 bit before calculation s = 0 card_num_length = len (result) for _ in range (2 Card_num_length + 2): t = int (result [card _ num_length-_ + 1]) if _% 2 = = 0: t * = 2s + = t if t < 10 else t% 10 + t / / 10 else: s + = t # the last bit is regarded as a check bit Used to complete so that 10 t = 10-s% 10 result + = str (0 if t = = 10 else t) return resultdef luhn (card_num): s = 0 card_num_length = len (card_num) for _ in range (1 Card_num_length + 1): t = int (card_ Num [card _ num_length-_]) if _% 2 = = 0: t * = 2s + = t if t < 10 else t% 10 + t / / 10 else: s + = t return s% 10 = = 0if _ name__ = ='_ main__': for _ in range (1000): Random_card_num = gen_card_num ('622609' 16) valid_result = luhn (random_card_num) print ('% s% s'% (random_card_num, valid_result))
These are all the contents of this article entitled "how to use the Luhn algorithm for python Bank Card number Verification". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.