Hash String

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    #1

    Hash String

    Hello,

    How can I hash a string in C#?
    I suppose I should use MD5 ... at least is what is used in ASP.NET
    membership.

    I need to has a string and compare it to an already hashed string.

    Thanks,
    Miguel
  • Peter Morris

    #2
    Re: Hash String

    string me = "Pete";
    int hashCode = me.GetHashCode( );

    Am I missing something?



    --
    Pete
    ====


    Comment

    • Alberto Poblacion

      #3
      Re: Hash String

      "shapper" <mdmoura@gmail. comwrote in message
      news:1f1d8efa-bfb9-4dc0-8c71-c5db3f7eef9d@8g 2000hse.googleg roups.com...
      How can I hash a string in C#?
      I suppose I should use MD5 ... at least is what is used in ASP.NET
      membership.
      >
      I need to has a string and compare it to an already hashed string.
      I use this two functions to get the MD5 hash of either a string or an array
      of bytes:

      public static byte[] GetHash(byte[] data)
      {
      MD5 md5 = MD5.Create();
      byte[] result = md5.ComputeHash (data);
      return result;
      }
      public static byte[] GetHash(string s)
      {
      byte[] myBytes = Encoding.UTF8.G etBytes(s);
      return GetHash(myBytes );
      }

      Comment

      • Peter Morris

        #4
        Re: Hash String

        If you want a one-way hash for passwords try this...

        public static string EncodePassword( string userName, string password)
        {
        TripleDESCrypto ServiceProvider des;
        MD5CryptoServic eProvider hashmd5;
        byte[] pwdhash, buff;
        hashmd5 = new MD5CryptoServic eProvider();
        pwdhash = hashmd5.Compute Hash(ASCIIEncod ing.ASCII.GetBy tes(userName));
        des = new TripleDESCrypto ServiceProvider ();
        des.Key = pwdhash;
        des.Mode = CipherMode.ECB; //CBC, CFB
        buff = ASCIIEncoding.A SCII.GetBytes(p assword);
        string result =
        Convert.ToBase6 4String(des.Cre ateEncryptor(). TransformFinalB lock(buff, 0,
        buff.Length));
        return result;
        }



        --
        Pete
        ====



        Comment

        • Ben Voigt [C++ MVP]

          #5
          Re: Hash String

          Peter Morris wrote:
          string me = "Pete";
          int hashCode = me.GetHashCode( );
          >
          Am I missing something?
          Yes. The result of GetHashCode is subject to change, even between
          subsequent runs of the same program. Only while the program is still
          running are you guaranteed consistent results. So it's useless for
          comparing against a saved hash.


          Comment

          • shapper

            #6
            Re: Hash String

            On Oct 9, 7:08 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
            Peter Morris wrote:
            string me = "Pete";
            int hashCode = me.GetHashCode( );
            >
            Am I missing something?
            >
            Yes.  The result of GetHashCode is subject to change, easicallyven between
            subsequent runs of the same program.  Only while the program is still
            running are you guaranteed consistent results.  So it's useless for
            comparing against a saved hash.
            Basically, in this case I am sending the user a confirmation email
            after she/he registers on a web site.
            On the email is the url. Something has follows:

            www.mydomain.com/ConfirmPassword/?key=...

            where key is an hash of a string created by joining the user UserName,
            Accoount CreateDate, ...

            So when the user goes to that URL I hash the same string again and
            confirm the two to activate the account.

            Isn't this possible?

            Thanks,
            Miguel

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Hash String

              shapper <mdmoura@gmail. comwrote:
              Basically, in this case I am sending the user a confirmation email
              after she/he registers on a web site.
              On the email is the url. Something has follows:
              >
              www.mydomain.com/ConfirmPassword/?key=...
              >
              where key is an hash of a string created by joining the user UserName,
              Accoount CreateDate, ...
              >
              So when the user goes to that URL I hash the same string again and
              confirm the two to activate the account.
              >
              Isn't this possible?
              Yup, and MD5 is probably the best bet for that.

              --
              Jon Skeet - <skeet@pobox.co m>
              Web site: http://www.pobox.com/~skeet
              Blog: http://www.msmvps.com/jon.skeet
              C# in Depth: http://csharpindepth.com

              Comment

              Working...