<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.sodium-crypto-secretbox" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
 <refnamediv>
  <refname>sodium_crypto_secretbox</refname>
  <refpurpose>Authenticated shared-key encryption</refpurpose>
 </refnamediv>

 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <type>string</type><methodname>sodium_crypto_secretbox</methodname>
   <methodparam><type>string</type><parameter>message</parameter></methodparam>
   <methodparam><type>string</type><parameter>nonce</parameter></methodparam>
   <methodparam><type>string</type><parameter>key</parameter></methodparam>
  </methodsynopsis>
  <para>
   Encrypt a message with a symmetric (shared) key.
  </para>

 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <variablelist>
   <varlistentry>
    <term><parameter>message</parameter></term>
    <listitem>
     <para>
      The plaintext message to encrypt.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term><parameter>nonce</parameter></term>
    <listitem>
     <para>
      A number that must be only used once, per message. 24 bytes long.
      This is a large enough bound to generate randomly (i.e. <function>random_bytes</function>).
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term><parameter>key</parameter></term>
    <listitem>
     <para>
      Encryption key (256-bit).
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
   Returns the encrypted string.
  </para>
 </refsect1>

 <refsect1 role="errors">
  &reftitle.errors;
  <itemizedlist>
   <listitem>
    <simpara>
     If <parameter>nonce</parameter> has a length of bytes different than
     <link linkend="constant.sodium-crypto-secretbox-noncebytes"><constant>SODIUM_CRYPTO_SECRETBOX_NONCEBYTES</constant></link>
     (24 bytes), a <classname>SodiumException</classname> will be thrown.
    </simpara>
   </listitem>
   <listitem>
    <simpara>
     If <parameter>key</parameter> has a length of bytes different than
     <link linkend="constant.sodium-crypto-secretbox-keybytes"><constant>SODIUM_CRYPTO_SECRETBOX_KEYBYTES</constant></link>
     (32 bytes), a <classname>SodiumException</classname> will be thrown.
    </simpara>
   </listitem>
   <listitem>
    <simpara>
     Throws a <classname>SodiumException</classname> on failure.
    </simpara>
   </listitem>
  </itemizedlist>
 </refsect1>

 <refsect1 role="examples">
  &reftitle.examples;
  <para>
   <example>
    <title><function>sodium_crypto_secretbox</function> example</title>
    <programlisting role="php">
<![CDATA[
<?php
// The $key must be kept confidential
$key = sodium_crypto_secretbox_keygen();
// Do not reuse $nonce with the same key
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$plaintext = "message to be encrypted";
$ciphertext = sodium_crypto_secretbox($plaintext, $nonce, $key);

var_dump(bin2hex($ciphertext));
// The same nonce and key are required to decrypt the $ciphertext
var_dump(sodium_crypto_secretbox_open($ciphertext, $nonce, $key));
?>
]]>
    </programlisting>
    &example.outputs.similar;
    <screen>
<![CDATA[
string(78) "3a1fa3e9f7b72ef8be51d40abf8e296c6899c185d07b18b4c93e7f26aa776d24c50852cd6b1076"
string(23) "message to be encrypted"
]]>
    </screen>
   </example>
  </para>
 </refsect1>

 <refsect1 role="seealso">
  &reftitle.seealso;
  <para>
   <simplelist>
    <member><function>sodium_crypto_secretbox_open</function></member>
    <member><function>sodium_crypto_secretbox_keygen</function></member>
    <member><function>random_bytes</function></member>
   </simplelist>
  </para>
 </refsect1>

</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
