MD5

J

Joshua Russell

Hi,
I'm trying to write a method that will take a string and return the 32Byte MD5 Digest (also in the form of a string).
The method I'm using is:

string MD5hash (string data)
{
// This is one implementation of the abstract class MD5.
MD5 md5 = new MD5CryptoServiceProvider();

ASCIIEncoding encoding=new ASCIIEncoding();
byte[] result = md5.ComputeHash(encoding.GetBytes(data));

string crypt = "";
for(int x = 0;x <= result.Length - 1; x++)
{
crypt += result[x];
}

return crypt;
}


The string that this method gives me is 91431072057033211115202222781313839180246. This is NOT the form I want. I'm after a string that looks like this 8f2f5a91b72102cd28355e9fc9000d6e.

Thanx Josh
 
E

Eric

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

static public string MDString(string s)
{
StringBuilder sb = new StringBuilder();
ASCIIEncoding enc = new ASCIIEncoding();
MD5 md = MD5CryptoServiceProvider.Create();

try
{
byte[] hash = md.ComputeHash(enc.GetBytes(s));

foreach (byte b in hash)
sb.Append(b.ToString("x2"));
return sb.ToString();
}
finally
{
md.Clear();
}
}

How about this? The form you were refering to is hex, which is what this
method does for a class I wrote earlier for use in my own software. I'm not
entirely sure if md.Clear(); is needed in this situation since we don't
reuse the md(); object within scope, but the description is "frees up
resources" so I didn't want to take any chances. Does anyone know if this is
really needed?

I also used StringBuilder instead of immutable strings for the sake of
efficiency.


"Joshua Russell" <joshATojmyster|DOTukDOTTeuDOOTorgNOSPAM.PLZ> wrote in
message Hi,
I'm trying to write a method that will take a string and return the 32Byte
MD5 Digest (also in the form of a string).
The method I'm using is:

string MD5hash (string data)
{
// This is one implementation of the abstract class MD5.
MD5 md5 = new MD5CryptoServiceProvider();

ASCIIEncoding encoding=new ASCIIEncoding();
byte[] result = md5.ComputeHash(encoding.GetBytes(data));

string crypt = "";
for(int x = 0;x <= result.Length - 1; x++)
{
crypt += result[x];
}

return crypt;
}


The string that this method gives me is
91431072057033211115202222781313839180246. This is NOT the form I want. I'm
after a string that looks like this 8f2f5a91b72102cd28355e9fc9000d6e.

Thanx Josh
 
C

Chris R. Timmons

Hi,
I'm trying to write a method that will take a string and return
the 32Byte MD5 Digest (also in the form of a string). The method
I'm using is:

string MD5hash (string data)
{
// This is one implementation of the abstract class MD5.
MD5 md5 = new MD5CryptoServiceProvider();

ASCIIEncoding encoding=new ASCIIEncoding();
byte[] result = md5.ComputeHash(encoding.GetBytes(data));

string crypt = "";
for(int x = 0;x <= result.Length - 1; x++)
{
crypt += result[x];
}

return crypt;
}


The string that this method gives me is
91431072057033211115202222781313839180246. This is NOT the form
I want. I'm after a string that looks like this
8f2f5a91b72102cd28355e9fc9000d6e.

Josh,

In the "for" loop, try

crypt += result[x].ToString("X2");


Hope this helps.

Chris.
 
J

Joshua Russell

Thx, that seems to work.

Josh

Chris R. Timmons said:
Hi,
I'm trying to write a method that will take a string and return
the 32Byte MD5 Digest (also in the form of a string). The method
I'm using is:

string MD5hash (string data)
{
// This is one implementation of the abstract class MD5.
MD5 md5 = new MD5CryptoServiceProvider();

ASCIIEncoding encoding=new ASCIIEncoding();
byte[] result = md5.ComputeHash(encoding.GetBytes(data));

string crypt = "";
for(int x = 0;x <= result.Length - 1; x++)
{
crypt += result[x];
}

return crypt;
}


The string that this method gives me is
91431072057033211115202222781313839180246. This is NOT the form
I want. I'm after a string that looks like this
8f2f5a91b72102cd28355e9fc9000d6e.

Josh,

In the "for" loop, try

crypt += result[x].ToString("X2");


Hope this helps.

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top