github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/validators/luhn-algorithm.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package validators
     4  
     5  // LuhnAlgorithm validate strings with the luhn (mod-10) algorithm!
     6  // https://en.wikipedia.org/wiki/Luhn_algorithm
     7  func LuhnAlgorithm(pan string) bool {
     8  	/* Validate string with Luhn (mod-10) */
     9  	var alter bool
    10  	var checksum int
    11  
    12  	for position := len(pan) - 1; position > -1; position-- {
    13  		digit := int(pan[position] - 48)
    14  		if alter {
    15  			digit = digit * 2
    16  			if digit > 9 {
    17  				digit = (digit % 10) + 1
    18  			}
    19  		}
    20  		alter = !alter
    21  		checksum += digit
    22  	}
    23  	return checksum%10 == 0
    24  }