Search code examples
asp.netasp.net-mvcasp.net-corehashmd5

ASP.NET Core MVC MD5 Hasing is not equal with online MD5 generator


I trying to do MD5 hash. Im compare my result with 3 MD5 online generator. But the result is different. What am I doing wrong?

private static string GenerateHash(string value)
    {
        var data = System.Text.Encoding.UTF8.GetBytes(value);
            data = System.Security.Cryptography.MD5.Create().ComputeHash(data);
        return Convert.ToBase64String(data);
    }

Calling the function:

ViewBag.Secret = GenerateHash("asd");

The result is: eBVpbsvxyW5olLd5RW0zDg==

But on the online generator the result is:7815696ecbf1c96e6894b779456d330e

(im not using salt)


Solution

  • Try this way, reproduced in my local and it's getting the same value with the online Hash Generator.

            public static string GetMd5HashNewMethod(string inputNew)
            {
                using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
                {
                    // inputNew = "Palnati"
                    byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(inputNew);
                    byte[] hashBytes = md5.ComputeHash(inputBytes);
    
                    return Convert.ToHexString(hashBytes); // .NET 5 +
                 // return Convert.ToBase64String(hashBytes);
                    
                }
            }
    

    Output from Code & Online:

    7b1ea3d03dafd1626817916e13a207e1
    
    7b1ea3d03dafd1626817916e13a207e1
    

    @kajahun123 - Yes, you are right we have to use .ToHexString() from .NET 5+