Create HMAC-SHA256 hash with BouncyCastle

I need to create a HMAC-SHA256 hash in PCL (for developing Xamarin Forms) that does not support the built-in HMAC / .NET cryptography classes, so I work with BouncyCastle to implement my cryptographic classes.

I need to generate the HMAC-SHA256 hash, but I could not find any examples on Google, and BouncyCastle does not seem to have documentation for this. Can anyone help me out?

+5
source share
3 answers

Thanks to the solution here, I came up with this code:

public class HmacSha256 { public byte[] Hash(string text, string key) { var hmac = new HMac(new Sha256Digest()); hmac.Init(new KeyParameter(Encoding.UTF8.GetBytes(key))); byte[] result = new byte[hmac.GetMacSize()]; byte[] bytes = Encoding.UTF8.GetBytes(text); hmac.BlockUpdate(bytes, 0, bytes.Length); hmac.DoFinal(result, 0); return result; } } 

Corresponds to unit test (uses FluentAssertions):

 [TestClass] public class HmacSha256Tests { private readonly HmacSha256 _hmac = new HmacSha256(); [TestMethod] public void Hash_GeneratesValidHash_ForInput() { // Arrange string input = "hello"; string key = "test"; string expected = "F151EA24BDA91A18E89B8BB5793EF324B2A02133CCE15A28A719ACBD2E58A986"; // Act byte[] output = _hmac.Hash(input, key); string outputHex = BitConverter.ToString(output).Replace("-", "").ToUpper(); // Assert expected.Should().Be(outputHex); } } 
+5
source

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.

+1
source
 private static void CreateToken(string message, string key) { System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); byte[]keyByte = encoding.GetBytes(key); HMACSHA256 hmacsha = new HMACSHA256(keyByte); byte[]messageBytes = encoding.GetBytes(message); byte[]hashmessage = hmacsha.ComputeHash(messageBytes); Console.WriteLine(ByteToString(hashmessage)); } public static string ByteToString(byte[]buff) { string sbinary = ""; for (int i = 0; i < buff.Length; i++) { sbinary += buff[i].ToString("X2"); // hex format } return (sbinary); } 

Above code, my time was saved while working in HMAC-SHA256, I hope this can help someone, and here is the link in detail http://billatnapier.com/security01.aspx

0
source

Source: https://habr.com/ru/post/1247992/


All Articles