Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Best way to calculate hash

Best way to calculate hash

Scheduled Pinned Locked Moved C#
csharpasp-netcomsysadmindata-structures
9 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Radoslav Bielik
    wrote on last edited by
    #1

    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 use CryptoServiceProvider approach? Also, with CryptoServiceProvider, 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

    J H 2 Replies Last reply
    0
    • R Radoslav Bielik

      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 use CryptoServiceProvider approach? Also, with CryptoServiceProvider, 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

      J Offline
      J Offline
      Jonathan de Halleux
      wrote on last edited by
      #2

      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

      R 1 Reply Last reply
      0
      • J Jonathan de Halleux

        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

        R Offline
        R Offline
        Radoslav Bielik
        wrote on last edited by
        #3

        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 of Encoding class doesn't take any input arguments. There is the GetString(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 the BitConverter class with its ToString(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

        J 1 Reply Last reply
        0
        • R Radoslav Bielik

          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 of Encoding class doesn't take any input arguments. There is the GetString(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 the BitConverter class with its ToString(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

          J Offline
          J Offline
          Jonathan de Halleux
          wrote on last edited by
          #4

          Sorry, ToString was GetString 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

          R 1 Reply Last reply
          0
          • J Jonathan de Halleux

            Sorry, ToString was GetString 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

            R Offline
            R Offline
            Radoslav Bielik
            wrote on last edited by
            #5

            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

            H 1 Reply Last reply
            0
            • R Radoslav Bielik

              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

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              Just enumerate the byte[] array and use ToString("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

              R 1 Reply Last reply
              0
              • R Radoslav Bielik

                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 use CryptoServiceProvider approach? Also, with CryptoServiceProvider, 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

                H Offline
                H Offline
                Heath Stewart
                wrote on last edited by
                #7

                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 the MD5 class (or MD5CryptoServiceProvider - 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

                R 1 Reply Last reply
                0
                • H Heath Stewart

                  Just enumerate the byte[] array and use ToString("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

                  R Offline
                  R Offline
                  Radoslav Bielik
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • H Heath Stewart

                    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 the MD5 class (or MD5CryptoServiceProvider - 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

                    R Offline
                    R Offline
                    Radoslav Bielik
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups