github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/crypto/randentropy/rand_entropy.go (about)

     1  package randentropy
     2  
     3  import (
     4  	crand "crypto/rand"
     5  	"io"
     6  )
     7  
     8  var Reader io.Reader = &randEntropy{}
     9  
    10  type randEntropy struct {
    11  }
    12  
    13  func (*randEntropy) Read(bytes []byte) (n int, err error) {
    14  	readBytes := GetEntropyCSPRNG(len(bytes))
    15  	copy(bytes, readBytes)
    16  	return len(bytes), nil
    17  }
    18  
    19  func GetEntropyCSPRNG(n int) []byte {
    20  	mainBuff := make([]byte, n)
    21  	_, err := io.ReadFull(crand.Reader, mainBuff)
    22  	if err != nil {
    23  		panic("reading from crypto/rand failed: " + err.Error())
    24  	}
    25  	return mainBuff
    26  }