1.都是使用openssl_sign,和openssl_verify函数,只是最后一个参数不同rsa为OPENSSL_ALGO_SHA1,rsa2为OPENSSL_ALGO_SHA256,下面是PHP本rsa签名实现类
<?php //rsa和rsa2 PHP版签名生成以及验证类 //$content,$signature,$publicKey,$type,$content原文,$signature被验证的签名,$publicKey和$privateKey公私钥都是只有内容的一行字符串,$type为rsa,或者rsa2 class RSA{ public static function sign($content,$privateKey,$type){ if($type == "rsa"){ openssl_sign($content,$signature,"-----BEGIN PRIVATE KEY-----\n".$privateKey."\n-----END PRIVATE KEY-----", OPENSSL_ALGO_SHA1); }elseif ($type == "rsa2") { openssl_sign($content,$signature,"-----BEGIN PRIVATE KEY-----\n".$privateKey."\n-----END PRIVATE KEY-----", OPENSSL_ALGO_SHA256); }else{ throw new Exception("Only support OPENSSL_ALGO_SHA1 or OPENSSL_ALGO_SHA256 algorithm signature!"); } return base64_encode($signature); } public static function verify($content,$signature,$publicKey,$type){ if($type == "rsa"){ return openssl_verify($content,base64_decode($signature),"-----BEGIN PUBLIC KEY-----\n".$publicKey."\n-----END PUBLIC KEY-----", OPENSSL_ALGO_SHA1); }elseif ($type == "rsa2") { return openssl_verify($content,base64_decode($signature),"-----BEGIN PUBLIC KEY-----\n".$publicKey."\n-----END PUBLIC KEY-----", OPENSSL_ALGO_SHA256); }else{ throw new Exception("Only support OPENSSL_ALGO_SHA1 or OPENSSL_ALGO_SHA256 algorithm signature verify!"); } } } ?>