我想将下面的C代码转换为perl。有人知道怎么做吗?你使用hmac_sha1_hex($data, $key);
吗?你使用digest:sha模块吗?我想用密钥签名一些数据,但是使用perl代替C。
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Web;
namespace Amazon.Cba.Signature.Common
{
public class SignatureCalculator
{
public SignatureCalculator()
{
}
public String calculateRFC2104HMAC(String data, String key)
{
String result = null;
KeyedHashAlgorithm algorithm = new HMACSHA1();
Encoding encoding = new UTF8Encoding();
algorithm.Key = encoding.GetBytes(key);
result = Convert.ToBase64String(algorithm.ComputeHash(encoding.GetBytes(data.ToCharArray())));
return result;
}
}
}
答案 0 :(得分:2)
我会这样做:
use MIME::Base64 ();
use Digest::HMAC_SHA1 ();
my $result = MIME::Base64::encode_base64(
Digest::HMAC_SHA1::hmac_sha1( $data, $key ),
''
);
或者如果你想要一个url-safe base64编码:
my $result = MIME::Base64::encode_base64url(
Digest::HMAC_SHA1::hmac_sha1( $data, $key ),
);
虽然你也可以一起使用单独的Digest :: HMAC和Digest :: SHA模块。