C to Perl Translation HMAC SHA1,数据和密钥

时间:2015-04-08 03:22:56

标签: c# perl hmacsha1

我想将下面的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;
        }
    }
}

1 个答案:

答案 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模块。