summaryrefslogtreecommitdiff
path: root/devdocs/go/crypto%2Frand%2Findex.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/go/crypto%2Frand%2Findex.html
new repository
Diffstat (limited to 'devdocs/go/crypto%2Frand%2Findex.html')
-rw-r--r--devdocs/go/crypto%2Frand%2Findex.html32
1 files changed, 32 insertions, 0 deletions
diff --git a/devdocs/go/crypto%2Frand%2Findex.html b/devdocs/go/crypto%2Frand%2Findex.html
new file mode 100644
index 00000000..0ffd3e5b
--- /dev/null
+++ b/devdocs/go/crypto%2Frand%2Findex.html
@@ -0,0 +1,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>