How to calculate MD5 in C#

Not only wget but also MD5SUM, I have been asked how to implement this functionality in C#. Again and again, I wrote below code in Mono on Ubuntu Feisty.

using System;
using System.Text;
using System.Security.Cryptography;
 
class MD5Sum {
    public string GetMd5Sum(string str) {
        byte[] input = ASCIIEncoding.ASCII.GetBytes(str);
        byte[] output = MD5.Create().ComputeHash(input);
        StringBuilder sb = new StringBuilder();
        for(int i=0;i
              

I used very similar Makefile as follows.

TARGET=md5sum.exe
MCS=mcs
 
all: $(TARGET)
 
clean:
        rm -f $(TARGET)
 
%.exe: %.cs
        $(MCS) $(MCSFLAGS) $(LIBS) -out:$@ $<

Tags: , ,

How to calculate MD5 in C#

Hi the Code byte[] output = MD5.Create().ComputeHash(input); gets only th MD5 hash from the Name of the File! To get the MD5 of the File you must open the File as Steam FileStream fs = System.IO.File.OpenRead(args[0]) byte[] output = MD5.Create().ComputeHash(fs); gets the MD5 has from the File! Best Rolf

Post new comment