php版rsa以及rsa2签名的区别

  • 内容
  • 评论
  • 相关

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!");
		}
	}
}
?>

本文标签:

版权声明:若无特殊注明,本文皆为《菜鸟站长》原创,转载请保留文章出处。

本文链接:php版rsa以及rsa2签名的区别 - https://wlphp.com/?post=268

发表评论

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

福利广告

Fatal error: Uncaught exception 'RedisException' with message 'WRONGPASS invalid username-password pair' in /www/wwwroot/www.wlphp.com/content/templates/FLY/footer.php:264 Stack trace: #0 /www/wwwroot/www.wlphp.com/content/templates/FLY/footer.php(264): Redis->auth('Bsgj0711!') #1 /www/wwwroot/www.wlphp.com/content/templates/FLY/echo_log.php(133): include('/www/wwwroot/ww...') #2 /www/wwwroot/www.wlphp.com/include/controller/log_controller.php(105): include('/www/wwwroot/ww...') #3 /www/wwwroot/www.wlphp.com/include/lib/dispatcher.php(77): Log_Controller->displayContent(Array) #4 /www/wwwroot/www.wlphp.com/index.php(13): Dispatcher->dispatch() #5 {main} thrown in /www/wwwroot/www.wlphp.com/content/templates/FLY/footer.php on line 264