<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Generate a pseudo-random string of bytes</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.openssl-public-encrypt.html">openssl_public_encrypt</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.openssl-seal.html">openssl_seal</a></div> <div class="up"><a href="ref.openssl.html">OpenSSL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.openssl-random-pseudo-bytes" class="refentry"> <div class="refnamediv"> <h1 class="refname">openssl_random_pseudo_bytes</h1> <p class="verinfo">(PHP 5 >= 5.3.0)</p><p class="refpurpose"><span class="refname">openssl_random_pseudo_bytes</span> — <span class="dc-title">Generate a pseudo-random string of bytes</span></p> </div> <div class="refsect1 description" id="refsect1-function.openssl-random-pseudo-bytes-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>openssl_random_pseudo_bytes</strong></span> ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter reference">&$crypto_strong</code></span> ] )</div> <p class="para rdfs-comment"> Generates a <span class="type"><a href="language.types.string.html" class="type string">string</a></span> of pseudo-random bytes, with the number of bytes determined by the <em><code class="parameter">length</code></em> parameter. </p> <p class="para"> It also indicates if a cryptographically strong algorithm was used to produce the pseudo-random bytes, and does this via the optional <em><code class="parameter">crypto_strong</code></em> parameter. It's rare for this to be <strong><code>FALSE</code></strong>, but some systems may be broken or old. </p> </div> <div class="refsect1 parameters" id="refsect1-function.openssl-random-pseudo-bytes-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">length</code></em></span> <dd> <p class="para"> The length of the desired string of bytes. Must be a positive integer. PHP will try to cast this parameter to a non-null integer to use it. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">crypto_strong</code></em></span> <dd> <p class="para"> If passed into the function, this will hold a <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> value that determines if the algorithm used was "cryptographically strong", e.g., safe for usage with GPG, passwords, etc. <strong><code>TRUE</code></strong> if it did, otherwise <strong><code>FALSE</code></strong> </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.openssl-random-pseudo-bytes-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the generated <span class="type"><a href="language.types.string.html" class="type string">string</a></span> of bytes on success, or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 examples" id="refsect1-function.openssl-random-pseudo-bytes-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-868"> <p><strong>Example #1 <span class="function"><strong>openssl_random_pseudo_bytes()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= -</span><span style="color: #0000BB">1</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700"><= </span><span style="color: #0000BB">4</span><span style="color: #007700">; </span><span style="color: #0000BB">$i</span><span style="color: #007700">++) {<br /> </span><span style="color: #0000BB">$bytes </span><span style="color: #007700">= </span><span style="color: #0000BB">openssl_random_pseudo_bytes</span><span style="color: #007700">(</span><span style="color: #0000BB">$i</span><span style="color: #007700">, </span><span style="color: #0000BB">$cstrong</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$hex </span><span style="color: #007700">= </span><span style="color: #0000BB">bin2hex</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">);<br /><br /> echo </span><span style="color: #DD0000">"Lengths: Bytes: </span><span style="color: #0000BB">$i</span><span style="color: #DD0000"> and Hex: " </span><span style="color: #007700">. </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$hex</span><span style="color: #007700">) . </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$hex</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$cstrong</span><span style="color: #007700">);<br /> echo </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Lengths: Bytes: -1 and Hex: 0 string(0) "" NULL Lengths: Bytes: 0 and Hex: 0 string(0) "" NULL Lengths: Bytes: 1 and Hex: 2 string(2) "42" bool(true) Lengths: Bytes: 2 and Hex: 4 string(4) "dc6e" bool(true) Lengths: Bytes: 3 and Hex: 6 string(6) "288591" bool(true) Lengths: Bytes: 4 and Hex: 8 string(8) "ab86d144" bool(true) </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.openssl-random-pseudo-bytes-seealso"> <h3 class="title">See Also</h3> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.bin2hex.html" class="function" rel="rdfs-seeAlso">bin2hex()</a> - Convert binary data into hexadecimal representation</span></li> <li class="member"> <span class="function"><a href="function.crypt.html" class="function" rel="rdfs-seeAlso">crypt()</a> - One-way string hashing</span></li> <li class="member"> <span class="function"><a href="function.mt-rand.html" class="function" rel="rdfs-seeAlso">mt_rand()</a> - Generate a better random value</span></li> <li class="member"> <span class="function"><a href="function.uniqid.html" class="function" rel="rdfs-seeAlso">uniqid()</a> - Generate a unique ID</span></li> </ul> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.openssl-public-encrypt.html">openssl_public_encrypt</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.openssl-seal.html">openssl_seal</a></div> <div class="up"><a href="ref.openssl.html">OpenSSL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>