HMACSHA256 on Windows Phone 8.1?

a 夏天 提交于 2019-12-08 04:32:24

问题


According to this MSDN article, there exists a class for generating HMACSHA256 hash codes in the System.Security.Cryptography namespace on WP8. However, the .Cryptography namespace doesn't appear to exist. Is something wrong with my project or is this documentation wrong? Is there another way to compute HMACSHA256 hashes on WP8?

http://msdn.microsoft.com/library/windows/apps/system.security.cryptography.hmacsha256(v=vs.105).aspx


回答1:


After much anguish, I have a function that works.

public static string HmacSha256(string secretKey, string value)
{
    // Move strings to buffers.
    var key = CryptographicBuffer.ConvertStringToBinary(secretKey, BinaryStringEncoding.Utf8);
    var msg = CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8);

    // Create HMAC.
    var objMacProv = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256);
    var hash = objMacProv.CreateHash(key);
    hash.Append(msg);
    return CryptographicBuffer.EncodeToHexString(hash.GetValueAndReset());
}



回答2:


There are two types of Windows Phone 8.1 app: those that are based on Silverlight (like with WP7.X & WP8.0) and those based on the Universal/RT/Jupiter format (as also used by Windows 8.1).

The System.Security.Cryptography namespace is only available for Silverlight apps and is not available if using the newer/other format.

Yes, it's unfortunate that the documentation doesn't make this clear.



来源:https://stackoverflow.com/questions/24792142/hmacsha256-on-windows-phone-8-1

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