You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
689 B

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