mirror of
https://github.com/usememos/memos.git
synced 2024-11-14 10:44:50 +08:00
d0b8b076cf
* feat: implement sign in with SSO * chore: update * chore: update * chore: update
60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package common
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"math/big"
|
|
"net/mail"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// HasPrefixes returns true if the string s has any of the given prefixes.
|
|
func HasPrefixes(src string, prefixes ...string) bool {
|
|
for _, prefix := range prefixes {
|
|
if strings.HasPrefix(src, prefix) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// ValidateEmail validates the email.
|
|
func ValidateEmail(email string) bool {
|
|
if _, err := mail.ParseAddress(email); err != nil {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func GenUUID() string {
|
|
return uuid.New().String()
|
|
}
|
|
|
|
func Min(x, y int) int {
|
|
if x < y {
|
|
return x
|
|
}
|
|
return y
|
|
}
|
|
|
|
var letters = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
|
|
|
// RandomString returns a random string with length n.
|
|
func RandomString(n int) (string, error) {
|
|
var sb strings.Builder
|
|
sb.Grow(n)
|
|
for i := 0; i < n; i++ {
|
|
// The reason for using crypto/rand instead of math/rand is that
|
|
// the former relies on hardware to generate random numbers and
|
|
// thus has a stronger source of random numbers.
|
|
randNum, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if _, err := sb.WriteRune(letters[randNum.Uint64()]); err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
return sb.String(), nil
|
|
}
|