Hi Grant,
Yes, the MD5 algorithm produces a hash value represented as a byte array.
The string value you have looks like the hex representation of this byte
array.
Are you planning to use this internally in your application, or are you
expecting it to work with other applications that are using the
FormsAuthentication encoding? If it's the latter, you need to do it the
same way the FormsAuthentication class is encoding the string.
Looks like the FormsAuthentication.HashPasswordForStoringInConfigFile is
doing a UTF-8 encoding of the input string, passing it through the MD5
hash algorithm, and then converting the resulting byte array into a hex
representation (using its internal MachineKey.ByteArrayToHexString) to
produce the output string.
The MachineKey.ByteArrayToHexString is internal and not accessible, so you
have to do the conversion yourself.
It would be something like this
string GetMD5HashCodeString(string str)
{
// create default implementation of MD5 hash algorithm
MD5 md5 = MD5.Create();
// use UTF-8 encoding of input string and pass it through
// hash algorith to get hash data
byte [] data =
md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str));
// convert hash data into hex string
StringBuilder sb = new StringBuilder();
foreach (byte b in data)
{
// use X2 format string for each byte to represent in capitalized
// hex notation (with leading 0 showing)
sb.Append(b.ToString("X2"));
}
return sb.ToString();
}
I haven't tested this, so I'm not sure if it will work. Give it a try.
-------------------
Rodger Constandse
Sequence Diagram Editor - A quick and easy way to create and edit sequence
diagrams. Learn more at <
http://www.SequenceDiagramEditor.com>
Grant said:
Thanks for the help Rodger
I used the example you gave me, butcannot seem to translate the result
back into a string.
Instead i get System.Byte[]
What i am trying to achieve is:
Given the string "grant"
I want "64FAD28965D003CDE964EA3016E257A3" returned
Here is my code:
private void btnGetHashCOde_Click(object sender, System.EventArgs e)
{
tbResult.Text = (MD5HashFromString(tbSource.Text)).ToString();
}
byte [] MD5HashFromString(string str)
{
// This is one implementation of the abstract class MD5.
MD5 md5 = new MD5CryptoServiceProvider();
System.Text.UnicodeEncoding ue = new System.Text.UnicodeEncoding();
byte [] data = ue.GetBytes(str);
byte[] result = md5.ComputeHash(data);
return result;
}
How should i correct this to get my desired result?
Thanks in advance again
Hi Grant,
The MD5 algorithm requires a byte [] or Stream to compute the hash. You
can use the UnicodeEncoding class in System.Text.Encoding, wich uses the
UTF-16 encoding, to get a byte [] representation of the string. You can
also use some other encoder in System.Text.Encoding depending on the
encoding type you want to use.
For example,
byte [] MD5HashFromString(string str)
{
// This is one implementation of the abstract class MD5.
MD5 md5 = new MD5CryptoServiceProvider();
UnicodeEncoding ue = new UnicodeEncoding();
byte [] data = ue.GetBytes(str);
byte[] result = md5.ComputeHash(data);
return result;
}
Hope this helps
----------------
Rodger Constandse (<
http://www.effexis.com>)
Sequence Diagram Editor - A quick and easy way to create and edit
sequence diagrams. Learn more at <
http://www.sequencediagrameditor.com>
Grant Merwitz wrote:
Hi
I am trying to get a MD5 hashcode from a string.
I have done this before in ASP.NET using the
FormsAuthentication.HashPasswordForStoringInConfigFile
How can i get this hashcode from a string in a windows form
TIA
Grant