File hash changes each time for the same file

Topics: Updater Application Block
Aug 9, 2006 at 10:09 AM
originally posted by: MarkNijhof


When trying to auto update my application I get the following message:

'Hash value for the file specified in manifest cannot be null if hash comparison is enabled. Hash value for file xxx.dll is null.'

but the hash value is defenitely in the manifest.xml file. I am using NAnt and CruiseControl to build my project and have created a quick and dirty task that creates the hashes, I took the code from the Update Manifest Tool. But than I noticed the hash keep changing even if the file doesn't. Exact same behavior from the Update Manifest Tool.

I was under the impression that the file hash should be the same each time you generate it when he file hasn't changed. Or is this expected behaviour?

Bellow is the C# part of the NAnt task
CryptographyConfigurationView cryptoView = new CryptographyConfigurationView(new FileConfigurationSource(project.Properties"config.path" + project.Properties"config.file"));
CryptographySettings cryptoSettings = cryptoView.CryptographySettings;
HashProviderData provider = cryptoView.GetHashProviderData("MD5CryptoServiceProvider");
if (provider is KeyedHashAlgorithmProviderData)
KeyedHashAlgorithmProviderData data = provider as KeyedHashAlgorithmProviderData;

// if the hash key file path is relative, combine it with the configFilePath
if (!Path.IsPathRooted(data.ProtectedKeyFilename))
data.ProtectedKeyFilename = Path.Combine(project.Properties"config.path", data.ProtectedKeyFilename);
HashProviderFactory factory = new HashProviderFactory(new FileConfigurationSource(project.Properties"config.path" + project.Properties"config.file"));
IHashProvider alg = factory.Create("MD5CryptoServiceProvider");

using (FileStream fs = File.Open(project.Properties"filename", FileMode.Open, FileAccess.Read, FileShare.Read))
// put the file in the buffer
byte[] fileBytes = new byte fs.Length ;
fs.Read( fileBytes, 0, fileBytes.Length );
// compute hash
byte[] hash = alg.CreateHash(fileBytes);
HashString = Convert.ToBase64String(hash);

The commented part I added because it is done in the Updater Manifest Tool and I haven't looked realy into what it does, but it doesn't matter in the output.

Hope someone can help me fix this, I don't want to create the Hashes manually each time NAnt has build a new version :)

Aug 9, 2006 at 10:36 AM
originally posted by: MarkNijhof

ps here is the confog file:

<section name="UpdaterConfiguration" type="Microsoft.ApplicationBlocks.Updater.Configuration.ApplicationUpdaterSettings, Microsoft.ApplicationBlocks.Updater" />
<section name="securityCryptographyConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration.CryptographySettings, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=, Culture=neutral, PublicKeyToken=null" />
<UpdaterConfiguration defaultDownloader="BITS" applicationId="{215E1AD7-9ABA-432f-A952-24BABA556850}" manifestUri="">
type="Microsoft.ApplicationBlocks.Updater.Downloaders.BitsDownloader, Microsoft.ApplicationBlocks.Updater.Downloaders"
targetServerType="BGAUTHTARGET_SERVER" />
algorithmType="System.Security.Cryptography.MD5CryptoServiceProvider, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"
type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.HashAlgorithmProvider, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=, Culture=neutral, PublicKeyToken=null"
name="MD5CryptoServiceProvider" />