PHP RSA 签名 可刷新运行 刷新

代码:

function generate(int $private_key_bits = 1024)
{
    $rsa = [
        "private_key" => "",
        "public_key" => ""
    ];
    $config = [
        "digest_alg" => "sha512",
        "private_key_bits" => $private_key_bits,
        "private_key_type" => OPENSSL_KEYTYPE_RSA,
    ];
    $res = openssl_pkey_new($config);
    openssl_pkey_export($res, $rsa['private_key']);
    $rsa['public_key'] = openssl_pkey_get_details($res)["key"];
    return $rsa;
}




function sign($data, $priKey, $code = 'base64')
{
    $ret = false;
    if (openssl_sign($data, $ret, openssl_get_privatekey($priKey))) {
        $ret = encode($ret, $code);
    }
    return $ret;
}
function encode($data, $code)
{
    switch (strtolower($code)) {
        case 'base64':
            $data = base64_encode('' . $data);
            break;
        case 'hex':
            $data = bin2hex($data);
            break;
        case 'bin':
        default:
    }
    return $data;
}

function verify($data, $sign, $pubKey,$code = 'base64')
{
    $ret = false;
    $sign = decode($sign, $code);
    if ($sign !== false) {
        switch (openssl_verify($data, $sign, openssl_get_publickey($pubKey))) {
            case 1:
                $ret = true;
                break;
            case 0:
            case -1:
            default:
                $ret = false;
        }
    }
    return $ret;
}

function decode($data, $code){
    switch (strtolower($code)) {
        case 'base64':
            $data = base64_decode($data);
            break;
        case 'hex':
            $data = ($data !== false && preg_match('/^[0-9a-fA-F]+$/i', $data) ? pack("H*", $data) : false);
            break;
        case 'bin':
        default:
    }
    return $data;
}


$rsa=generate();

$private_key=$rsa['private_key'];
$public_key=$rsa['public_key'];

echo "private_key:\n" . $private_key . "\n\r";
echo "public_key:\n" . $public_key . "\n\r";

$data=json_encode(['code'=>0]);
echo '要签名的数据:' . $data, "\n-------------------------------\n";

$sign=sign($data,$private_key);
echo "生成的签名: " . $sign . "\n";

$verify=verify($data,$sign,$public_key);
echo "验证的结果: \n";
var_export($verify);
echo "\n";

输出:

private_key:
-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALwouLzI7QMTG6NV
G/iHqrFP7OsHYVoiU8g2RPW2w3jHBy+Hb2cSWTPnh6vpZnhfbCUquH8NZFuYjlZW
dZEWOhX4MZD0zdRSOujbrANVEu27p/1XtMuN3DWMS2S96r24w4Hhi4B84K7mBgNz
Z6/5y+0T7ePo6u19VCkSawrp6CrrAgMBAAECgYBZoMQvGT7dQhLeQMJsJqPGhp4C
bqeO+d8S9dbvfLmsJJJlr4v/exwGv3k/CJUJLdsj8S+lRcnSX98dfV3PWNkdoNeE
wUHjs3DosAWevLFTJVMyYN3u5VWzRyciUWUAPYplhwwcjpv7nT4b9C8JfnCNzq07
+FRe/Wq8UPGOEvwYUQJBAN8h0RiWe4Uyfk0PAQXUyNS0ZglD8Y2CIdYESE3zGyFX
pzevSPWzRzsVqu0QBTPEB04XYMJstjIVBQuzy77A770CQQDX4BeQt9Kl3N7+vjbj
TwFnz0xre+yOglHMZimBJQCx3t06An/mGcmrOE18EULYlGrjUWogVwYPfAGahVSu
lnvHAkEAwsVrFzJsWgWMeElrlCJ8uCdMljPny6VXUdDlKJ3YBc/UmGf6oPbAa9Pr
IwoD8H7h/Ymsnap1k9wDTobAiB80ZQJBAKpunIw/5MnYSCH80N7DOEvaCO5vB0M3
thbWrXvtV9AWMLik0o862tIYAGysQAc3ne/cDhp8OietcqKpE0NHQW8CQAVMbS0r
Kob5s2pI4boeHepOGTQGmU46DL0vlEtcY1HOrOdxqTHOmtOTJn0ZIrkTzDBH4mwu
8IuNmoKAJafWBu8=
-----END PRIVATE KEY-----


public_key:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8KLi8yO0DExujVRv4h6qxT+zr
B2FaIlPINkT1tsN4xwcvh29nElkz54er6WZ4X2wlKrh/DWRbmI5WVnWRFjoV+DGQ
9M3UUjro26wDVRLtu6f9V7TLjdw1jEtkveq9uMOB4YuAfOCu5gYDc2ev+cvtE+3j
6OrtfVQpEmsK6egq6wIDAQAB
-----END PUBLIC KEY-----


要签名的数据:{"code":0}
-------------------------------
生成的签名: PSYfOebN5MpJwtys9x9W/Y1HB2C2ui+jrUEIj3W5YXA9hXURgjJLoicauAmZpWZZTV/3qxoF9jYeafLK32Ab9PZ1YB6z98wyIGnwqc+uWmnliMfdVyLY7fVvnYgFZnO+KgZYS5BxO8SHFGnb7iHW1YRmRFUj49sntscCtxr3JXI=
验证的结果: 
true