<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.openssl-sign" xmlns="http://docbook.org/ns/docbook">
 <refnamediv>
  <refname>openssl_sign</refname>
  <refpurpose>Generate signature</refpurpose>
 </refnamediv>
 
 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <type>bool</type><methodname>openssl_sign</methodname>
   <methodparam><type>string</type><parameter>data</parameter></methodparam>
   <methodparam><type>string</type><parameter role="reference">signature</parameter></methodparam>
   <methodparam><type class="union"><type>OpenSSLAsymmetricKey</type><type>OpenSSLCertificate</type><type>array</type><type>string</type></type><parameter>private_key</parameter></methodparam>
   <methodparam choice="opt"><type class="union"><type>string</type><type>int</type></type><parameter>algorithm</parameter><initializer><constant>OPENSSL_ALGO_SHA1</constant></initializer></methodparam>
  </methodsynopsis>
  <para>
   <function>openssl_sign</function> computes a signature for the
   specified <parameter>data</parameter> by generating a cryptographic
   digital signature using the private key associated with
   <parameter>private_key</parameter>. Note that the data itself is
   not encrypted.
  </para>
 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <para>
   <variablelist>
    <varlistentry>
     <term><parameter>data</parameter></term>
     <listitem>
      <para>
       The string of data you wish to sign
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>signature</parameter></term>
     <listitem>
      <para>
       If the call was successful the signature is returned in
       <parameter>signature</parameter>.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>private_key</parameter></term>
     <listitem>
      <para>
       <classname>OpenSSLAsymmetricKey</classname> - a key, returned by <function>openssl_get_privatekey</function>
      </para>
      <para>
       <type>string</type> - a <acronym>PEM</acronym> formatted key
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>algorithm</parameter></term>
     <listitem>
      <para>
       <type>int</type> - one of these <link linkend="openssl.signature-algos">Signature Algorithms</link>.
      </para>
      <para>
       <type>string</type> - a valid string returned by <function>openssl_get_md_methods</function> example, "sha256WithRSAEncryption" or "sha384".
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </para>
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
   &return.success;
  </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>private_key</parameter> accepts an <classname>OpenSSLAsymmetricKey</classname>
       or <classname>OpenSSLCertificate</classname> instance now;
       previously, a &resource; of type <literal>OpenSSL key</literal> or <literal>OpenSSL X.509</literal>
       was accepted.
      </entry>
     </row>
    </tbody>
   </tgroup>
  </informaltable>
 </refsect1>

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

// fetch private key from file and ready it
$pkeyid = openssl_pkey_get_private("file://src/openssl-0.9.6/demos/sign/key.pem");

// compute signature
openssl_sign($data, $signature, $pkeyid);

// free the key from memory
openssl_free_key($pkeyid);
?>
]]>
    </programlisting>
   </example>
   <example>
    <title><function>openssl_sign</function> example</title>
    <programlisting role="php">
<![CDATA[
<?php
//data you want to sign
$data = 'my data';

//create new private and public key
$new_key_pair = openssl_pkey_new(array(
    "private_key_bits" => 2048,
    "private_key_type" => OPENSSL_KEYTYPE_RSA,
));
openssl_pkey_export($new_key_pair, $private_key_pem);

$details = openssl_pkey_get_details($new_key_pair);
$public_key_pem = $details['key'];

//create signature
openssl_sign($data, $signature, $private_key_pem, OPENSSL_ALGO_SHA256);

//save for later
file_put_contents('private_key.pem', $private_key_pem);
file_put_contents('public_key.pem', $public_key_pem);
file_put_contents('signature.dat', $signature);

//verify signature
$r = openssl_verify($data, $signature, $public_key_pem, "sha256WithRSAEncryption");
var_dump($r);
?>
]]>
    </programlisting>
   </example>
 
  </para>
 </refsect1>

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