Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I'm having some problems when get md5 of a file -----> put it is a
if(a="ewfewf")
{
...}
but i got some mistakes.
Please help!

What I have tried:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Posted
Updated 13-Jun-19 2:28am

1 solution

Here's the code you can use to get started.
If you get LINQPad - The .NET Programmer's Playground[^] you can drop the code into a new file and run it and see the output.

C#
void Main()
{
	// creating a temp file so the test will work on your machine.
        var tempFile1 = Path.GetTempFileName();
        // writing bytes which will be used to generate MD5 hash
	File.AppendAllText(tempFile1, "this is a test");
        // read all bytes of file so we can send them to the MD5 hash algo
	Byte [] allBytes = File.ReadAllBytes(tempFile1);
	System.Security.Cryptography.HashAlgorithm md5Algo = null;
	md5Algo = new System.Security.Cryptography.MD5CryptoServiceProvider();
        // compute the Hash (MD5) on the bytes we got from the file
	byte[] hash = md5Algo.ComputeHash(allBytes);
	Console.WriteLine(BytesToHex(hash));
      // output will be : 54B0C58C7CE9F2A8B551351102EE0938
     File.Delete(tempFile1);  // delete the temp file
      // Now you just need to do this for the second file and compare the strings.
	
}

private string BytesToHex(byte[] bytes) 
{ 
	// write each byte as two char hex output.
    return String.Concat(Array.ConvertAll(bytes, x => x.ToString("X2"))); 
}
 
Share this answer
 
v2
Comments
Member 13369156 13-Jun-19 22:45pm    
Thank you very much!
[no name] 18-Jun-19 8:19am    
Thanks for help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900