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?
1
votes
If you find no built-in way, there's a free CryptoBlackbox package in our SecureBlackbox product which will let you do this. eldos.com/sbb/desc-cryptobb.php
– Eugene Mayevski 'Callback
The MSDN page that you referenced contains "Supported in: 8.1, 8.0, 7.1, 7.0". Did you include/import all the relevant libraries in your project?
– Oleg Estekhin
2 Answers
3
votes
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());
}
1
votes
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.