public void FIPS186_d(string testName, SHA384 hash, byte[] input, byte[] result)
 {
     byte[] output = hash.TransformFinalBlock(input, 0, input.Length);
     // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
     // AssertEquals( testName + ".d.1", result, output );
     Assert.IsNotNull(output, testName + ".d.1");
     Assert.AreEqual(result, hash.Hash, testName + ".d.2");
     // required or next operation will still return old hash
     hash.Initialize();
 }
        public static string HashExecutable(string path)
        {
            WindowsBinaryData r = ExecutableParser(path);

            byte[] selfHash;
            int    checkSumIndex = r.checkSumPos;
            int    tableIndex    = r.CertificateTableSizePos - 4;
            int    endIndex      = 0;

            if (r.CertificateTableAddress != 0)
            {
                endIndex = r.CertificateTableAddress;
            }

            if (endIndex == 0)
            {
                // Hash the entire file except the .msh at the end if .msh is present
                int mshLen = GetMshLengthFromExecutable(path);
                if (mshLen > 0)
                {
                    mshLen += 20;
                }
                using (SHA384 sha384 = SHA384Managed.Create())
                {
                    sha384.Initialize();
                    using (FileStream stream = File.OpenRead(path))
                    {
                        hashPortionOfStream(sha384, stream, 0, (int)stream.Length - mshLen); // Start --> end - (mshLen + 20)
                        sha384.TransformFinalBlock(new byte[0], 0, 0);
                        selfHash = sha384.Hash;
                    }
                }
                return(BitConverter.ToString(selfHash).Replace("-", string.Empty).ToLower());
            }

            using (SHA384 sha384 = SHA384Managed.Create())
            {
                sha384.Initialize();
                using (FileStream stream = File.OpenRead(path))
                {
                    hashPortionOfStream(sha384, stream, 0, checkSumIndex);              // Start --> checkSumIndex
                    sha384.TransformBlock(new byte[4], 0, 4, null, 0);                  // 4 zero bytes
                    hashPortionOfStream(sha384, stream, checkSumIndex + 4, tableIndex); // checkSumIndex + 4 --> tableIndex
                    sha384.TransformBlock(new byte[8], 0, 8, null, 0);                  // 8 zero bytes
                    hashPortionOfStream(sha384, stream, tableIndex + 8, endIndex);      // tableIndex + 8 --> endIndex
                    sha384.TransformFinalBlock(new byte[0], 0, 0);
                    selfHash = sha384.Hash;
                }
            }
            return(BitConverter.ToString(selfHash).Replace("-", string.Empty).ToLower());
        }
 public void FIPS186_e(string testName, SHA384 hash, byte[] input, byte[] result)
 {
     byte[] copy = new byte [input.Length];
     for (int i = 0; i < input.Length - 1; i++)
     {
         hash.TransformBlock(input, i, 1, copy, i);
     }
     byte[] output = hash.TransformFinalBlock(input, input.Length - 1, 1);
     // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
     // AssertEquals (testName + ".e.1", result, output);
     Assert.IsNotNull(output, testName + ".e.1");
     Assert.AreEqual(result, hash.Hash, testName + ".e.2");
     // required or next operation will still return old hash
     hash.Initialize();
 }
Example #4
0
 /// <summary>
 /// Returns a byte array of the hash value.
 /// </summary>
 public byte[] Final()
 {
     _sha384Provider.TransformFinalBlock(new byte[0], 0, 0);
     return(_sha384Provider.Hash);
 }
Example #5
0
 protected override byte[] HashFinal()
 {
     hash.TransformFinalBlock(Empty, 0, 0);
     HashValue = hash.Hash;
     return(HashValue);
 }