MD5 Hash Compare C# Created Hash to SQL 2005 Create Hash

Last post 04-21-2008 4:01 AM by GavH. 2 replies.

Sort Posts:

  • MD5 Hash Compare C# Created Hash to SQL 2005 Create Hash

    04-19-2008, 8:20 AM
    • Member
      194 point Member
    • GavH
    • Member since 10-12-2006, 9:06 AM
    • Posts 83

    HI All,

     I have an issue which is most likely a lack of knowledge on my part,

    I have a situation which requires me to compare a hash (MD5) generated in an ASP.Net application against a text value (the pre-hashed value) in SQL 2005.

    The hash comes from a cookie and was hashed as MD5 using System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile()

    In SQL I am using the function HASHBYTES

    Please consider the following query:

    SELECT     UID
    FROM         t_Collected_Users
    WHERE     HashBytes('MD5',Username) = @Username
            AND HashBytes('MD5', Password) = @Password

    Where the paramaters @Username and @Password are 32 character hashes generated ASP.Net side.  The trouble is the sql function HashBytes creates a 16 character hash, so the two never compare.

    What can I do to the 32 Character ASP.Net hash to make it a 16 character SQL compatible MD5 hash?

     

    Many Thanks



     

  • Re: MD5 Hash Compare C# Created Hash to SQL 2005 Create Hash

    04-19-2008, 11:51 AM
    Answer
    • All-Star
      90,046 point All-Star
    • SGWellens
    • Member since 01-02-2007, 9:27 PM
    • Twin Cities, MN
    • Posts 7,347
    • Moderator
      TrustedFriends-MVPs

    This may help you:

    master.dbo.fn_varbintohexstr(HashBytes('MD5',Username))

    Note: This is an undocumented function.

    Note: The string it returns is prefixed with 0x which you may need to deal with.

    Steve Wellens

    My blog
  • Re: MD5 Hash Compare C# Created Hash to SQL 2005 Create Hash

    04-21-2008, 4:01 AM
    • Member
      194 point Member
    • GavH
    • Member since 10-12-2006, 9:06 AM
    • Posts 83

    Thanks,

     

    thats just what I needed.

     

    Much appreciated. 

Page 1 of 1 (3 items)