1000 points if you can answer this question...

S

Skidz Tweak

Hi all...
This is a weird one.. Top points to anyone that gets it...

I have a function that hashes a file (Byte[]) I just opened... I
already know what the hash should be, because I stored that info
before I saved the file... But when I run a function to check if they
match.. sometimes they don't... but.. if I break in... and rehash in
the immediate window.. they match correctly...

This is the function that check the file I just opened.. to see if the
hash matches...
Parameter info:
string hashCode is what the hash code should be...
byte[] results is the file I just loaded (yes the stream is closed by
the time I reach here)


public void FileCheck(string hashCode, Byte[] results)
{
string hashCheck =
Convert.ToBase64String(SHA256HashGenerator.ComputeHash(results)) +
results.Length;
if (hashCode != hashCheck)
MainConsole.Instance.Error("[AssetDataPlugin]:
Resulting files didn't match hash."); // <<<<< THIS IS WHERE I BREAK
IN
else if (waserror)
MainConsole.Instance.Error("[AssetDataPlugin]: Asset
does match... ");
}

you can see commented above in the function.. where I break in.. (it
says "THIS IS WHERE I BREAK IN") then I do the following in the
immediate window..
? hashCheck
"1VGnTd7l7tzZJD2vROE8ORxX+h8IltoZ6nHqBEDKRz0=23338"
? hashCode
"VPeLSIp0TJZlqRAgfr75SJo2HA01QA6vgkClEqMxNOQ=23338"
? Convert.ToBase64String(SHA256HashGenerator.ComputeHash(results)) +
results.Length;
"VPeLSIp0TJZlqRAgfr75SJo2HA01QA6vgkClEqMxNOQ=23338"

So.. the 1 thousand point question... why is hashCheck different? it
should be the same.. right???
 
A

Arne Vajhøj

[...]
you can see commented above in the function.. where I break in.. (it
says "THIS IS WHERE I BREAK IN") then I do the following in the
immediate window..
? hashCheck
"1VGnTd7l7tzZJD2vROE8ORxX+h8IltoZ6nHqBEDKRz0=23338"
? hashCode
"VPeLSIp0TJZlqRAgfr75SJo2HA01QA6vgkClEqMxNOQ=23338"
? Convert.ToBase64String(SHA256HashGenerator.ComputeHash(results)) +
results.Length;
"VPeLSIp0TJZlqRAgfr75SJo2HA01QA6vgkClEqMxNOQ=23338"

So.. the 1 thousand point question... why is hashCheck different? it
should be the same.. right???

Assuming the current stack frame is in fact the "FileCheck" method, then
the only way to explain the behavior you claim to be occurring is for the
contents of the "results" array to be modified by a different thread after
you compute the "hashCheck" value but before you re-attempt the computation
in the Immediate window.

Or another thread is using SHA256HashGenerator.

HashAlgorithm ComputeHash is not thread safe.

Arne
 
S

Skidz Tweak

Thanks Arne...Peter...

Ame WINS!! :)
Thats what it was..

I change the code
From:
Convert.ToBase64String(SHA256HashGenerator.ComputeHash(results)) +
results.Length;
To:
Convert.ToBase64String(new SHA256Managed().ComputeHash(results)) +
results.Length;

and that seems to have fixed it...

Thanks a bunch Ame, you were tremendous help! I don't think I would
have thought of that without you :)

Sorry Peter :( your ideas where good though...

If you would like to see the exact change made:
https://github.com/aurora-sim/Aurora-Sim/compare/05c962a...b4c9149

Thanks again guys :)
 
A

Arne Vajhøj

Thanks Arne...Peter...

Ame WINS!! :)
Thats what it was..

I change the code
From:
Convert.ToBase64String(SHA256HashGenerator.ComputeHash(results)) +
results.Length;
To:
Convert.ToBase64String(new SHA256Managed().ComputeHash(results)) +
results.Length;

and that seems to have fixed it...

Thanks a bunch Ame, you were tremendous help! I don't think I would
have thought of that without you :)

Sorry Peter :( your ideas where good though...

If you would like to see the exact change made:
https://github.com/aurora-sim/Aurora-Sim/compare/05c962a...b4c9149

An almost classic example of a thread concurrency issue.

And the ComputeHash method looks pretty innocent. You send
bytes in and get a checksum back - there is nothing in
the signature that give hints to it not being thread safe.

The reality is that it is a convenience method in an
abstract super class that first calls one method
implemented in a concrete sub class with the data
resulting in state of the object being updated and
then calls another method implemented in the concrete
sub class to get the hash from the stored state.

So not only does the docs say that instance methods
may not be thread safe - in this case it is not
thread safe.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top