package entropy import ( "crypto/rand" "sync" ) type OSEntropySource struct { mu sync.Mutex } func (e *OSEntropySource) GetEntropy(minEntropy int64, minEntropyInputLength int64, maxEntropyInputLength int64) ([]byte, error) { //使用互斥锁保证熵源的独占性 e.mu.Lock() defer e.mu.Unlock() n := minEntropy if n < minEntropyInputLength { n = minEntropyInputLength } entropy := make([]byte, n) if _, err := rand.Reader.Read(entropy); err != nil { return nil, err } return entropy, nil }