github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/crypto/randentropy/rand_entropy.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package randentropy
    13  
    14  import (
    15  	crand "crypto/rand"
    16  	"io"
    17  )
    18  
    19  var Reader io.Reader = &randEntropy{}
    20  
    21  type randEntropy struct {
    22  }
    23  
    24  func (*randEntropy) Read(bytes []byte) (n int, err error) {
    25  	readBytes := GetEntropyCSPRNG(len(bytes))
    26  	copy(bytes, readBytes)
    27  	return len(bytes), nil
    28  }
    29  
    30  func GetEntropyCSPRNG(n int) []byte {
    31  	mainBuff := make([]byte, n)
    32  	_, err := io.ReadFull(crand.Reader, mainBuff)
    33  	if err != nil {
    34  		panic("reading from crypto/rand failed: " + err.Error())
    35  	}
    36  	return mainBuff
    37  }