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-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALXEk6kEldXTTm1o
ou1ed7Vo/DAIkdw7R45hpKRwmpTdukmvuA0yLg6uB91Ar8Pe7p99UAhWk+YG2lj9
W6pYC3svi671VznehfUawYEirtAzKLPJw/cRm2cejOw1oeOEDANjG1yfsQTlKAHK
IU9tuHvTqRcorN0NG7ulEbGftnntAgMBAAECgYEAmmbPX4VrmeUS+9EkhSn32fvC
LTM6Y69fS00aRCz6wnXOpoVDfMIhaYrfV0x5gZK/dG9IKq0QmRc9JtagpQSBSsS+
gNnbhZvD6pgChMiYYnQH1Dr2/WK0b2sfG+bxHi/QusYf4HHxGhQBT5p6jA0x4+jE
p9xxAAW0TnUNNI2EV0ECQQDsm+S/NcP/kJeRYPdY5YnavlwrMjzLjCWzPW2kRLOg
Gj+Pc7AW51TQ+Sath+0XjjFs/DgK6mclekdfkSmknvOZAkEAxKobDUjtbT0zsLxB
vhnA3dqqKMlCAVSxVxVydH+8Nvr6Sa2C79fJa7uLh9Ow5iU1cnj14hNym+jqICB1
58FtdQJBANeVHzH9Sg0UcJVclWIhGp79LTuKdYw8sKkEwl6W3ld1Mw6AqSPlOQmC
yfP40lTREUWs9V80vjCOnnN770Z4MZkCQFBULwKJJDm3gvKQosXUXRwBYfmQ4z4J
6vnoD70YI7fNYghQMChpWqN/kmmjhSEsV51vXPe/+9NFGsSOnbpRHrUCQDYUo4j1
U238heANOz3wXIHx6eGOIbeVN/vP/oMB7kHZ2mbeKrkhCKvtmQwfCmeEGRNL+/21
xG/5OvgMXyV8H1s=
-----END PRIVATE KEY-----


public_key:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC1xJOpBJXV005taKLtXne1aPww
CJHcO0eOYaSkcJqU3bpJr7gNMi4OrgfdQK/D3u6ffVAIVpPmBtpY/VuqWAt7L4uu
9Vc53oX1GsGBIq7QMyizycP3EZtnHozsNaHjhAwDYxtcn7EE5SgByiFPbbh706kX
KKzdDRu7pRGxn7Z57QIDAQAB
-----END PUBLIC KEY-----


要签名的数据:{"code":0}
-------------------------------
生成的签名: Kyj/GlvNiba1CZA/84eLpFE3RS7CCkXl20aIKcEnVxMurk1X8dx0j56DQPs5LlAoza4VT3Qu8xGnMn7RzEcGdDCQCb0dqI1hqu/w6qxE/0UOvdD0bStb0TANue+rjZdBsQ7mqryZcn+UzoL8FIjdzTKDJzaZ58fKiEB5TpJF+kY=
验证的结果: 
true
遇到问题?