HMACSHA256 on Windows Phone 8.1?

大兔子大兔子 提交于 2019-12-06 15:36:07

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());
}

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.

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