v8.run/go/exp@v0.0.26-0.20230226010534-afcdbd3f782d/util/randutil/randstr.go (about)

     1  package randutil
     2  
     3  import (
     4  	"crypto/rand"
     5  	"encoding/base64"
     6  
     7  	"v8.run/go/exp/fastrand"
     8  )
     9  
    10  func RandString(size int) string {
    11  	b := make([]byte, size)
    12  	_, err := rand.Read(b)
    13  	if err != nil {
    14  		// Fallback to a FastRandReader.
    15  		rng := fastrand.AcquireRNG()
    16  		fr := fastrand.FastRandReader{RNG: rng}
    17  		fr.Read(b)
    18  		rng.Release()
    19  	}
    20  	return base64.RawURLEncoding.EncodeToString(b)
    21  }