SoFunction
Updated on 2025-03-04

Go language to realize bank card number Luhn verification

1. Verification rules for bank card numbers

The Luhn algorithm is used to verify the bank card number, and the verification process is roughly as follows:

1. Number the card number string from right to left. The first digit on the right is 1, the second digit on the right is 2, and the third digit on the right is 3....

2. Traverse from right to left, perform the third step on each bit character t, and add the calculation results of each bit to obtain a number s.

3. Calculation rules for each bit: If this bit is an odd digit, then return t itself. If it is an even digit, first multiply t by 2 to get a number n. If n is a single digit (less than 10), return n directly, otherwise add the single digit and ten digits of n to return.

4. If s can be divisible by 10, this number is valid, otherwise the number is invalid.

Because the final result will take more than 10 to determine whether 10 can be divided, it is also called the modular 10 algorithm.

2. Generate bank card number test data that complies with Luhn rules

Since you have figured out the verification rules for bank card numbers, you can generate some test data that can pass Luhn verification based on this rule.

Ideas:

Because the rightmost digit is an odd digit, and the odd digit does not need to change the value and put whatever it is. This feature is very important and can be used to make up for exactly 10.

So it is obviously possible to infer that the algorithm that generates n-bits conforms to the Luhn rule:

1. Randomly generate n-1 characters, called string x.

2. First assume that the string x has n bits (in fact, the missing bit on the rightmost bit is n-1 bit, and the last bit is filled with 0), calculate x according to the length of n bits,

3. In the previous step, get the checksum s of the string x, add s to a number y, so that it can be divisible 10, and this y is the number that should be placed on the first rightmost position.

4. x+y does string splicing operation to obtain the final n-bit string that complies with Luhn's rules.

The entire code is as follows:

package main
import (
	"fmt"
	"math/rand"
	"strconv"
	"time"
)
func main() {
	(checkCarNum("6226095711989751"))
	cardNum := genCardNum("622609", 16)
	(cardNum)
	(checkCarNum(cardNum))
}
func checkCarNum(cardNum string) bool {
	sum, err := getCardNumSum(cardNum)
	if err != nil {
		return false
	}
	return sum%10 == 0
}
func getCardNumSum(cardNum string) (int64, error) {
	sum := int64(0)
	length := len(cardNum)
	index := length - 1
	for {
		t, err := (string(cardNum[index]), 10, 64)
		if err != nil {
			return 0, err
		}
		if index%2 == 0 {
			t = t * 2
			if t >= 10 {
				t = t%10 + t/10
			}
		}
		sum += t
		if index <= 0 {
			break
		}
		index--
	}
	return sum, nil
}
func genCardNum(startWith string, totalNum int) string {
	result := startWith
	length := len(result)
	((().UnixNano()))
	for {
		result += ("%d", (10))
		if length == totalNum-1 {
			break
		}
		length++
	}
	sum, _ := getCardNumSum(result + "0")
	t := 10 - sum%10
	if t == 10 {
		t = 0
	}
	result += ("%d", t)
	return result
}

refer to:Bank card number verification algorithm (Luhn algorithm, also known as the modulo 10 algorithm) 

The above is the detailed content of the Go language to implement Luhn verification for bank card. For more information about Luhn verification for go bank card, please pay attention to my other related articles!