phpseclib:使用证书验证签名数据

phpseclib: Validating signed data using certificate

我确实有 private.pempublic.crt。我的目标是使用 private.pem 签名并使用 public.crt 验证其签名。如何使用 phpseclib 实现此目的?

$data = 'test';
$rsa = new RSA();
$privatekey = file_get_contents(storage_path('app/private.pem'));
$rsa->loadKey($privatekey); 
$signed = $rsa->sign($data);

$publickey = file_get_contents(storage_path('app/public.crt'));
$rsa->loadKey($publickey);

return $rsa->verify($data, $signed) ? 'verified' : 'unverified';

得到我的答案here:

<?php
$data = 'test';
$rsa = new RSA();
$x509 = new X509();
$privatekey = file_get_contents(storage_path('app/private.pem'));
$rsa->loadKey($privatekey);
$signed = $rsa->sign($data);

$publickey = file_get_contents(storage_path('app/public.crt'));
$x509->loadX509($publickey);
$rsa = $x509->getPublicKey();
return $rsa->verify($data, $signed) ? 'verified' : 'unverified';