private byte[] MacTest2(string FileName, byte[] IKm) { using (FileStream inStream = new FileStream(FileName, FileMode.Open)) { using (MacStream mac = new MacStream(new HMAC(new SHA512(), IKm))) { mac.Initialize(inStream); mac.IsConcurrent = false; return(mac.ComputeMac()); } } }
private bool MacTest3(byte[] IKm) { byte[] data = new CSPPrng().GetBytes(33033); byte[] hash1; byte[] hash2; using (MacStream mac1 = new MacStream(new HMAC(new SHA512(), IKm))) { mac1.Initialize(new MemoryStream(data)); mac1.IsConcurrent = false; hash1 = mac1.ComputeMac(); } using (HMAC mac2 = new HMAC(new SHA512(), IKm)) hash2 = mac2.ComputeMac(data); return(Evaluate.AreEqual(hash1, hash2)); }
private void HmacDescriptionTest() { CSPPrng rng = new CSPPrng(); byte[] data = rng.GetBytes(rng.Next(100, 400)); byte[] key = rng.GetBytes(64); HMAC mac = new HMAC(Digests.SHA256); mac.Initialize(key); byte[] c1 = mac.ComputeMac(data); MacDescription mds = new MacDescription(64, Digests.SHA256); MacStream mst = new MacStream(mds, new KeyParams(key)); mst.Initialize(new MemoryStream(data)); byte[] c2 = mst.ComputeMac(); if (!Evaluate.AreEqual(c1, c2)) { throw new Exception("MacStreamTest: HMAC code arrays are not equal!"); } }
private void CmacDescriptionTest() { CSPPrng rng = new CSPPrng(); byte[] data = rng.GetBytes(rng.Next(100, 400)); byte[] key = rng.GetBytes(32); byte[] iv = rng.GetBytes(16); CMAC mac = new CMAC(BlockCiphers.Rijndael); mac.Initialize(key, iv); byte[] c1 = mac.ComputeMac(data); MacDescription mds = new MacDescription(32, BlockCiphers.Rijndael, IVSizes.V128, BlockSizes.B128, RoundCounts.R14); MacStream mst = new MacStream(mds, new KeyParams(key, iv)); mst.Initialize(new MemoryStream(data)); byte[] c2 = mst.ComputeMac(); if (!Evaluate.AreEqual(c1, c2)) { throw new Exception("MacStreamTest: CMAC code arrays are not equal!"); } }
/// <summary> /// Test the MacStream class implementation /// <para>Throws an Exception on failure</</para> /// </summary> public static void StreamMacTest() { byte[] data; byte[] key; MemoryStream instrm; MemoryStream outstrm = new MemoryStream(); using (KeyGenerator kg = new KeyGenerator()) { data = kg.GetBytes(512); key = kg.GetBytes(64); } // data to digest instrm = new MemoryStream(data); byte[] code1; byte[] code2; using (MacStream sm = new MacStream(new HMAC(new SHA512(), key))) { sm.Initialize(instrm); code1 = sm.ComputeMac(); } using (HMAC hm = new HMAC(new SHA512())) { hm.Initialize(key); code2 = hm.ComputeMac(data); } // compare the hash codes if (!Evaluate.AreEqual(code1, code2)) { throw new Exception(); } }