mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-13 00:36:28 +00:00
8f3effed32
New node ID:s contain four Luhn check digits and are grouped differently. Code uses NodeID type instead of string, so it's formatted homogenously everywhere.
38 lines
700 B
Go
38 lines
700 B
Go
package luhn
|
|
|
|
import "strings"
|
|
|
|
type Alphabet string
|
|
|
|
var (
|
|
Base32 Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567="
|
|
Base32Trimmed Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
|
|
)
|
|
|
|
func (a Alphabet) Generate(s string) rune {
|
|
factor := 1
|
|
sum := 0
|
|
n := len(a)
|
|
|
|
for i := range s {
|
|
codepoint := strings.IndexByte(string(a), s[i])
|
|
addend := factor * codepoint
|
|
if factor == 2 {
|
|
factor = 1
|
|
} else {
|
|
factor = 2
|
|
}
|
|
addend = (addend / n) + (addend % n)
|
|
sum += addend
|
|
}
|
|
remainder := sum % n
|
|
checkCodepoint := (n - remainder) % n
|
|
return rune(a[checkCodepoint])
|
|
}
|
|
|
|
func (a Alphabet) Validate(s string) bool {
|
|
t := s[:len(s)-1]
|
|
c := a.Generate(t)
|
|
return rune(s[len(s)-1]) == c
|
|
}
|