kls/links/alphabet.go

34 lines
575 B
Go
Raw Normal View History

2022-03-13 04:46:04 +00:00
package links
import (
"crypto/rand"
2022-03-20 01:49:41 +00:00
"fmt"
2022-03-13 04:46:04 +00:00
"math/big"
2022-03-20 01:49:41 +00:00
"regexp"
2022-03-13 04:46:04 +00:00
)
const codeLength = 5
var (
alphabet = "ABCDEFGHJKMNPQRSTUVWXYZ23456789"
alphabetLength = big.NewInt(int64(len(alphabet)))
2022-03-20 01:49:41 +00:00
ValidShortCode = regexp.MustCompile(fmt.Sprintf(`^/[%s]{%d}$`, alphabet, codeLength))
2022-03-13 04:46:04 +00:00
)
func randInt() int {
p, err := rand.Int(rand.Reader, alphabetLength)
if err != nil {
panic(err.Error())
}
return int(p.Int64())
}
func GenCode() string {
var code []byte
for i := 0; i < codeLength; i++ {
code = append(code, alphabet[randInt()])
}
return string(code)
}