Best way to calculate hash
-
Hi everyone, Which is the better way to calculate hash of a simple string, should I use
MD5CryptoServiceProvider
or is it OK to use this static method:FormsAuthentication.HashPasswordForStoringInConfigFile
? Please note that it will only be used in an ASP.NET server control. I know the latter method has been designed for a different purpose, but it does exactly what I need in a simple and straightforward way (just 1 line of code), so I was wondering if this would be correct or more efficient or if I should rather useCryptoServiceProvider
approach? Also, withCryptoServiceProvider
, is there a simple way of converting a byte array into a hexadecimal string represenatation (I mean simpler than iterating through the array and appending hexadecimal represenatation of each byte to the end of the string)? Thanks in advance for any ideas, Rado
Radoslav Bielik http://www.neomyz.com/poll [^] - Get your own web poll
-
Hi everyone, Which is the better way to calculate hash of a simple string, should I use
MD5CryptoServiceProvider
or is it OK to use this static method:FormsAuthentication.HashPasswordForStoringInConfigFile
? Please note that it will only be used in an ASP.NET server control. I know the latter method has been designed for a different purpose, but it does exactly what I need in a simple and straightforward way (just 1 line of code), so I was wondering if this would be correct or more efficient or if I should rather useCryptoServiceProvider
approach? Also, withCryptoServiceProvider
, is there a simple way of converting a byte array into a hexadecimal string represenatation (I mean simpler than iterating through the array and appending hexadecimal represenatation of each byte to the end of the string)? Thanks in advance for any ideas, Rado
Radoslav Bielik http://www.neomyz.com/poll [^] - Get your own web poll
Radoslav Bielik wrote: Also, with CryptoServiceProvider, is there a simple way of converting a byte array into a hexadecimal string represenatation
byte[] bits = ...;
string hash = System.Text.Encoding.Default.ToString(bits);Jonathan de Halleux - My Blog
-
Radoslav Bielik wrote: Also, with CryptoServiceProvider, is there a simple way of converting a byte array into a hexadecimal string represenatation
byte[] bits = ...;
string hash = System.Text.Encoding.Default.ToString(bits);Jonathan de Halleux - My Blog
That would be actually very nice, but I'm not sure I get it right or if I'm doing something wrong. The
ToString()
method ofEncoding
class doesn't take any input arguments. There is theGetString(byte[] ...)
method, but it will return a string representation of the bytes in the byte array, not a hexadecimal representation (like "7ba183d2") which is what I'm trying to do. And finally, there is also theBitConverter
class with itsToString(byte[] ...)
method which works, but it will return a string where octets are separated by dashes. Rado
Radoslav Bielik http://www.neomyz.com/poll [^] - Get your own web poll
-
That would be actually very nice, but I'm not sure I get it right or if I'm doing something wrong. The
ToString()
method ofEncoding
class doesn't take any input arguments. There is theGetString(byte[] ...)
method, but it will return a string representation of the bytes in the byte array, not a hexadecimal representation (like "7ba183d2") which is what I'm trying to do. And finally, there is also theBitConverter
class with itsToString(byte[] ...)
method which works, but it will return a string where octets are separated by dashes. Rado
Radoslav Bielik http://www.neomyz.com/poll [^] - Get your own web poll
Sorry,
ToString
wasGetString
indeed. I though you just needed to convert a byte[] to string. Do you really need hex representation ? you can convert to Base64. Jonathan de Halleux - My Blog -
Sorry,
ToString
wasGetString
indeed. I though you just needed to convert a byte[] to string. Do you really need hex representation ? you can convert to Base64. Jonathan de Halleux - My BlogJonathan de Halleux wrote: Sorry, ToString was GetString indeed. I though you just needed to convert a byte[] to string. Do you really need hex representation ? Yes, I should have made that more obvious :-O Thank you for your response anyway! Rado
Radoslav Bielik http://www.neomyz.com/poll [^] - Get your own web poll
-
Jonathan de Halleux wrote: Sorry, ToString was GetString indeed. I though you just needed to convert a byte[] to string. Do you really need hex representation ? Yes, I should have made that more obvious :-O Thank you for your response anyway! Rado
Radoslav Bielik http://www.neomyz.com/poll [^] - Get your own web poll
Just enumerate the
byte[]
array and useToString("x2")
on each byte - appending the value - to have a valid hexidecimal representation. Make sure you include the "2", though, otherwise your hex string may not be a multiple of 2 (so any value less than 128 would not have the first "0"):byte[] buffer = Encoding.Default.GetBytes("Hello, world!");
StringBuilder sb = new StringBuilder(buffer.Length * 2); // Initial capacity
foreach (byte b in buffer)
sb.Append(b.ToString("x2")); // Faster than using StringBuilder.AppendFormat
return sb.ToString();Software Design Engineer Developer Division Sustained Engineering, Microsoft My Articles
-
Hi everyone, Which is the better way to calculate hash of a simple string, should I use
MD5CryptoServiceProvider
or is it OK to use this static method:FormsAuthentication.HashPasswordForStoringInConfigFile
? Please note that it will only be used in an ASP.NET server control. I know the latter method has been designed for a different purpose, but it does exactly what I need in a simple and straightforward way (just 1 line of code), so I was wondering if this would be correct or more efficient or if I should rather useCryptoServiceProvider
approach? Also, withCryptoServiceProvider
, is there a simple way of converting a byte array into a hexadecimal string represenatation (I mean simpler than iterating through the array and appending hexadecimal represenatation of each byte to the end of the string)? Thanks in advance for any ideas, Rado
Radoslav Bielik http://www.neomyz.com/poll [^] - Get your own web poll
Ask yourself - do you want faster code or do you want to write less code? Using
FormsAuthentication.HashPasswordForStoringInConfigFile
is quicker to use (albeit a long method name!) but using theMD5
class (orMD5CryptoServiceProvider
- it's really the same thing) will result in a little less code (not much, but will save a few extra IL instructions):MD5 md5 = MD5.Create();
byte[] buffer = Encoding.UTF8.GetBytes(inputString);
byte[] hash = md5.ComputeHash(buffer);
StringBuilder sb = new StringBuilder(hash.Length * 2);
foreach (byte b in buffer)
sb.Append(b.ToString("x2");
return sb.ToString();FormsAuthentication.HashPasswordForStoringInConfigFile
adds a few steps for comparing the string you pass ("md5" or "sha1").Software Design Engineer Developer Division Sustained Engineering, Microsoft My Articles
-
Just enumerate the
byte[]
array and useToString("x2")
on each byte - appending the value - to have a valid hexidecimal representation. Make sure you include the "2", though, otherwise your hex string may not be a multiple of 2 (so any value less than 128 would not have the first "0"):byte[] buffer = Encoding.Default.GetBytes("Hello, world!");
StringBuilder sb = new StringBuilder(buffer.Length * 2); // Initial capacity
foreach (byte b in buffer)
sb.Append(b.ToString("x2")); // Faster than using StringBuilder.AppendFormat
return sb.ToString();Software Design Engineer Developer Division Sustained Engineering, Microsoft My Articles
Yes, this is exactly what I'm doing but it looked too complicated to me and I thought that there surely must be a simpler way. :rolleyes: Originally, I was just concatenating the strings using the += operator, but using the
StringBuilder's Append
method is probably more efficient? Thanks for your ideas, Rado
Radoslav Bielik http://www.neomyz.com/poll [^] - Get your own web poll
-
Ask yourself - do you want faster code or do you want to write less code? Using
FormsAuthentication.HashPasswordForStoringInConfigFile
is quicker to use (albeit a long method name!) but using theMD5
class (orMD5CryptoServiceProvider
- it's really the same thing) will result in a little less code (not much, but will save a few extra IL instructions):MD5 md5 = MD5.Create();
byte[] buffer = Encoding.UTF8.GetBytes(inputString);
byte[] hash = md5.ComputeHash(buffer);
StringBuilder sb = new StringBuilder(hash.Length * 2);
foreach (byte b in buffer)
sb.Append(b.ToString("x2");
return sb.ToString();FormsAuthentication.HashPasswordForStoringInConfigFile
adds a few steps for comparing the string you pass ("md5" or "sha1").Software Design Engineer Developer Division Sustained Engineering, Microsoft My Articles
That's what I was basically interested in, which one is more efficient and elegant, and I thank you a lot for making it clear to me. :) In this case, the performance was not really an issue because the operation doesn't occur too often, but the few more lines of code are good if it is a more elegant way to use MD5CryptoProvider. :) Thanks again Heath! Rado
Radoslav Bielik http://www.neomyz.com/poll [^] - Get your own web poll