MCPcopy Create free account
hub / github.com/bytebase/bytebase / RandomString

Function RandomString

backend/common/util.go:43–59  ·  view source on GitHub ↗

RandomString returns a random string with length n.

(n int)

Source from the content-addressed store, hash-verified

41
42// RandomString returns a random string with length n.
43func RandomString(n int) (string, error) {
44 var sb strings.Builder
45 sb.Grow(n)
46 for i := 0; i < n; i++ {
47 // The reason for using crypto/rand instead of math/rand is that
48 // the former relies on hardware to generate random numbers and
49 // thus has a stronger source of random numbers.
50 randNum, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
51 if err != nil {
52 return "", err
53 }
54 if _, err := sb.WriteRune(letters[randNum.Uint64()]); err != nil {
55 return "", err
56 }
57 }
58 return sb.String(), nil
59}
60
61// HasPrefixes returns true if the string s has any of the given prefixes.
62func HasPrefixes(src string, prefixes ...string) bool {

Callers 7

CreateUserMethod · 0.92
generateRecoveryCodesFunction · 0.92
CreateServiceAccountMethod · 0.92
UpdateServiceAccountMethod · 0.92

Calls 2

StringMethod · 0.65
IntMethod · 0.45

Tested by

no test coverage detected