mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-20 10:58:54 +00:00

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@334621 c90b9560-bf6c-de11-be94-00142212c4b1
170 lines
4.5 KiB
XML
170 lines
4.5 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<refentry xmlns="http://docbook.org/ns/docbook" xml:id="function.openssl-sign">
|
|
<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>mixed</type><parameter>priv_key_id</parameter></methodparam>
|
|
<methodparam choice="opt"><type>mixed</type><parameter>signature_alg</parameter><initializer>OPENSSL_ALGO_SHA1</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>priv_key_id</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>priv_key_id</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
<type>resource</type> - a key, returned by <function>openssl_get_privatekey</function>
|
|
</para>
|
|
<para>
|
|
<type>string</type> - a PEM formatted key
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>signature_alg</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="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
|
|
-->
|