-1

What's the equivalent C# code of following Java code:

import org.apache.shiro.crypto.hash.Md5Hash;
public static String enPwd(String username, String pwd) { return (new Md5Hash(username + pwd, username, 2)).toHex().toString(); }

I want to know how to get same hash result in C#.

Sven
  • 79
  • 10
  • tried, but not get same result – Sven Oct 09 '19 at 01:54
  • Please edit it to include a [mcve], and test data and expected hash. You might also want to look at the source code for Md5Hash in Java: [here](https://github.com/apache/shiro/blob/master/crypto/hash/src/main/java/org/apache/shiro/crypto/hash/Md5Hash.java), and the super class [here](https://github.com/apache/shiro/blob/master/crypto/hash/src/main/java/org/apache/shiro/crypto/hash/SimpleHash.java) – ProgrammingLlama Oct 09 '19 at 02:03
  • 1
    Why on earth would you want an MD5 for a password hash? – theMayer Oct 09 '19 at 02:30

1 Answers1

0

As far as I can tell from reading the Java code here and here, the difference is that it performs multiple iterations of the MD5 algorithm.

Taking this answer and modifying it, I've reached this:

public static string CreateMD5(string input, string salt = null, int iterations = 1)
{
    // Use input string to calculate MD5 hash
    using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
    {
        byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);

        // modification from linked answer: prefix with salt
        if (!string.IsNullOrEmpty(salt) != null)
        {
            inputBytes = System.Text.Encoding.ASCII.GetBytes(salt).Concat(inputBytes).ToArray();
        }


        byte[] hashBytes = md5.ComputeHash(inputBytes);



        // modification from linked answer: iterate N times
        for (int i = 1; i < iterations; ++i)
        {
            hashBytes = md5.ComputeHash(hashBytes);
        }




        // Convert the byte array to hexadecimal string
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hashBytes.Length; i++)
        {
            sb.Append(hashBytes[i].ToString("X2"));
        }
        return sb.ToString();
    }
}

public static string enPwd(string username, string pwd)
{
    return CreateMD5(username + pwd, username, 2);
}

As you can see, you just need to hash the resulting hash.

I've replicated what I saw in the Java library here, and the C# code produces the same result here.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86