Reputation: 370
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?
Upvotes: 1
Views: 1350
Reputation: 370
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());
}
Upvotes: 3
Reputation: 65556
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.
Upvotes: 1