Label Cloud

Thursday, December 20, 2007

Calculating Hashvalues for files the way .NET does for the Application Manifest

hey

I had to write a custom download component to download modules for a ClickOnce deployed application. The actual downloading is simple, the tricky part was creating the manifest and make sure that I only download files that are required.

I am using an GeneraApplicationManifest MSBuild task to generate an application manifest. The documentation is very easy to follow.

The generated manifest will include a Hash value. It is fairly simple to compute the same hash value manually and be able to validate it.

private bool HashChanged(string fileName, string originalHashValue)
        {
            byte[] Hash = Convert.FromBase64String(originalHashValue));
            byte[] newHash;
            SHA1Managed sha = new SHA1Managed();
         
            FileStream strm = null;
            try
            {
                strm = new FileStream(fileName, FileMode.Open, FileAccess.Read);         
                newHash = sha.ComputeHash(strm);
            }
            finally
            {
                if (strm != null)
                    strm.Close();
            }

            if (Hash.Length != newHash.Length)
                return true;
            for(int i = 0; i< Hash.Length; i++)
            {
                if (Hash[i] != newHash[i])
                    return true;
            }
            return false;
        }
Technorati Tags: ,,,


Share/Save/Bookmark

No comments:

Directory of Computers/Tech Blogs