当前位置:网站首页>Go realizes Luhn verification of bank card
Go realizes Luhn verification of bank card
2022-04-22 19:46:00 【dz45693】
One 、 Verification rules of bank card number
The verification of bank card number adopts Luhn Algorithm , The verification process is roughly as follows :
1. Number the card number string from right to left , The first on the far right is 1, The second on the far right is 2, The third on the far right is 3….
2. Traverse from right to left , For each character t Perform the third step , And add the calculation results of each bit to get a number s.
3. Calculation rules for each bit : If this one is odd , Then return to t In itself , If it's an even number , The first will t multiply 2 Get a number n, If n It's one digit. ( Less than 10), Go straight back to n, Otherwise it would be n Add the single digit and ten digit of to return .
4. If s Be able to divide 10, Then this number is valid , Otherwise... The number is invalid .
Because the final result will be right 10 Take the remainder to determine whether it can be divided 10, So it is also called module 10 Algorithm .
Two 、 Generating coincidence Luhn Regular bank card number test data
Now that I have found out the verification rules of bank card number , Then you can generate some rules that can pass Luhn Verified test data .
Ideas :
Because the rightmost digit is odd , Odd digits don't need to change the value, just put what you want , This feature is important , It can be used to make up just enough to divide 10.
So obviously it can be inferred that he was born into n Bit coincidence Luhn Regular algorithm :
1. Random generation n-1 Bit character , Called string x.
2. Let's assume that the string x Yes n position ( In fact, the missing one on the far right is n-1 position , The last one uses 0 Fill in the space ), take x according to n Bit length calculation and s,
3. In the previous step, we get the string x Checksum s, take s Add a number y, So that it can just divide 10, This y It's the number that should be placed first on the far right .
4. x+y Do string splicing , To get the final n Bit coincidence Luhn The string of the rule .
The whole code is as follows :
package main
import (
"fmt"
"math/rand"
"strconv"
"time"
)
func main() {
fmt.Println(checkCarNum("6226095711989751"))
cardNum := genCardNum("622609", 16)
fmt.Println(cardNum)
fmt.Println(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 := strconv.ParseInt(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)
rand.New(rand.NewSource(time.Now().UnixNano()))
for {
result += fmt.Sprintf("%d", rand.Intn(10))
if length == totalNum-1 {
break
}
length++
}
sum, _ := getCardNumSum(result + "0")
t := 10 - sum%10
if t == 10 {
t = 0
}
result += fmt.Sprintf("%d", t)
return result
}
Reference resources : Bank card number verification algorithm (Luhn Algorithm , It's also called model 10 Algorithm ) - CC11001100 - Blog Garden
版权声明
本文为[dz45693]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221942539917.html
边栏推荐
猜你喜欢

【AI视野·今日Robot 机器人论文速览 第三十三期】Thu, 21 Apr 2022
![[AI vision · quick review of robot papers today, issue 33] Thu, 21 APR 2022](/img/d9/075af1453cb0aded4945074d0643c2.png)
[AI vision · quick review of robot papers today, issue 33] Thu, 21 APR 2022

C#开发OPC客户端

互联网快讯:联想公布ESG新进展;极米H3S、极米Z6X Pro出色音画获好评;小红书回应“裁员20%”

quarkus依赖注入之九:bean读写锁

图像预训练模型的起源解说和使用示例

调用mysql存储过程报错:mysql 1449 : The user specified as a definer ('root'@'%') does not exist

RoyalScope快速定位CAN网络中的故障节点,排产CAN总线质量

2022年中国数字科技专题分析

Excel consolidation calculation of multiple similar tables (consolidation calculation tool)
随机推荐
【自动化测试基础知识】自动化测试的基本概念及常用框架
软件测试入行必看,一文800字教你Allure 测试报告环境搭建
关于log4j2的重新加载,以及不同级别日志输出到不同日志文件
The concept, function, problem and solution of closure
2018-8-10-win10-uwp-business game-1.2.1
The origin explanation and use example of image pre training model
Golang本地缓存选型对比及原理总结
When MySQL designs a table, two timestamp fields are required
From functional test to automatic test, the salary doubled. I sorted out this 3000 word super complete learning guide
.net socket.io客户端使用过程
[Niuke brush question 19] MP3 cursor position
Is it true to download the financial app of qiniu business school? Is it safe to open an account in qiniu
How can I apply for convertible bonds? Is it safe to purchase convertible bonds?
C develop OPC client
【2022应届生看过来】一个无经验的大学毕业生,可以转行做软件测试吗?
2路CAN/CAN FD 数据记录诊断仪为企业解决偶发性错误难点
这就是深度学习如此强大的原因
让模型训练速度提升2到4倍,「彩票假设」作者的这个全新PyTorch库火了
12-Delta Lake
ASP. Net webapi and WebService return JSON data