mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 03:07:45 +08:00
29 lines
680 B
Go
29 lines
680 B
Go
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// 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
|
|
|
|
// checkLuHn checks `value` with LUHN algorithm.
|
|
// It's usually used for bank card number validation.
|
|
func (v *Validator) checkLuHn(value string) bool {
|
|
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
|
|
}
|