Files
goodlink/utils/tools.go
2025-01-23 09:49:14 +08:00

21 lines
413 B
Go

package utils
import (
"crypto/rand"
"math/big"
)
func RandomBytes(length int) []byte {
bytes := make([]byte, length)
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for i := 0; i < length; {
bint, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
if err != nil {
return nil
}
bytes[i] = charset[bint.Int64()]
i++
}
return bytes
}