1

I am new to C# just want to ask some questions.

  1. I got a MD5 sum in C#, I should put the code in a class, but where am I going to call this method code from? ASPX, or what?. I remember that class cannot run on its own.

  2. How to write the method to call that?

  3. The file that i want to create a MD5 has for is a text file.

This is what I have found:

public static string CalculateMD5Hash(string strInput)
{
  MD5 md5 = System.Security.Cryptography.MD5.Create();
  byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(strInput);
  byte[] hash = md5.ComputeHash(inputBytes);             

  StringBuilder sb = new StringBuilder();            
  for (int i = 0; i < hash.Length; i++)            
  {                
    sb.Append(hash[i].ToString("x2")); 
  }         
  return sb.ToString();       
} 
larsmoa
  • 12,604
  • 8
  • 62
  • 85
CutexBabies
  • 103
  • 1
  • 4
  • 10
  • Are you trying to generate an MD5 hash of a file from a Console application? – tofutim May 31 '11 at 07:28
  • That is a method you can just put it in your code behind and call it CalculateMD5Hash(AnyData); – Ruben May 31 '11 at 07:28
  • That's up to you to decide what sort of application you want to host it in... windows forms app? console app? web app? We can't tell you that, we don't know what you need to do with it - add some more information about your intended use of this. (by the way md5 is considered to be very weak!!) – Nathan May 31 '11 at 07:29
  • what a console application? i just want to see the output of the result after md5 the text file(seeing the crypt value) – CutexBabies May 31 '11 at 07:39
  • @Nathan , what i intend is to see the value of the text file after it go through the md5 – CutexBabies May 31 '11 at 07:40

1 Answers1

1

You need to put this method inside some class. For example you could create a console application with the following contents:

using System;
using System.Security.Cryptography;
using System.Text;

public class CryptoUtils
{
    public static string CalculateMD5Hash(string strInput)
    {
        MD5 md5 = System.Security.Cryptography.MD5.Create();
        byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(strInput);
        byte[] hash = md5.ComputeHash(inputBytes);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hash.Length; i++)
        {
            sb.Append(hash[i].ToString("x2"));
        }
        return sb.ToString();
    } 
}

class Program
{
    static void Main()
    {
        var input = "some input";
        var md5 = CryptoUtils.CalculateMD5Hash(input);
        Console.WriteLine(md5);
    }
}

Now the CryptoUtils class could be placed in a separate file.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 2
    I suggest you can use UTF-8 but not ASCII. Otherwise, you hardly to process Asia language correctly. – Lu Ming May 31 '11 at 07:35
  • oh, how to change it? and also what will be the diff if i use web app or window form app, cause i not sure what really is console app – CutexBabies May 31 '11 at 07:44
  • 1
    @CutexBabies, I would strongly recommend you learning some basic things in .NET about the different types of applications (web, desktop, console, ...), assemblies, namespaces, classes, ... before calculating MD5 hashes. You seem to be lacking some very basic understanding and without those foundations it will be difficult for you to *reuse* code samples you find over the internet. – Darin Dimitrov May 31 '11 at 07:46
  • cause i better in vb than c sharp...what i mean was the code diff in each of the application and also i never hear of console – CutexBabies May 31 '11 at 07:52
  • @CutexBabies: I totally agree with Darin Dimitrov - you lack some of the very basics required to successully write a piece of software. You should read into things first: the statement "i never hear of console" tells me that you have no idea what you're doing. I suggest you buy a beginner's book on .NET (C#, VB.NET) development and go through it first. – Thorsten Dittmar May 31 '11 at 08:02
  • And no, I'm not being arrogant. On the contrary. I hope you will quickly learn and enjoy software development. But you need some knowledge to begin with. Otherwise it is hard to answer your questions without adding to your list of questions. – Thorsten Dittmar May 31 '11 at 08:04
  • if i just want to use the output does it still matters which app i using – CutexBabies May 31 '11 at 08:24
  • @CutexBabies, yes it matters. Where do you want to use this output? In what context? In what application? – Darin Dimitrov May 31 '11 at 08:33
  • the output right i need to compare with it with another md5 which use java language...which mean that i must compare c# md5 output to java output...i need to md5 a text file...as for what application, my boss did not specific...so it make me more confuse...is this what you asking? – CutexBabies May 31 '11 at 08:48
  • @Darin i can use any of the application...just that iproduce a result/output – CutexBabies Jun 01 '11 at 00:44