note 85870 rejected from function.openssl-sign by thiago
| From: | thiago@php.net | Date: | Mon, 22 Sep 2008 12:28:59 +0000 |
| Subject: | note 85870 rejected from function.openssl-sign by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-144604@lists.php.net to get a copy of this message | ||
Note Submitter: hanbsd at 163 dot com
----
i create private key with
$configargs = array(
"digest_alg" => "sha1",
"private_key_bits" => 1024,
"private_key_type" => OPENSSL_KEYTYPE_DSA,
"encrypt_key" => false
);
But I can not get signature by openssl_sign($data, $signature, $key).
Then I use openssl in shell
#openssl dgst -dss1 -sign id_dsa foo.sha1 > sigfile.bin
openssl create a signature file : sigfile.bin
#openssl dgst -dss1 -verify id_dsa.pub -signature sigfile.bin foo.sha1
openssl print: "Verified OK"
It looks something bug of PHP function openssl_sign()
My PHP Version: 5.2.6
demo code:
<?php
$data = "sfsdfsdfs";
// First we need to have a string containing the private key in PEM format
// this file comes with OpenSSL 0.9.6
$fp = fopen("/home/id_dsa", "r");
$pkey = fread($fp, 8192);
fclose($fp);
// get private key from the PEM format
// $key is an encr key resource id if success, else false
$key = openssl_get_privatekey($pkey);
// calculate signature
openssl_sign($data, $signature, $key);
openssl_free_key($key);
// recipient verifies signature
// read certificate
$fp = fopen("/home/id_dsa.pub", "r");
$cert = fread($fp, 8192);
fclose($fp);
// Get public key from the certificate
$pubkey = openssl_get_publickey($cert);
// state whether signature is okay or not
echo openssl_verify($data, $signature, $pubkey) == 1 ? "ok\n" : "bad\n";
// free key
openssl_free_key($pubkey);
?>