2021-01-12 10:46:39 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-05-10 10:56:11 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gvalid
|
|
|
|
|
2021-03-23 17:53:20 +08:00
|
|
|
// checkLuHn checks `value` with LUHN algorithm.
|
2020-05-10 10:56:11 +08:00
|
|
|
// It's usually used for bank card number validation.
|
2021-01-12 10:46:39 +08:00
|
|
|
func (v *Validator) checkLuHn(value string) bool {
|
2020-05-10 10:56:11 +08:00
|
|
|
var (
|
|
|
|
sum = 0
|
|
|
|
nDigits = len(value)
|
|
|
|
parity = nDigits % 2
|
|
|
|
)
|
|
|
|
for i := 0; i < nDigits; i++ {
|
|
|
|
var digit = int(value[i] - 48)
|
|
|
|
if i%2 == parity {
|
|
|
|
digit *= 2
|
|
|
|
if digit > 9 {
|
|
|
|
digit -= 9
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sum += digit
|
|
|
|
}
|
|
|
|
return sum%10 == 0
|
|
|
|
}
|