Using this PCL branch of BouncyCastle https://www.nuget.org/packages/BouncyCastle-PCL/1.0.0.6 , it is really simple, virtually identical to the api windows.
public string ComputeHMAC(string message) { var keyBytes = Encoding.UTF8.GetBytes(Constants.API_KEY); var messageBytes = Encoding.UTF8.GetBytes(message); var hmac = new HMACSHA256(keyBytes); byte[] result = hmac.ComputeHash(messageBytes); return Convert.ToBase64String(result); }
And unit test using the actual .Net version:
[Test, AutoMoqData] public void Hash_Algorithm_Correct ( [NoAutoProperties] HashMacService sut, string message) { string expected; var key = Encoding.UTF8.GetBytes(Constants.API_KEY); using (var hmac = new HMACSHA256(key)) { var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(message)); expected = Convert.ToBase64String(hash); } var result = sut.ComputeHMAC(message); Assert.That(result, Is.EqualTo(expected)); }
I used PCLCrypto, but it continued to crash on Xamarin iOS, it was much cleaner and could be checked by the module, but PCLCrypto required the apis platform to be deployed on the device.
source share