/// <summary> /// Returns the SHA512 HMAC hash for the file /// </summary> /// <param name="FilePath">Path to input file</param> /// <param name="HashKey">HMAC key</param> /// <returns>Computed hash value [byte[]]</returns> internal byte[] GetChecksum(string FilePath, byte[] HashKey) { using (SHA512HMAC hmac = new SHA512HMAC(HashKey)) { int blockSize = hmac.BlockSize; int bytesTotal = 0; byte[] buffer = new byte[blockSize]; byte[] chkSum = new byte[hmac.DigestSize]; using (BinaryReader inputReader = new BinaryReader(new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.None))) { // start of file bytes inputReader.BaseStream.Position = MessageHeader.GetHeaderSize; int bytesRead = 0; // read into mac while ((bytesRead = inputReader.Read(buffer, 0, blockSize)) == blockSize) { hmac.BlockUpdate(buffer, 0, bytesRead); bytesTotal += bytesRead; if (bytesTotal > MAX_SIGNED) break; } // last block if (bytesRead > 0) hmac.BlockUpdate(buffer, 0, bytesRead); // get the hash hmac.DoFinal(chkSum, 0); } return chkSum; } }
private bool MacTest3(byte[] IKm) { byte[] data = new CSPRng().GetBytes(33033); byte[] hash1; byte[] hash2; using (StreamMac mac1 = new StreamMac(new SHA512HMAC(IKm))) { mac1.Initialize(new MemoryStream(data)); mac1.IsConcurrent = false; hash1 = mac1.ComputeMac(); } using (SHA512HMAC mac2 = new SHA512HMAC(IKm)) hash2 = mac2.ComputeMac(data); return Compare.AreEqual(hash1, hash2); }
private void MacTest(byte[] Key, byte[] Input, byte[] Out256, byte[] Out512) { byte[] hash = new byte[32]; byte[] trnHash; // test 1: HMAC interface HMAC hmac1 = new HMAC(new SHA256()); hmac1.Initialize(new KeyParams(Key)); hash = hmac1.ComputeMac(Input); // truncated output, test case #5 if (Out256.Length != 32) { trnHash = new byte[Out256.Length]; Buffer.BlockCopy(hash, 0, trnHash, 0, Out256.Length); if (!Compare.AreEqual(Out256, trnHash)) throw new Exception("SHA256HMAC is not equal! Expected: " + HexConverter.ToString(Out256) + " Received: " + HexConverter.ToString(trnHash)); } else { if (!Compare.AreEqual(Out256, hash)) throw new Exception("SHA256HMAC is not equal! Expected: " + HexConverter.ToString(Out256) + " Received: " + HexConverter.ToString(hash)); } // test 2: 256 hmac using (SHA256HMAC hmac2 = new SHA256HMAC()) { hmac2.Initialize(new KeyParams(Key)); hash = hmac2.ComputeMac(Input); } if (Out256.Length != 32) { trnHash = new byte[Out256.Length]; Buffer.BlockCopy(hash, 0, trnHash, 0, Out256.Length); if (!Compare.AreEqual(Out256, trnHash)) throw new Exception("SHA256HMAC is not equal! Expected: " + HexConverter.ToString(Out256) + " Received: " + HexConverter.ToString(trnHash)); } else { if (!Compare.AreEqual(Out256, hash)) throw new Exception("SHA256HMAC is not equal! Expected: " + HexConverter.ToString(Out256) + " Received: " + HexConverter.ToString(hash)); } hash = new byte[32]; // test 3: HMAC interface hmac1 = new HMAC(new SHA512()); hmac1.Initialize(new KeyParams(Key)); hash = hmac1.ComputeMac(Input); if (Out512.Length != 64) { trnHash = new byte[Out512.Length]; Buffer.BlockCopy(hash, 0, trnHash, 0, Out512.Length); if (!Compare.AreEqual(Out512, trnHash)) throw new Exception("SHA512HMAC is not equal! Expected: " + HexConverter.ToString(Out512) + " Received: " + HexConverter.ToString(trnHash)); } else { if (!Compare.AreEqual(Out512, hash)) throw new Exception("SHA512HMAC is not equal! Expected: " + HexConverter.ToString(Out512) + " Received: " + HexConverter.ToString(hash)); } // test 4: 256 hmac using (SHA512HMAC hmac3 = new SHA512HMAC()) { hmac3.Initialize(new KeyParams(Key)); hash = hmac3.ComputeMac(Input); } if (Out512.Length != 64) { trnHash = new byte[Out512.Length]; Buffer.BlockCopy(hash, 0, trnHash, 0, Out512.Length); if (!Compare.AreEqual(Out512, trnHash)) throw new Exception("SHA512HMAC is not equal! Expected: " + HexConverter.ToString(Out512) + " Received: " + HexConverter.ToString(trnHash)); } else { if (!Compare.AreEqual(Out512, hash)) throw new Exception("SHA512HMAC is not equal! Expected: " + HexConverter.ToString(Out512) + " Received: " + HexConverter.ToString(hash)); } }