views:

288

answers:

3

Hello everyone. In a WinForm application using C#.NET 2.0 (on Vista), I am using SHA1 hash to create a hash from a string and store the hash in a text file (with UTF-8 encoding). I want to use the hash stored in text file to in a condition. When I run the project in Vista it works properly (i.e. the condition results in true), but when I run in XP the project does not run.

Is the way hash created in Vista different from XP?

Code extract

byte[] HashValue;
byte[] MessageBytes = Encoding.UTF8.GetBytes(strPlain);
SHA1Managed SHhash = new SHA1Managed();
StringBuilder strHex = new StringBuilder("");
HashValue = SHhash.ComputeHash(MessageBytes);
foreach (byte b in HashValue)
{
    strHex.AppendFormat("{0:x2}", b);
}
// storing strHex in a text file with UTF-8 encoding

Test condition

string newHash = Program.GetHash("This will be hashed.");
// GetHash() does has the same code as above, but instead of storing hash in file in return
// hash.
bool validHash = newHash.Equals(oldHash);
// old has is the one stored in file
if (validHash)
{
    // some code
}

[Edit]

The main problem is the same code works fine in Vista, but breaks down in XP. If there is some logical problem it should not work in any OS.

Thanks.

A: 

I am curious why you mention UTF-8 encoding in relation to storing the hash value in a text file. Are you attempting to store the raw data bytes, somehow converted to UTF-8, or are you storing a hexadecimal representation of the hash value?

Normally when storing a hash value in a text file, you would use the hex representation, such as:

3e2f9d9069abd6ace2cb18f7390a06c034a0f9dd

There would be no need to specifically use UTF-8 encoding, since the above is plain ordinary ASCII.

Greg Hewgill
Thanks. I am using hex representation as shown above. Is UTF-8 causing the problem? Regards
kobra
It is unlikely that UTF-8 is causing your problem. Can you post the code you're using?
Greg Hewgill
A: 

I suspect that the old hash stored in the file might be incorrect. Try out a simple console application snippet on each machine. Something like:

Console.WriteLine(Program.GetHash("This will be hashed."));

If these are indeed giving the same result then it must be something to do with the comparison routine (i.e. likely oldHash mentioned above).

One other thing to note; I see you're using bool validHash to store the comparison results however checking the boolean validSource afterwards. Is this just a mistype?

Paul Mason
Thanks for point out validSource. I have corrected it. It was just mistype. Regards
kobra
Hi. <code>Console.WriteLine(Program.GetHash("This will be hashed."));</code> give different result in XP and Vista. No idea why as the code is same in both case.
kobra
+1  A: 

How are you passing the binaries between the machines? I once encountered a hash validation problem when zipping the binaries with the maximum compression mode 7zip offers and unzipping it with winzip on the other side, when I was preparing a ClickOnce package on my machine.

Marc Wittke
Thanks. I am email the binaries in compressed (zip) format.
kobra