-1

I'm struggling with a legacy code base and an MD5 function (shared below) that is returning a value that I cannot recreate in any fashion via SQL Server, despite using HASHBYTES, Base64 encoding or other strategies.

Our MD5 C# function:

public string Md5(string value)
{
    CryptographyManager crypto = EnterpriseLibraryContainer.Current.GetInstance<CryptographyManager>();

    //byte[] valueToHash = (new UnicodeEncoding()).GetBytes(value);
    string generatedHash = crypto.CreateHash("MD5CryptoServiceProvider", value);

    // Clear the byte array memory.
    //Array.Clear(valueToHash, 0, valueToHash.Length);

    return generatedHash;
}

When I run the word 'test' through the MD5 function in C#, it returns the following:

RTYSPVSIIGNYx5++zd8EfNwYAONmPWZnsKaYiQHBCP8=

This is not standard Base64 encoding, and I cannot figure out a way to recreate it in SQL for an associated project.

Please help me to understand what the MD5 function above is returning.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Hi Derek, I do not have an answer for your question, but I do found a few links that might be helpful for you. Furthermore, perhaps you can try ask this question or search on [crytography.stackexchange](https://crypto.stackexchange.com). Links : [1](https://crypto.stackexchange.com/questions/29234/is-base64-the-best-two-way-hash-function-to-encrypt-and-transmit-a-set-of-intege?newreg=10fc9b4ec5c346748c5fa102fe4785bd) [2](https://stackoverflow.com/questions/4278170/md5-hash-and-base64-encoding) Hope it helps :). – LEE Hau Chon May 02 '19 at 02:54
  • 2
    "This is not standard Base64 encoding" what is your basis for claiming this? – Ian Kemp May 02 '19 at 05:21

1 Answers1

-1

test this : Hope it helps MD5 Hash and Base64 encoding

public static string MD5(this string normal)
        {
            byte[] textBytes = System.Text.Encoding.UTF8.GetBytes(normal);
            try
            {
                System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler;
                cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] hash = cryptHandler.ComputeHash(textBytes);
                string ret = "";
                foreach (byte a in hash)
                {
                    if (a < 16)
                        ret += "0" + a.ToString("x");
                    else
                        ret += a.ToString("x");
                }
                return ret;
            }
            catch
            {
                throw;
            }
        }
saman samadi
  • 436
  • 2
  • 8
  • 19
  • It should be mentioned that you return hex not Base64. Also your hex code can be simplified by replacing your if block with just `ret += a.ToString("X2");`. – ckuri May 02 '19 at 08:37