<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.openssl-seal" xmlns="http://docbook.org/ns/docbook">
 <refnamediv>
  <refname>openssl_seal</refname>
  <refpurpose>Seal (encrypt) data</refpurpose>
 </refnamediv>
 
 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <type class="union"><type>int</type><type>false</type></type><methodname>openssl_seal</methodname>
   <methodparam><type>string</type><parameter>data</parameter></methodparam>
   <methodparam><type>string</type><parameter role="reference">sealed_data</parameter></methodparam>
   <methodparam><type>array</type><parameter role="reference">encrypted_keys</parameter></methodparam>
   <methodparam><type>array</type><parameter>public_key</parameter></methodparam>
   <methodparam><type>string</type><parameter>cipher_algo</parameter></methodparam>
   <methodparam choice="opt"><type>string</type><parameter role="reference">iv</parameter><initializer>&null;</initializer></methodparam>
  </methodsynopsis>
  <para>
   <function>openssl_seal</function> seals (encrypts)
   <parameter>data</parameter> by using the given <parameter>cipher_algo</parameter> with a randomly generated
   secret key. The key is encrypted with each of the public keys
   associated with the identifiers in <parameter>public_key</parameter>
   and each encrypted key is returned
   in <parameter>encrypted_keys</parameter>. This means that one can send
   sealed data to multiple recipients (provided one has obtained their
   public keys). Each recipient must receive both the sealed data and
   the envelope key that was encrypted with the recipient's public key.
  </para>
 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <para>
   <variablelist>
    <varlistentry>
     <term><parameter>data</parameter></term>
     <listitem>
      <para>
       The data to seal.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>sealed_data</parameter></term>
     <listitem>
      <para>
       The sealed data.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>encrypted_keys</parameter></term>
     <listitem>
      <para>
       Array of encrypted keys.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>public_key</parameter></term>
     <listitem>
      <para>
       Array of <classname>OpenSSLAsymmetricKey</classname> instances containing public keys.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>cipher_algo</parameter></term>
     <listitem>
      <para>
       The cipher method.
       <caution>
        <simpara>
         The default value (<literal>'RC4'</literal>) is considered insecure.
         It is strongly recommended to explicitly specify a secure cipher method.
        </simpara>
       </caution>
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>iv</parameter></term>
     <listitem>
      <para>
       The initialization vector.
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </para>
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
   Returns the length of the sealed data on success, or &false; on error.
   If successful the sealed data is returned in
   <parameter>sealed_data</parameter>, and the envelope keys in
   <parameter>encrypted_keys</parameter>.
  </para>
 </refsect1>

 <refsect1 role="changelog">
  &reftitle.changelog;
  <informaltable>
   <tgroup cols="2">
    <thead>
     <row>
      <entry>&Version;</entry>
      <entry>&Description;</entry>
     </row>
    </thead>
    <tbody>
     <row>
      <entry>8.0.0</entry>
      <entry>
       <parameter>public_key</parameter> accepts an &array; of
       <classname>OpenSSLAsymmetricKey</classname> instances now;
       previously, an &array; of &resource;s of type <literal>OpenSSL key</literal>
       was accepted.
      </entry>
     </row>
     <row>
      <entry>8.0.0</entry>
      <entry>
       <parameter>cipher_algo</parameter> is no longer an optional parameter.
      </entry>
     </row>
     <row>
      <entry>8.0.0</entry>
      <entry>
       <parameter>iv</parameter> is nullable now.
      </entry>
     </row>
    </tbody>
   </tgroup>
  </informaltable>
 </refsect1>

 <refsect1 role="examples">
  &reftitle.examples;
  <para>
   <example>
    <title><function>openssl_seal</function> example</title>
    <programlisting role="php">
<![CDATA[
<?php
// $data is assumed to contain the data to be sealed

// fetch public keys for our recipients, and ready them
$fp = fopen("/src/openssl-0.9.6/demos/maurice/cert.pem", "r");
$cert = fread($fp, 8192);
fclose($fp);
$pk1 = openssl_get_publickey($cert);
// Repeat for second recipient
$fp = fopen("/src/openssl-0.9.6/demos/sign/cert.pem", "r");
$cert = fread($fp, 8192);
fclose($fp);
$pk2 = openssl_get_publickey($cert);

// seal message, only owners of $pk1 and $pk2 can decrypt $sealed with keys
// $ekeys[0] and $ekeys[1] respectively.
openssl_seal($data, $sealed, $ekeys, array($pk1, $pk2));

// free the keys from memory
openssl_free_key($pk1);
openssl_free_key($pk2);
?>
]]>
    </programlisting>
   </example>
  </para>
 </refsect1>

 <refsect1 role="seealso">
  &reftitle.seealso;
  <para>
   <simplelist>
    <member><function>openssl_open</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
-->
