patchman/common/randomString.go

22 lines
534 B
Go

package common
import (
"crypto/rand"
"math/big"
)
// RandomString generates a string with the given length using crypt/rand
func RandomString(length int) (string, error) {
const characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_="
byteSlice := make([]byte, length)
for i := 0; i < length; i++ {
randomInt, err := rand.Int(rand.Reader, big.NewInt(int64(len(characters))))
if err != nil {
return "", err
}
byteSlice[i] = characters[randomInt.Int64()]
}
return string(byteSlice), nil
}