Files

53 lines
1.1 KiB
Go

package idutil
import (
"crypto/rand"
"encoding/hex"
"strings"
"unicode/utf8"
)
// New 生成 32 位消息号,符合 1–64 位字母数字及 _ . -。
func New() string {
var b [16]byte
_, _ = rand.Read(b[:])
return hex.EncodeToString(b[:])
}
// ValidMessage 检查消息号。
func ValidMessage(s string) bool {
if len(s) < 1 || len(s) > 64 {
return false
}
for i := 0; i < len(s); i++ {
c := s[i]
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '.' || c == '-' {
continue
}
return false
}
return true
}
// ValidEndpoint 检查端编号:小写字母、数字、_ . -。
func ValidEndpoint(s string) bool {
if len(s) < 1 || len(s) > 64 {
return false
}
for i := 0; i < len(s); i++ {
c := s[i]
if (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == '.' || c == '-' {
continue
}
return false
}
return true
}
// ValidName 检查给人看的名称。
func ValidName(s string) bool {
s = strings.TrimSpace(s)
n := utf8.RuneCountInString(s)
return n >= 1 && n <= 40
}