github.com/searKing/golang/go@v1.2.117/crypto/rand/bytes_crypto.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package rand
     6  
     7  import (
     8  	"crypto/rand"
     9  )
    10  
    11  // BytesCrypto returns securely generated random bytes.
    12  // It will return an error if the system's secure random
    13  // number generator fails to function correctly, in which
    14  // case the caller should not continue.
    15  func BytesCrypto(n int) ([]byte, error) {
    16  	b := make([]byte, n)
    17  	_, err := rand.Read(b)
    18  	// Note that err == nil only if we read len(b) bytes.
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  
    23  	return b, nil
    24  }