ucloud api文档中对签名加密的方法实在看不明白,而且没有perl版本,特意写了一份供大家参考
use URI::Escape; use utf8; use Encode; use AnyEvent::HTTP::LWP::UserAgent; use Digest::SHA qw(sha1 sha1_hex sha1_base64); sub ucloudApiConn { my ( $publicKey, $privateKey, $baseUrl, $paramData ) = @_; #定义签名拼接字符串、http字符串 my ( $signatureStr, $params ); my $ua = LWP::UserAgent->new; for my $key ( sort keys %$paramData ) { $signatureStr .= $key; $signatureStr .= $paramData->{$key}; #将HTTP字符串转化uri方式 push @{$params}, "$key=" . uri_escape_utf8( $paramData->{$key} ); } #拼接私钥 $signatureStr .= $privateKey; #使用sha1算法加密 my $signatureKey = sha1_hex($signatureStr); #拼接传递的url参数 my $paramUrl = join "&", @$params; $paramUrl = $paramUrl . "&" . "Signature=$signatureKey"; my $url = "$baseUrl/?" . $paramUrl; print $url, "\n"; return $url; }
时间: 2024-11-07 15:40:53