Click here to Skip to main content
15,898,222 members
Articles / Operating Systems / Windows
Article

MD5SUM FOR C#

Rate me:
Please Sign up or sign in to vote.
3.00/5 (13 votes)
5 Jan 2005 65K   14   10
How to compute the same hash as MD5SUM does (HEX).

Reason For This Howto

As you may know the MD5CryptoService, which the .NET Framework uses, doesn't create an md5 sum's string. So you only have the pure byte[] which isn't that useful if you want to use it in a database or on your Web Server to compare the sum with your file's sum! So, if many people have no idea how to get their sum into the right way, here's my howto to create the same sums like "md5sum" does:

Code

This is the second version with the Hex-bug fixed.

C#
using System.Security.Cryptography;
.....
.....
string MD5SUM(byte[] FileOrText) //Output: String<-> Input: Byte[] //
{
return BitConverter.ToString(new 
    MD5CryptoServiceProvider().ComputeHash(FileOrText)).Replace("-","").ToLower();
} 

// Quite simple isn't it? I hope you enjoyed this tiny article and 
//I hope I could help ya! //

You can use this example of course for SHA1 as well, just change the MD5CryptoServiceProvider to SHA1CryptoServiceProvider.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Austria Austria
I am Max Gattringer an Austrian student - I've got a passion for programming C#, cryptograpy and nearly everything concerning PCs Big Grin | :-D

Comments and Discussions

 
GeneralThis is great! Pin
mondokat3-Apr-07 10:27
mondokat3-Apr-07 10:27 
GeneralMD5 Pin
ARROLLO2-Jan-06 14:12
ARROLLO2-Jan-06 14:12 
GeneralPerformance Pin
Axel Rietschin2-Feb-05 16:03
professionalAxel Rietschin2-Feb-05 16:03 
GeneralYeah, you're right ;-) Pin
Anonymous7-Jan-05 12:36
Anonymous7-Jan-05 12:36 
GeneralAnother suggestion Pin
MrEyes5-Jan-05 23:09
MrEyes5-Jan-05 23:09 
GeneralJust two suggestions Pin
Dennis C. Dietrich5-Jan-05 10:58
Dennis C. Dietrich5-Jan-05 10:58 
GeneralRe: Just two suggestions Pin
Robert Rohde5-Jan-05 20:09
Robert Rohde5-Jan-05 20:09 
GeneralRe: Just two suggestions Pin
Dennis C. Dietrich6-Jan-05 1:08
Dennis C. Dietrich6-Jan-05 1:08 
Robert Rohde wrote:
In general I agree that StringBuilder is faster. But in this case the calculation of the MD5 value will by far run longer than the string concatenation, so the change will not be significant.
First rule for optimizing: Only optimize what is relevant for optimization (90-10 rule).


I know what you mean and personally I would even apply another rule before it comes to the 90-10 rule you quoted (that would be the 'optimization last' rule of XP). However, I don't think that should be used in this case because this is about .NET best practices rather than general purpose rules. Consider the case that you want to calculate the MD5 hash not for one file (or a few big files) but for a bunch of small files. Calculating the MD5 hash for a few ten thousand files would lead to a few hundred thousand unnecessary memcopy operations during the string creation. And I'd say that is significant (although you might argue that it wouldn't matter on today's CPUs).

Best regards from Schöneberg
Dennis
GeneralRe: Just two suggestions Pin
Jeffrey Sax6-Jan-05 13:52
Jeffrey Sax6-Jan-05 13:52 
GeneralRe: Just two suggestions Pin
Max Gattringer7-Jan-05 12:52
Max Gattringer7-Jan-05 12:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.