github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekarand/rand_reader.go (about)

     1  // Copyright © 2021. All rights reserved.
     2  // Author: Ilya Stroy.
     3  // Contacts: iyuryevich@pm.me, https://github.com/qioalice
     4  // License: https://opensource.org/licenses/MIT
     5  
     6  package ekarand
     7  
     8  import (
     9  	crand "crypto/rand"
    10  	mrand "math/rand"
    11  )
    12  
    13  type (
    14  	// MathRandReader is an io.Reader interface that provides thread safe Read()
    15  	// method using math/rand random number generator.
    16  	// According with math/rand rules, if you don't instantiate a Rng generator
    17  	// but use package level's function they will be thread safety.
    18  	// You can use both of (*MathRandReader)(nil) or MathRandReader{} variants.
    19  	MathRandReader struct{}
    20  
    21  	// CryptoRandReader is an io.Reader interface that provides thread safe Read()
    22  	// method with increased rng complexity comparing with math/rand
    23  	// sacrificing performance.
    24  	// You can use both of (*CryptoRandReader)(nil) or CryptoRandReader{} variants.
    25  	CryptoRandReader struct{}
    26  )
    27  
    28  func (_ *MathRandReader) Read(p []byte) (n int, err error) {
    29  	return mrand.Read(p)
    30  }
    31  
    32  func (_ *CryptoRandReader) Read(p []byte) (n int, err error) {
    33  	return crand.Read(p)
    34  }