建设银行 php 基于 sha1withRSA 算法的签名 和 验签

作者: cayman 分类: 未分类 发布时间: 2018-04-21 11:17
 function alonersaSign($data,$privatekey,$signType = "RSA",$keyfromfile=false) {

    if(!$keyfromfile){
        $priKey=$privatekey;
        $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
            wordwrap($priKey, 64, "\n", true) .
            "\n-----END RSA PRIVATE KEY-----";
    }
    else{
        $priKey = file_get_contents($privatekey);
        $res = openssl_get_privatekey($priKey);
    }

    ($res) or die('您使用的私钥格式错误,请检查RSA私钥配置');

    if ("RSA2" == $signType) {
        openssl_sign($data, $sign, $res, OPENSSL_ALGO_SHA256);
    } else {
        openssl_sign($data, $sign, $res);
    }

    if($keyfromfile){
        openssl_free_key($res);
    }
    $sign = base64_encode($sign);
    return $sign;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注