0
采纳
php数字签名与校验
1. 生成一对公钥和私钥
$res = openssl_pkey_new();
openssl_pkey_export($res,$pri);
$d= openssl_pkey_get_details($res);
$pub = $d['key'];
var_dump($pri,$pub);
输出依次为私钥和公钥的pem字串
2. 保存私钥,公钥公开
如果需要对某数据进行签名,证明那数据是你签名的,就需要用私钥:
$res = openssl_pkey_get_private($pri);
if (openssl_sign('hello', $out, $res))
var_dump(base64_encode($out));
上例中 $pri 为自己的私钥,'hello' 为待签名的数据,如果签名成功,最后输出为base64编码后的签名
3. 别人用你公开的公钥验证你的数据签名
$sig = base64_decode($sig);
$res = openssl_pkey_get_public($pubkey);
if (openssl_verify('hello', $sig, $res) === 1);
// 通过验证
上例中 $sig 为之前你base64编码过的签名, $pubkey 为你的公钥