Home > Backend Development > PHP Tutorial > php openssl_sign()函数有什么用?

php openssl_sign()函数有什么用?

PHPz
Release: 2020-09-04 17:14:01
Original
6040 people have browsed it

php openssl_sign()函数用于生成签名;该函数通过使用与$priv_key_id参数关联的私钥生成加密数字签名,为指定的数据输入签名。$priv_key_id是一个由openssl_get_privatekey()返回的密钥。

php openssl_sign()函数有什么用?

php openssl_sign()函数

openssl_sign - 生成签名

语法

openssl_sign( string $data , string &$signature , mixed $priv_key_id [, mixed $signature_alg = OPENSSL_ALGO_SHA1 ] )
Copy after login

openssl_sign() 通过使用与priv_key_id关联的私钥生成加密数字签名,为指定的数据输入签名。 请注意,数据本身未加密。

参数:

1.png

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE。

示例

//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);
Copy after login

版本支持

2.png

更多相关知识,请访问 PHP中文网!!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template