Exemplo n.º 1
0
        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());
        }
Exemplo n.º 2
0
        private static void hashPortionOfStream(SHA384 sha384, FileStream stream, int start, int end)
        {
            stream.Seek(start, SeekOrigin.Begin);
            int fileLengthToHash = (end - start);

            byte[] buf = new byte[65535];
            while (fileLengthToHash > 0)
            {
                int l = stream.Read(buf, 0, (int)Math.Min(fileLengthToHash, buf.Length));
                fileLengthToHash -= l;
                sha384.TransformBlock(buf, 0, l, null, 0);
            }
        }
Exemplo n.º 3
0
 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();
 }
Exemplo n.º 4
0
 /// <summary>
 /// Updates the hash with data.
 /// </summary>
 /// <param name="data">Data buffer.</param>
 /// <param name="len">Length of buffer to hash.</param>
 public void Update(byte[] data, uint len)
 {
     _sha384Provider.TransformBlock(data, 0, (int)len, data, 0);
 }
Exemplo n.º 5
0
 protected override void HashCore(byte[] array, int ibStart, int cbSize)
 {
     hash.TransformBlock(array, ibStart, cbSize, null, 0);
 }