How to calculate HMAC-SHA1 authentication code in .NET 4.5 Core

三世轮回 提交于 2019-12-03 15:55:59
jbtule

The Windows.Security.Cryptography namespace does contain HMAC.

You create a MacAlgorithmProvider object by calling the static OpenAlgorithm method and specifying one of the following algorithm names: HMAC_MD5 HMAC_SHA1 HMAC_SHA256 HMAC_SHA384 HMAC_SHA512 AES_CMAC

http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.core.macalgorithmprovider.aspx

public static byte[] HmacSha1Sign(byte[] keyBytes, string message){ 
    var messageBytes= Encoding.UTF8.GetBytes(message);
    MacAlgorithmProvider objMacProv = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
    CryptographicKey hmacKey = objMacProv.CreateKey(keyBytes.AsBuffer());
    IBuffer buffHMAC = CryptographicEngine.Sign(hmacKey, messageBytes.AsBuffer());
    return buffHMAC.ToArray();

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!