mirror of
https://github.com/syncthing/syncthing.git
synced 2024-11-17 19:08:46 -07:00
65aaa607ab
Change made by: - running "gvt fetch" on each of the packages mentioned in Godeps/Godeps.json - `rm -rf Godeps` - tweaking the build scripts to not mention Godeps - tweaking the build scripts to test `./lib/...`, `./cmd/...` explicitly (to avoid testing vendor) - tweaking the build scripts to not juggle GOPATH for Godeps and instead set GO15VENDOREXPERIMENT. This also results in some updated packages at the same time I bet. Building with Go 1.3 and 1.4 still *works* but won't use our vendored dependencies - the user needs to have the actual packages in their GOPATH then, which they'll get with a normal "go get". Building with Go 1.6+ will get our vendored dependencies by default even when not using our build script, which is nice. By doing this we gain some freedom in that we can pick and choose manually what to include in vendor, as it's not based on just dependency analysis of our own code. This is also a risk as we might pick up dependencies we are unaware of, as the build may work locally with those packages present in GOPATH. On the other hand the build server will detect this as it has no packages in it's GOPATH beyond what is included in the repo. Recommended tool to manage dependencies is github.com/FiloSottile/gvt.
71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
// Copyright (C) 2014 Jakob Borg
|
|
|
|
// Package luhn generates and validates Luhn mod N check digits.
|
|
package luhn
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// An alphabet is a string of N characters, representing the digits of a given
|
|
// base N.
|
|
type Alphabet string
|
|
|
|
var (
|
|
Base32 Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
|
|
)
|
|
|
|
// Generate returns a check digit for the string s, which should be composed
|
|
// of characters from the Alphabet a.
|
|
func (a Alphabet) Generate(s string) (rune, error) {
|
|
if err := a.check(); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
factor := 1
|
|
sum := 0
|
|
n := len(a)
|
|
|
|
for i := range s {
|
|
codepoint := strings.IndexByte(string(a), s[i])
|
|
if codepoint == -1 {
|
|
return 0, fmt.Errorf("Digit %q not valid in alphabet %q", s[i], a)
|
|
}
|
|
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]), nil
|
|
}
|
|
|
|
// Validate returns true if the last character of the string s is correct, for
|
|
// a string s composed of characters in the alphabet a.
|
|
func (a Alphabet) Validate(s string) bool {
|
|
t := s[:len(s)-1]
|
|
c, err := a.Generate(t)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return rune(s[len(s)-1]) == c
|
|
}
|
|
|
|
// check returns an error if the given alphabet does not consist of unique characters
|
|
func (a Alphabet) check() error {
|
|
cm := make(map[byte]bool, len(a))
|
|
for i := range a {
|
|
if cm[a[i]] {
|
|
return fmt.Errorf("Digit %q non-unique in alphabet %q", a[i], a)
|
|
}
|
|
cm[a[i]] = true
|
|
}
|
|
return nil
|
|
}
|