使用sha256WithRSAEncryption签署需要什么版本的OpenSSL

Nie*_*tle 1 php openssl cryptography

使用PHP 5.2.4和OpenSSL 0.9.8g模块,我试图创建一个签名摘要

openssl_sign($stuff, $signeddigest, $key, 'sha256WithRSAEncryption');
Run Code Online (Sandbox Code Playgroud)

唉$ signeddigest返回为空,我没有错误.

如果不指定'sha256WithRSAEncryption'算法,则使用默认算法返回带符号的摘要.

PHP 5.3.10和OpenSSL 1.0.0g上运行的代码相同.OpenSSL 0.9.8g中不支持'sha256WithRSAEncryption'算法吗?

Nie*_*tle 8

一位好朋友提出了在旧的PHP 5.2.4和OpenSSL 0.9.8g模块上使用sha256WithRSAEncryption的解决方法.

使用http://www.di-mgt.com.au/rsa_alg.html上提供的信息,他给我写了以下代码:

function my_openssl_sign($data, &$signature, $priv_key_id, $signature_alg = 'sha256WithRSAEncryption') {
    $pinfo = openssl_pkey_get_details($priv_key_id);
    $hash = hash('sha256', $data);
    $t = '3031300d060960864801650304020105000420'; # sha256
    $t .= $hash;
    $pslen = $pinfo['bits']/8 - (strlen($t)/2 + 3);

    $eb = '0001' . str_repeat('FF', $pslen) . '00' . $t;
    $eb = pack('H*', $eb);

    return openssl_private_encrypt($eb, $signature, $priv_key_id, OPENSSL_NO_PADDING); 
}
Run Code Online (Sandbox Code Playgroud)

谢谢你,Mads,你真是太棒了!