none
MD5 Hash Returning different values on IIS

    Question

  • I'm comparing css files using md5 hash to see if the file has been updated.  Here's a quick summary:

    // Get the hash value

     

    byte[] result = md5.ComputeHash(fileStream);

    // Converting to a hex value for comparison

     

    StringBuilder sb = new StringBuilder();

     

    for (int i = 0; i < result.Length; i++)

    {

    sb.Append(result[i].ToString(

    "x2"));

    }

     

    return sb.ToString();

     


    This gives me a unique name based on the number of bytes.  I compare this name with the existing css file.  If nothing has changed, they should be named  the same and the file should exist.

    This works fine when I test it locally using Visual Studio's built in server.  However, when I push this to production, it gives me different md5 values, even though nothing has changed.

    I'm stumped.  Any help is appreciated.

    Thanks

    James


    James
    Wednesday, January 27, 2010 8:01 PM

Answers

  • Hi,

    How do you retrieve the css file , it appears it has been changed when it's on the server.
    If the files files are the same , the MD5 will not change.

    You might also need to try IIS forum for more help:
    http://forums.iis.net/

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Harry Zhu Tuesday, February 09, 2010 3:23 AM
    Tuesday, February 02, 2010 4:50 AM

All replies

  • Hi,

    How do you retrieve the css file , it appears it has been changed when it's on the server.
    If the files files are the same , the MD5 will not change.

    You might also need to try IIS forum for more help:
    http://forums.iis.net/

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Harry Zhu Tuesday, February 09, 2010 3:23 AM
    Tuesday, February 02, 2010 4:50 AM
  • Hi,

    Apply the code this way:

    public string FileToMD5Hash(string path)

            {

                using (System.Security.Cryptography.MD5 md = new System.Security.Cryptography.MD5CryptoServiceProvider())

                {

                    StringBuilder sb = new StringBuilder(); 

                    byte[] file = Encoding.UTF8.GetBytes(path);

                    byte[] hash = md.ComputeHash(file);

                    for(int i = 0; i<hash.Length; i++)

                    {

                        sb.Append(hash[i].ToString("x2"));  

                    }

                    return sb.ToString(); 

                }

            }

     

    Add the using and the namespaces + classes inside a using()
    this means that the code will only be applied inside your function/method
    which in this case is FileToMd5Hash(); .

    For higher security and better monitoring of file changes apply the SHA256-hash algorithm.

    I hope this helps...

    Have a nice day...

    Best regards,
    Fisnik


    Coder24.com
    Tuesday, February 02, 2010 6:50 AM
  • Hi,

    Have you tried downloaded the files to see what is changed? It's hard for us to see the problem without more info.

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, February 05, 2010 8:55 AM