<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.sodium.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.sodium-crypto-secretstream-xchacha20poly1305-init-push.php',
    1 => 'sodium_crypto_secretstream_xchacha20poly1305_init_push',
    2 => 'Initialize a secretstream context for encryption',
  ),
  'up' => 
  array (
    0 => 'ref.sodium.php',
    1 => 'Sodium Functions',
  ),
  'prev' => 
  array (
    0 => 'function.sodium-crypto-secretstream-xchacha20poly1305-init-pull.php',
    1 => 'sodium_crypto_secretstream_xchacha20poly1305_init_pull',
  ),
  'next' => 
  array (
    0 => 'function.sodium-crypto-secretstream-xchacha20poly1305-keygen.php',
    1 => 'sodium_crypto_secretstream_xchacha20poly1305_keygen',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/sodium/functions/sodium-crypto-secretstream-xchacha20poly1305-init-push.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.sodium-crypto-secretstream-xchacha20poly1305-init-push" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sodium_crypto_secretstream_xchacha20poly1305_init_push</h1>
  <p class="verinfo">(PHP 7 &gt;= 7.2.0, PHP 8)</p><p class="refpurpose"><span class="refname">sodium_crypto_secretstream_xchacha20poly1305_init_push</span> &mdash; <span class="dc-title">Initialize a secretstream context for encryption</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.sodium-crypto-secretstream-xchacha20poly1305-init-push-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>sodium_crypto_secretstream_xchacha20poly1305_init_push</strong></span>(<span class="methodparam"><span class="attribute"><a href="class.sensitiveparameter.php">#[\SensitiveParameter]</a> </span><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$key</code></span>): <span class="type"><a href="language.types.array.php" class="type array">array</a></span></div>

  <p class="simpara">
   Initialize a secretstream context for encryption.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.sodium-crypto-secretstream-xchacha20poly1305-init-push-parameters">
  <h3 class="title">Parameters</h3>
  <dl>
   
    <dt><code class="parameter">key</code></dt>
    <dd>
     <span class="simpara">
      Cryptography key. See <span class="function"><a href="function.sodium-crypto-secretstream-xchacha20poly1305-keygen.php" class="function">sodium_crypto_secretstream_xchacha20poly1305_keygen()</a></span>.
     </span>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.sodium-crypto-secretstream-xchacha20poly1305-init-push-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="simpara">
   An array with two string values:
  </p>
  <ul class="simplelist">
   <li>The secretstream state, needed for further pushes</li>
   <li>The secretstream header, which needs to be provided to the recipient so they can pull data</li>
  </ul>
 </div>


 <div class="refsect1 examples" id="refsect1-function.sodium-crypto-secretstream-xchacha20poly1305-init-push-examples">
  <h3 class="title">Examples</h3>
  <div class="example" id="sodium-crypto-secretstream-xchacha20poly1305-init-push.example.basic">
   <p><strong>Example #1 <span class="function"><strong>sodium_crypto_secretstream_xchacha20poly1305_init_push()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">encrypt_file</span><span style="color: #007700">(</span><span style="color: #0000BB">string $inputFilePath</span><span style="color: #007700">, </span><span style="color: #0000BB">string $outputFilePath</span><span style="color: #007700">, </span><span style="color: #0000BB">string $key</span><span style="color: #007700">): </span><span style="color: #0000BB">void<br /></span><span style="color: #007700">{<br />    [</span><span style="color: #0000BB">$state</span><span style="color: #007700">, </span><span style="color: #0000BB">$header</span><span style="color: #007700">] = </span><span style="color: #0000BB">sodium_crypto_secretstream_xchacha20poly1305_init_push</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br /><br />    </span><span style="color: #0000BB">$inputFile </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$inputFilePath</span><span style="color: #007700">, </span><span style="color: #DD0000">'rb'</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">$outputFile </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$outputFilePath</span><span style="color: #007700">, </span><span style="color: #DD0000">'wb'</span><span style="color: #007700">);<br />    </span><span style="color: #FF8000">// Write the header:<br />    </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$outputFile</span><span style="color: #007700">, </span><span style="color: #0000BB">$header</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">$inputFileSize </span><span style="color: #007700">= </span><span style="color: #0000BB">fstat</span><span style="color: #007700">(</span><span style="color: #0000BB">$inputFile</span><span style="color: #007700">)[</span><span style="color: #DD0000">'size'</span><span style="color: #007700">];<br /><br />    </span><span style="color: #FF8000">// Encrypt the file and write its contents to the output file:<br />    </span><span style="color: #007700">for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">&lt; </span><span style="color: #0000BB">$inputFileSize</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">+= </span><span style="color: #0000BB">8175</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">$ptxt_chunk </span><span style="color: #007700">= </span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$inputFile</span><span style="color: #007700">, </span><span style="color: #0000BB">8175</span><span style="color: #007700">);<br />        </span><span style="color: #0000BB">$ctxt_chunk </span><span style="color: #007700">= </span><span style="color: #0000BB">sodium_crypto_secretstream_xchacha20poly1305_push</span><span style="color: #007700">(</span><span style="color: #0000BB">$state</span><span style="color: #007700">, </span><span style="color: #0000BB">$ptxt_chunk</span><span style="color: #007700">);<br />        </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$outputFile</span><span style="color: #007700">, </span><span style="color: #0000BB">$ctxt_chunk</span><span style="color: #007700">);<br />    }<br /><br />    </span><span style="color: #0000BB">sodium_memzero</span><span style="color: #007700">(</span><span style="color: #0000BB">$state</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$inputFile</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$outputFile</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">// sodium_crypto_secretstream_xchacha20poly1305_keygen()<br /></span><span style="color: #0000BB">$key </span><span style="color: #007700">= </span><span style="color: #0000BB">sodium_base642bin</span><span style="color: #007700">(</span><span style="color: #DD0000">'MS0lzb7HC+thY6jY01pkTE/cwsQxnRq0/2L1eL4Hxn8='</span><span style="color: #007700">, </span><span style="color: #0000BB">SODIUM_BASE64_VARIANT_ORIGINAL</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">file_put_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'hello.txt'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Hello world!'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">encrypt_file</span><span style="color: #007700">(</span><span style="color: #DD0000">'hello.txt'</span><span style="color: #007700">, </span><span style="color: #DD0000">'hello.txt.encrypted'</span><span style="color: #007700">, </span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sodium_bin2hex</span><span style="color: #007700">(</span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'hello.txt.encrypted'</span><span style="color: #007700">)));<br /></span><span style="color: #0000BB">?&gt;</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="examplescode"><pre class="examplescode">string(106) &quot;971e33b255f0990ef3931caf761c59136efa77b434832f28ec719e3ff73f5aec38b3bba1574ab5b70a8844d8da36a668e802cfea2c&quot;</pre>
</div>
   </div>
  </div>
 </div>


</div><?php manual_footer($setup); ?>