Hi,
I need MD5 calculator which calculate md5 of a file using foreach loop.
please help me
Thanks

Here's some code I grabbed from my old app

using System.Security.Cryptography;

/// <summary>
/// Calculate MD5 for the bytes in a stream object
/// </summary>
/// <param name="StreamIn">A stream in</param>
/// <param name="ResultMD5">Returns MD5 as a byte array</param>
private void CalculateMD5(Stream StreamIn, ref byte[] ResultMD5)
{
    MD5 ProvMD5;
    ProvMD5 = MD5CryptoServiceProvider.Create();
    ProvMD5.Initialize();
    ResultMD5 = ProvMD5.ComputeHash(StreamIn);
    ProvMD5.Clear();
}

/// <summary>
/// Return MD5 as a hex string
/// </summary>
/// <param name="BytesIn">MD5 value as an array of bytes</param>
/// <returns></returns>
private string MD5ToString(byte[] BytesIn)
{
    int UBits;
    int LBits;
    string TempStr;
    int i;

    TempStr = "";
    for (i = 0; i >= BytesIn.GetUpperBound(0); i++)
    {
        UBits = (BytesIn[i] & 240) >> 4;
        LBits = (BytesIn[i] & 15);
        TempStr += string.Format("{0:x}", UBits);
        TempStr += string.Format("{0:x}", LBits);
    }
    return (TempStr);
}

The first sub calculates MD5 from a stream and returns the hash value as an array of bytes. The latter sub takes in an array of bytes and returns it as a hex string.

Here's a snippet for testing the code

string[] FileNames;
FileStream NewFileStream;
byte[] Buffer = new byte[0];
            
// Fill FileNames array

foreach (string s in FileNames)
{
    NewFileStream = new FileStream(s, FileMode.Open, FileAccess.Read);
    CalculateMD5(NewFileStream, ref Buffer);
    Console.WriteLine(s + " : " + MD5ToString(Buffer));
}
Console.ReadKey();

And yes, there's a foreach loop :D

commented: Great! +10

sir here u use {0:x}

why u didn't use {0:x1} or {0:x1}

commented: You can't. -2

sir here u use {0:x}
why u didn't use {0:x1} or {0:x1}

That's a bit OT when the point is to calculate MD5. Anyway, that function returns a hex string like I wanted to have my MD5 represented. You can use some other representation for MD5, although hex format is a "standard" way. And by the way, I don't think that {0:x1} is a valid string format :-/

it's not giving 32 charter long string
is i use x2 instead of x1 it gives 32.I didn't know why?
please help me

A small and easy-to-spot bug in converting byte array to hex string (I had ported this code from VB.NET)

private string MD5ToString(byte[] BytesIn)
{
    int UBits;
    int LBits;
    string TempStr;
    int i;

    TempStr = "";
    for (i = 0; i <= BytesIn.GetUpperBound(0); i++)
    {
        UBits = (BytesIn[i] & 240) >> 4;
        LBits = (BytesIn[i] & 15);
        TempStr += string.Format("{0:x}", UBits);
        TempStr += string.Format("{0:x}", LBits);
    }
    return (TempStr);
}

or without VB.NET-stylish GetUpperBound(0):

private string MD5ToString(byte[] BytesIn)
{
    int UBits;
    int LBits;
    string TempStr;
    int i;

    TempStr = "";
    for (i = 0; i < BytesIn.Length; i++)
    {
        UBits = (BytesIn[i] & 240) >> 4;
        LBits = (BytesIn[i] & 15);
        TempStr += string.Format("{0:x}", UBits);
        TempStr += string.Format("{0:x}", LBits);
    }
    return (TempStr);
}

Now it (both) return 32 hex chars. And if you want the hex string in uppercase, use ToUpper() method to the returned string.

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.