Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is Golang crypto/rand thread safe?

The source of math/rand.Rand states Read is not thread safe (when sharing a source). What about crypto/rand? The source states it uses getrandom(2) or /dev/urandom, but it is unclear what happens with concurrent calls.

Update: comments have helped clarify the difference between rand.Read() and rand.Rand.Read()

To be a thread safe:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. Will it panic when Read is called concurrently?
  2. Will it keep the random sequence when called concurrently? Or can duplicates be given to concurrent callers?

>Solution :

  1. rand.Reader from crypto/rand must be safe for concurrent access, because it is defined as "a global, shared instance of a cryptographically secure random number generator". There would be no way to synchronized its use between packages.
  2. rand.Read from crypto/rand is safe because the rand.Reader is safe, and there is no other shared state.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading