php,RSA签名

RSA签名

/**
     * RSA签名
     * @param $data 待签名数据
     * @param $private_key 私钥字符串
     * return 签名结果
     */
    function rsaSign($data, privateKey) {
        $search = [
            "-----BEGIN RSA PRIVATE KEY-----",
            "-----END RSA PRIVATE KEY-----",
            "\n",
            "\r",
            "\r\n"
        ];
        $private_key = str_replace($search,"", privateKey);
        $private_key = $search[0] . PHP_EOL . wordwrap($private_key, 64, "\n", true) . PHP_EOL . $search[1];
        $pkeyid = openssl_get_privatekey($private_key);
        if($pkeyid) {
            openssl_sign($data, $sign, $pkeyid, OPENSSL_ALGO_MD5);
            openssl_free_key($pkeyid);
        }else{
            exit("私钥格式有误");
        }
        $sign = base64_encode($sign);
        return $sign;
    }

 

你可能感兴趣的:(PHP)