github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/random/random.go (about)

     1  package random
     2  
     3  import (
     4  	"crypto/rand"
     5  	"fmt"
     6  )
     7  
     8  // New returns a byte slice of the specified length with cryptographically
     9  // random conents.
    10  func New(length int) ([]byte, error) {
    11  	// Create the buffer.
    12  	result := make([]byte, length)
    13  
    14  	// Read random data.
    15  	if _, err := rand.Read(result[:]); err != nil {
    16  		return nil, fmt.Errorf("unable to read random data: %w", err)
    17  	}
    18  
    19  	// Success.
    20  	return result, nil
    21  }