// https://csharp.hotexamples.com/examples/Org.BouncyCastle.Crypto.Macs/Poly1305/BlockUpdate/php-poly1305-blockupdate-method-examples.html public static void Poly1305AES() { Org.BouncyCastle.Crypto.IMac mac = new Org.BouncyCastle.Crypto.Macs.Poly1305(new Org.BouncyCastle.Crypto.Engines.AesEngine()); byte[] key = null; byte[] n = new byte[16]; byte[] input = System.Text.Encoding.UTF8.GetBytes("Hello World!"); byte[] output = new byte[mac.GetMacSize()]; for (int loop = 0; loop < 13; loop++) { mac.Init(new Org.BouncyCastle.Crypto.Parameters.ParametersWithIV(new Org.BouncyCastle.Crypto.Parameters.KeyParameter(key), n)); mac.BlockUpdate(input, 0, input.Length); mac.DoFinal(output, 0); } }
private void Poly1305CheckHash(byte[] keyMaterial, byte[] input, byte[] tag) { Org.BouncyCastle.Crypto.Macs.Poly1305 poly1305 = new Org.BouncyCastle.Crypto.Macs.Poly1305(); poly1305.Init(new Org.BouncyCastle.Crypto.Parameters.KeyParameter(keyMaterial)); poly1305.BlockUpdate(input, 0, input.Length); byte[] mac = new byte[poly1305.GetMacSize()]; poly1305.DoFinal(mac, 0); if (!Org.BouncyCastle.Utilities.Arrays.AreEqual(tag, mac)) { System.Console.WriteLine("fail"); // Fail("rfc7539", Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(tag), Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(mac)); } }