@@ -0,0 +1,33 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const codeCharset = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"
|
||||
|
||||
// GenerateCode génère un code de 16 caractères alphanumériques,
|
||||
// regroupés par 4 et séparés par des tirets (ex: "A3F9-K2M7-XQ4R-8ZT1").
|
||||
func GenerateCode() (string, error) {
|
||||
const length = 16
|
||||
const groupSize = 4
|
||||
|
||||
b := make([]byte, length)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
for i := 0; i < length; i++ {
|
||||
if i > 0 && i%groupSize == 0 {
|
||||
sb.WriteByte('-')
|
||||
}
|
||||
sb.WriteByte(codeCharset[int(b[i])%len(codeCharset)])
|
||||
}
|
||||
return sb.String(), nil
|
||||
}
|
||||
|
||||
func NormalizeUsername(u string) string {
|
||||
return strings.ToLower(strings.TrimSpace(u))
|
||||
}
|
||||
Reference in New Issue
Block a user