Question

I am trying to make a program which computes hash values of two dates.The dates are creation date of the file and the date time when it was last updated.I am getting the same hast values for both in every case even if date of updation and creation are different.My simple aim is to computes the hash values of the two dates and compare them. Please help me with my code . Correct the code or suggest any new code.Thanks in advance.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace ConsoleApplication3
{


    class Program
    {
        static void Main()
        {
            string source, source1;

            FileInfo info = new FileInfo("D:\\file.txt");

            DateTime time1 = info.CreationTime;
            Console.WriteLine(time1);

            DateTime time2 = info.LastAccessTime;
            Console.WriteLine(time2);


            DateTime time3 = info.LastWriteTime;
            Console.WriteLine(time3);

            source = Convert.ToString(time1);
            source1 = Convert.ToString(time3);
            using (MD5 md5Hash = MD5.Create())
            {
                string hash = GetMd5Hash(md5Hash, source);
                string hash1 = GetMd5Hash(md5Hash, source1);
                Console.WriteLine("The MD5 hash of" + time1 + "is:" + hash + ".");
                Console.WriteLine("The MD5 hash of" + time3 + "is:" + hash1 + ".");

                if (VerifyMd5Hash(md5Hash, source, hash))
                {
                    Console.WriteLine("The hashes are the same.");
                }
                else
                {
                    Console.WriteLine("The hashes are not same.");
                }
                Console.ReadLine();
            }
        }



        static string GetMd5Hash(MD5 md5Hash, string source1)
        {

            FileInfo info = new FileInfo("D:\\file.txt");
            DateTime time3 = info.LastWriteTime;

            source1 = Convert.ToString(time3);
            // Convert the input string to a byte array and compute the hash. 
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(source1));

            // Create a new Stringbuilder to collect the bytes 
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data  
            // and format each one as a hexadecimal string. 
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string. 
            return sBuilder.ToString();
        }

        // Verify a hash against a string. 
        static bool VerifyMd5Hash(MD5 md5Hash, string source1, string hash)
        {
            // Hash the input. 
            string hashOfInput = GetMd5Hash(md5Hash, source1);

            // Create a StringComparer an compare the hashes.
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }

        }


    }

}
Was it helpful?

Solution

Your function GetMd5Hash has a file baked into it that overrides whatever gets passed in. Just kill off the first three lines inside of it

static string GetMd5Hash(MD5 md5Hash, string source1)
{
    //FileInfo info = new FileInfo("D:\\file.txt");
    //DateTime time3 = info.LastWriteTime;

    //source1 = Convert.ToString(time3);

OTHER TIPS

I think ther is an issues in GetMd5Hash method. independently by your input you compute hash using info.LastWriteTime from same file.

A good approach is to always use input from external call in procedures, to avoid this kind of errors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top