Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services net内核中的keyedhash算法_Amazon Web Services_Asp.net Core - Fatal编程技术网

Amazon web services net内核中的keyedhash算法

Amazon web services net内核中的keyedhash算法,amazon-web-services,asp.net-core,Amazon Web Services,Asp.net Core,我需要将以下.Net代码转换为.Net Core: static byte[] HmacSHA256(String data, byte[] key) { String algorithm = "HmacSHA256"; KeyedHashAlgorithm kha = KeyedHashAlgorithm.Create(algorithm); kha.Key = key; return kha.ComputeHash(Encoding.UTF8.GetByte

我需要将以下.Net代码转换为.Net Core:

static byte[] HmacSHA256(String data, byte[] key)
{
    String algorithm = "HmacSHA256";
    KeyedHashAlgorithm kha = KeyedHashAlgorithm.Create(algorithm);
    kha.Key = key;

    return kha.ComputeHash(Encoding.UTF8.GetBytes(data));
}
上面的代码片段用于AmazonAWS密钥签名,取自

我正在使用System.Security.Cryptography.Primitives 4.3.0和KeyedHashAlgorithm。创建方法不存在。查看,我可以看到Create方法现在已经存在,但它不受支持:

 public static new KeyedHashAlgorithm Create(string algName)
        {
            throw new PlatformNotSupportedException();
}

问题是,在.NETCore中,我的KeyedHashAlgorithm.Create(字符串algName)的替代方案是什么

.Net核心似乎提供了您所需要的功能:

static byte[] HmacSHA256(String data, byte[] key)
{
    HMACSHA256 hashAlgorithm = new HMACSHA256(key);

    return hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(data));
}

仅供参考,如果使用AWSSDK,您可以省去手动生成密钥签名的麻烦

using Amazon.Runtime.Internal.Auth;

DateTime now = DateTime.UtcNow;

string signature = ToHex(AWS4Signer.ComposeSigningKey("yourSecretKey", "us-west-2", now.ToString("yyyyMMdd"), "s3"));

public static string ToHex(byte[] data)
{
    var sb = new StringBuilder();
    for (var i = 0; i < data.Length; i++)
    {
        sb.Append(data[i].ToString("x2"));
    }
    return sb.ToString();
}
使用Amazon.Runtime.Internal.Auth;
DateTime now=DateTime.UtcNow;
字符串签名=ToHex(AWS4Signer.ComposeSigningKey(“yourSecretKey”,“us-west-2”,now.ToString(“yyyyMMdd”),“s3”);
公共静态字符串ToHex(字节[]数据)
{
var sb=新的StringBuilder();
对于(变量i=0;i
更换该行

KeyedHashAlgorithm kha = KeyedHashAlgorithm.Create(algorithm);


谢谢我认为这会起作用,只是代码中缺少了单词
new
newhmacsha256(键)@Ross Right-已修复;)别忘了把它扔进一个使用块这里是我如何使用建议的AWS4Signer的:
private string\u签名(string stringToSign){byte[]key=AWS4Signer.ComposeSigningKey(AppSettings.AmazonS3.SecretAccessKey,AppSettings.AmazonS3.Region,Datestamp,AppSettings.AmazonS3.Service);return(AWS4Signer.computekeydhash(SigningAlgorithm.HmacSHA256,key,stringToSign).Hex();}
KeyedHashAlgorithm kha = (KeyedHashAlgorithm)CryptoConfig.CreateFromName(algorithm);