summaryrefslogtreecommitdiff
path: root/devdocs/go/crypto%2Frand%2Findex.html
blob: 0ffd3e5b9cb37b49d18d27b4fc94de322deb406b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<h1> Package rand  </h1>     <ul id="short-nav">
<li><code>import "crypto/rand"</code></li>
<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
<li><a href="#pkg-index" class="indexLink">Index</a></li>
<li><a href="#pkg-examples" class="examplesLink">Examples</a></li>
</ul>     <h2 id="pkg-overview">Overview </h2> <p>Package rand implements a cryptographically secure random number generator. </p>     <h2 id="pkg-index">Index </h2>  <ul id="manual-nav">
<li><a href="#pkg-variables">Variables</a></li>
<li><a href="#Int">func Int(rand io.Reader, max *big.Int) (n *big.Int, err error)</a></li>
<li><a href="#Prime">func Prime(rand io.Reader, bits int) (*big.Int, error)</a></li>
<li><a href="#Read">func Read(b []byte) (n int, err error)</a></li>
</ul> <div id="pkg-examples"> <h3>Examples</h3>  <dl> <dd><a class="exampleLink" href="#example_Read">Read</a></dd> </dl> </div> <h3>Package files</h3> <p>  <span>rand.go</span> <span>rand_getrandom.go</span> <span>rand_unix.go</span> <span>util.go</span>  </p>   <h2 id="pkg-variables">Variables</h2> <p>Reader is a global, shared instance of a cryptographically secure random number generator. </p>
<p>On Linux, FreeBSD, Dragonfly, NetBSD and Solaris, Reader uses getrandom(2) if available, /dev/urandom otherwise. On OpenBSD and macOS, Reader uses getentropy(2). On other Unix-like systems, Reader reads from /dev/urandom. On Windows systems, Reader uses the ProcessPrng API. On JS/Wasm, Reader uses the Web Crypto API. On WASIP1/Wasm, Reader uses random_get from wasi_snapshot_preview1. </p>
<pre data-language="go">var Reader io.Reader</pre> <h2 id="Int">func <span>Int</span>  </h2> <pre data-language="go">func Int(rand io.Reader, max *big.Int) (n *big.Int, err error)</pre> <p>Int returns a uniform random value in [0, max). It panics if max &lt;= 0. </p>
<h2 id="Prime">func <span>Prime</span>  </h2> <pre data-language="go">func Prime(rand io.Reader, bits int) (*big.Int, error)</pre> <p>Prime returns a number of the given bit length that is prime with high probability. Prime will return error for any error returned by <a href="#Read">rand.Read</a> or if bits &lt; 2. </p>
<h2 id="Read">func <span>Read</span>  </h2> <pre data-language="go">func Read(b []byte) (n int, err error)</pre> <p>Read is a helper function that calls Reader.Read using io.ReadFull. On return, n == len(b) if and only if err == nil. </p>   <h4 id="example_Read"> <span class="text">Example</span>
</h4> <p>This example reads 10 cryptographically secure pseudorandom numbers from rand.Reader and writes them to a byte slice. </p> <p>Code:</p> <pre class="code" data-language="go">c := 10
b := make([]byte, c)
_, err := rand.Read(b)
if err != nil {
    fmt.Println("error:", err)
    return
}
// The slice should now contain random bytes instead of only zeroes.
fmt.Println(bytes.Equal(b, make([]byte, c)))

</pre> <p>Output:</p> <pre class="output" data-language="go">false
</pre><div class="_attribution">
  <p class="_attribution-p">
    &copy; Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
    <a href="http://golang.org/pkg/crypto/rand/" class="_attribution-link">http://golang.org/pkg/crypto/rand/</a>
  </p>
</div>