Search code examples
phpencryptionx509phpseclib

phpseclib: Validating signed data using certificate


I do have a private.pem and public.crt. my goal is to signed using private.pem and to verify its signature using public.crt. How do I achieve this by using 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';

Solution

  • got my answer 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';