How to add digital signature (RSA, Certificate, etc) to any of file, using PHP?

后端 未结 3 1303
我寻月下人不归
我寻月下人不归 2020-12-14 04:45

I need to know if any kind of file can be signed digitally, using RSA, a certificate, and that stuff, or if only certain kind of files can be signed. All this, using PHP.

3条回答
  •  不知归路
    2020-12-14 05:21

    Using phpseclib, a pure PHP RSA implementation (updated here):

    createKey());
    
    $plaintext = 'terrafrost';
    
    $rsa->loadKey($privatekey);
    $signature = $rsa->sign($plaintext);
    
    $rsa->loadKey($publickey);
    echo $rsa->verify($plaintext, $signature) ? 'verified' : 'unverified';
    ?>
    

    The analog to that with the CLI openssl is as follows:

    openssl dgst -sha1 -sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:-1 -out signature.txt -sign privatekey.txt plaintext.txt 
    openssl dgst -sha1 -sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:-1 -prverify privatekey.txt -signature signature.txt plaintext.txt 
    

提交回复
热议问题