Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MD5\SHA256 hash troubles #8723

Closed
vertexpipeline opened this issue Aug 10, 2017 · 2 comments
Closed

MD5\SHA256 hash troubles #8723

vertexpipeline opened this issue Aug 10, 2017 · 2 comments

Comments

@vertexpipeline
Copy link

After hashing via MD5, SHA256 returns wrong results.
Example:

var file = "C:\\Games\\StarCraft II\\SC2Data\\data\\data.008";
var stream = File.Open(file, FileMode.Open);
var startTime = DateTime.Now;
var hashMd5 = MD5.Create().ComputeHash(stream);// hashing MD5 
var end = DateTime.Now - startTime;
WriteLine($"Md5 hash time - {end}, hash = {Convert.ToBase64String(hashMd5)}"); // ~15 sec.

var hashSha256 = SHA256.Create().ComputeHash(stream);//~ 0.5
end = DateTime.Now - startTime;
WriteLine($"SHA256 hash time - {end}, hash = {Convert.ToBase64String(hashSha256)}"); //returns wrong hash

var hashSha512 = SHA512.Create().ComputeHash(stream); // it is too
end = DateTime.Now - startTime;
WriteLine($"SHA512 hash time - {end}, hash = {Convert.ToBase64String(hashSha512)}");

The example returns results after 15 seconds average, but why SHA256 returns result after ~ 0.5 sec.
There is a correct hash:

var file = "C:\\Games\\StarCraft II\\SC2Data\\data\\data.008";
var stream = File.Open(file, FileMode.Open);
var startTime = DateTime.Now;
var hashSha512 = SHA512.Create().ComputeHash(stream);//hashing 1:30 min.
var end = DateTime.Now - startTime;
WriteLine($"SHA512 hash time - {end}, hash = {Convert.ToBase64String(hashSha512)}");//returns correct hash

Thanks.

@stephentoub
Copy link
Member

You're not resetting the stream back to the beginning, so the first hash hashes the file, and the next ones have no data to hash.

@vertexpipeline
Copy link
Author

oh, thanks. It's works. I don't know, why i didn't it...

@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants